refactor: clean code

This commit is contained in:
Fu Diwei
2025-05-20 14:15:17 +08:00
parent 7a663d31cb
commit 4ad08d983a
27 changed files with 169 additions and 247 deletions

View File

@@ -14,19 +14,18 @@ import (
)
type Client struct {
apiHost string
apiKey string
apiKey string
client *resty.Client
}
func NewClient(apiHost, apiKey string) *Client {
client := resty.New()
client := resty.New().
SetBaseURL(strings.TrimRight(apiHost, "/"))
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
apiKey: apiKey,
client: client,
apiKey: apiKey,
client: client,
}
}
@@ -78,11 +77,10 @@ func (c *Client) sendRequest(path string, params interface{}) (*resty.Response,
data["request_time"] = fmt.Sprintf("%d", timestamp)
data["request_token"] = c.generateSignature(fmt.Sprintf("%d", timestamp))
url := c.apiHost + path
req := c.client.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetFormData(data)
resp, err := req.Post(url)
resp, err := req.Post(path)
if err != nil {
return resp, fmt.Errorf("baota api error: failed to send request: %w", err)
} else if resp.IsError() {