func()

in internal/discovery/group_manager.go [216:264]


func (g *GroupManager) getAppGroup(groupId, appKey string) (*common.AppGroupInfo, error) {
	var (
		urlStr string

		domain          = g.client.Domain()
		namespace       = g.client.Namespace()
		namespaceSource = g.client.NamespaceSource()
	)
	if domain == "" {
		return nil, errors.New("domain missing")
	}

	if namespace != "" {
		urlStr = fmt.Sprintf("http://%s%s?groupId=%s&namespace=%s&appKey=%s", domain, appGroupURL, groupId, namespace, url.QueryEscape(appKey))
		if namespaceSource != "" {
			urlStr += fmt.Sprintf("&namespaceSource=%s", namespaceSource)
		}
	} else {
		urlStr = fmt.Sprintf("http://%s%s?groupId=%s&appKeys=%s", domain, appGroupURL, groupId, appKey)
	}

	resp, err := g.client.HttpClient().Get(urlStr)
	if err != nil {
		return nil, fmt.Errorf("request appGroup failed, groupId:%s, err:%s", groupId, err.Error())
	}
	if resp.StatusCode != http.StatusOK {
		return nil, fmt.Errorf("request appGroup failed, groupId:%s, status:%d", groupId, resp.StatusCode)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return nil, fmt.Errorf("request appGroup failed, groupId:%s, read body error:%s", groupId, err.Error())
	}
	defer resp.Body.Close()
	var respData struct {
		Success bool                 `json:"success"`
		Message string               `json:"message"`
		Data    *common.AppGroupInfo `json:"data"`
	}
	err = json.Unmarshal(body, &respData)
	if err != nil {
		return nil, fmt.Errorf("request appGroup failed, groupId:%s, body:%s unmarshal body error:%s", groupId, string(body), err.Error())
	}
	if !respData.Success {
		return nil, fmt.Errorf("request appGroup failed, groupId:%s, message:%s", groupId, respData.Message)
	}

	return respData.Data, nil
}