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,45 +1,32 @@
package baishansdk
import (
"encoding/json"
"net/http"
)
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", params, &result)
resp := CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}
func (c *Client) GetDomainConfig(req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := GetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", params, &result)
resp := GetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}
func (c *Client) SetDomainConfig(req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
params := make(map[string]any)
jsonData, _ := json.Marshal(req)
json.Unmarshal(jsonData, &params)
result := SetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", params, &result)
resp := SetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", req, &resp)
if err != nil {
return nil, err
}
return &result, nil
return &resp, nil
}

View File

@@ -29,17 +29,23 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
return c
}
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R()
req.Method = method
req.URL = "https://cdn.api.baishan.com" + path
if strings.EqualFold(method, http.MethodGet) {
data := make(map[string]string)
for k, v := range params {
data[k] = fmt.Sprintf("%v", v)
qs := make(map[string]string)
if params != nil {
temp := make(map[string]any)
jsonData, _ := json.Marshal(params)
json.Unmarshal(jsonData, &temp)
for k, v := range temp {
qs[k] = fmt.Sprintf("%v", v)
}
}
req = req.
SetQueryParams(data).
SetQueryParams(qs).
SetQueryParam("token", c.apiToken)
} else {
req = req.
@@ -58,7 +64,7 @@ func (c *Client) sendRequest(method string, path string, params map[string]any)
return resp, nil
}
func (c *Client) sendRequestWithResult(method string, path string, params map[string]any, result BaseResponse) error {
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result BaseResponse) error {
resp, err := c.sendRequest(method, path, params)
if err != nil {
return err