finclip-app-manager/infrastructure/client/httpcall/domain.go

77 lines
2.2 KiB
Go
Raw Normal View History

2023-10-31 14:07:26 +08:00
package httpcall
import (
"context"
"encoding/json"
"errors"
"finclip-app-manager/infrastructure/config"
)
type DomainResponseData struct {
Service struct {
Request []string `json:"request"`
Socket []string `json:"socket"`
Download []string `json:"download"`
Upload []string `json:"upload"`
} `json:"service"`
Business struct {
Domains []string `json:"domains"`
} `json:"business"`
Whitelist struct {
Domains []string `json:"domains"`
} `json:"whitelist"`
Blacklist struct {
Domains []string `json:"domains"`
} `json:"blacklist"`
NeedCrt bool `json:"needCrt"`
}
type DomainResponse struct {
Errcode string `json:"errcode"`
Error string `json:"error"`
Data DomainResponseData `json:"data"`
}
func (c *Client) GetAppDomain(ctx context.Context, appId string) (*DomainResponseData, error) {
url := config.GetConfig().MOPDomainURL + appId
log.Infof("GetAppDomain url:%s", url)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetAppDomain client req err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != 200 {
log.Errorf("GetAppDomain rsp code err:%s", rsp.String())
return nil, errors.New("GetAppDomain rsp code no 200")
}
log.Infof("GetAppDomain info:%s", rsp.String())
resp := DomainResponse{}
err = json.Unmarshal(rsp.Body(), &resp)
if err != nil {
log.Errorf("Failed to unmarshal DomainResponse: %v\n", err)
return nil, err
}
return &resp.Data, nil
}
func (c *Client) GetAppDomainByOrganId(ctx context.Context, organId string) (*DomainResponseData, error) {
url := config.GetConfig().MOPDomainURLV2 + "?organId=" + organId
log.Infof("GetAppDomain url:%s", url)
rsp, err := c.Request(ctx).SetHeaders(c.getNewDefaultReqHeader()).Get(url)
if err != nil {
log.Errorf("GetAppDomain client req err:%s", err.Error())
return nil, err
}
if rsp.StatusCode() != 200 {
log.Errorf("GetAppDomain rsp code err:%s", rsp.String())
return nil, errors.New("GetAppDomain rsp code no 200")
}
log.Infof("GetAppDomain info:%s", rsp.String())
resp := DomainResponse{}
err = json.Unmarshal(rsp.Body(), &resp)
if err != nil {
log.Errorf("Failed to unmarshal DomainResponse: %v\n", err)
return nil, err
}
return &resp.Data, nil
}