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

@@ -11,19 +11,16 @@ import (
)
type Client struct {
apiId string
apiSecret string
client *resty.Client
}
func NewClient(apiId, apiSecret string) *Client {
client := resty.New()
client := resty.New().
SetBaseURL("https://api.dns.la/api").
SetBasicAuth(apiId, apiSecret)
return &Client{
apiId: apiId,
apiSecret: apiSecret,
client: client,
client: client,
}
}
@@ -33,9 +30,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
}
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetBasicAuth(c.apiId, c.apiSecret)
req.Method = method
req.URL = "https://api.dns.la/api" + path
req := c.client.R()
if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string)
if params != nil {
@@ -51,12 +46,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs)
} else {
req = req.
SetHeader("Content-Type", "application/json").
SetBody(params)
req = req.SetHeader("Content-Type", "application/json").SetBody(params)
}
resp, err := req.Send()
resp, err := req.Execute(method, path)
if err != nil {
return resp, fmt.Errorf("dnsla api error: failed to send request: %w", err)
} else if resp.IsError() {