func()

in internal/discovery/group_manager.go [108:152]


func (g *GroupManager) getAppGroupId(groupId, appKey string) (int64, error) {
	if len(g.client.Domain()) == 0 {
		return 0, errors.New("domain missing")
	}
	var urlStr string
	if len(g.client.Namespace()) > 0 {
		urlStr = fmt.Sprintf("http://%s%s?groupId=%s&namespace=%s&appKey=%s", g.client.Domain(), appGroupIdURL, groupId, g.client.Namespace(), url.QueryEscape(appKey))
		if len(g.client.NamespaceSource()) > 0 {
			urlStr += "&namespaceSource=" + g.client.NamespaceSource()
		}
	} else {
		urlStr = fmt.Sprintf("http://%s%s?groupId=%s&appKeys=%s", g.client.Domain(), appGroupIdURL, groupId, appKey)
	}
	resp, err := g.client.HttpClient().Get(urlStr)
	if err != nil {
		return 0, fmt.Errorf("request appGroupId failed, groupId:%s, err:%s", groupId, err.Error())
	}
	if resp.StatusCode != http.StatusOK {
		return 0, fmt.Errorf("request appGroupId failed, groupId:%s, status:%d", groupId, resp.StatusCode)
	}

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return 0, fmt.Errorf("request appGroupId 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    string `json:"data"`
	}
	err = json.Unmarshal(body, &respData)
	if err != nil {
		return 0, fmt.Errorf("request appGroupId failed, groupId:%s, body:%s unmarshal body error:%s", groupId, string(body), err.Error())
	}
	if !respData.Success {
		return 0, fmt.Errorf("request appGroupId failed, groupId:%s, message:%s", groupId, respData.Message)
	}

	ret, err := strconv.ParseInt(respData.Data, 10, 64)
	if err != nil {
		return 0, fmt.Errorf("request appGroupId failed, data expect int, but got=%s", respData.Data)
	}
	return ret, nil
}