refactor: reimpl 3rd sdks

This commit is contained in:
Fu Diwei
2025-02-19 21:55:38 +08:00
parent 688a013d73
commit 469c24751e
16 changed files with 131 additions and 189 deletions

View File

@@ -1,16 +1,8 @@
package gnamesdk
import (
"encoding/json"
)
func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := AddDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/add", params, &result)
err := c.sendRequestWithResult("/api/resolution/add", req, &result)
if err != nil {
return nil, err
}
@@ -18,40 +10,28 @@ func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomai
}
func (c *Client) ModifyDomainResolution(req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ModifyDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/edit", params, &result)
resp := ModifyDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/edit", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}
func (c *Client) DeleteDomainResolution(req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := DeleteDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/delete", params, &result)
resp := DeleteDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/delete", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}
func (c *Client) ListDomainResolution(req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := ListDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/list", params, &result)
resp := ListDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/list", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}

View File

@@ -58,14 +58,15 @@ func (c *Client) generateSignature(params map[string]string) string {
return strings.ToUpper(fmt.Sprintf("%x", hash))
}
func (c *Client) sendRequest(path string, params map[string]any) (*resty.Response, error) {
if params == nil {
params = make(map[string]any)
}
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
data := make(map[string]string)
for k, v := range params {
data[k] = fmt.Sprintf("%v", v)
if params != nil {
temp := make(map[string]any)
jsonData, _ := json.Marshal(params)
json.Unmarshal(jsonData, &temp)
for k, v := range temp {
data[k] = fmt.Sprintf("%v", v)
}
}
data["appid"] = c.appId
data["gntime"] = fmt.Sprintf("%d", time.Now().Unix())
@@ -85,7 +86,7 @@ func (c *Client) sendRequest(path string, params map[string]any) (*resty.Respons
return resp, nil
}
func (c *Client) sendRequestWithResult(path string, params map[string]any, result BaseResponse) error {
func (c *Client) sendRequestWithResult(path string, params interface{}, result BaseResponse) error {
resp, err := c.sendRequest(path, params)
if err != nil {
return err
@@ -94,7 +95,7 @@ func (c *Client) sendRequestWithResult(path string, params map[string]any, resul
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("gname api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != 1 {
return fmt.Errorf("gname api error: %d - %s", errcode, result.GetMsg())
return fmt.Errorf("gname api error: %d - %s", errcode, result.GetMessage())
}
return nil

View File

@@ -2,20 +2,20 @@ package gnamesdk
type BaseResponse interface {
GetCode() int
GetMsg() string
GetMessage() string
}
type baseResponse struct {
Code int `json:"code"`
Msg string `json:"msg"`
Code int `json:"code"`
Message string `json:"msg"`
}
func (r *baseResponse) GetCode() int {
return r.Code
}
func (r *baseResponse) GetMsg() string {
return r.Msg
func (r *baseResponse) GetMessage() string {
return r.Message
}
type AddDomainResolutionRequest struct {