From 4ad08d983a60ead2e291b85a991e0316bded37c9 Mon Sep 17 00:00:00 2001 From: Fu Diwei Date: Tue, 20 May 2025 14:15:17 +0800 Subject: [PATCH] refactor: clean code --- .../deployer/providers/webhook/webhook.go | 2 +- .../pkg/core/notifier/providers/bark/bark.go | 3 +- .../core/notifier/providers/gotify/gotify.go | 3 +- .../providers/mattermost/mattermost.go | 5 +-- .../notifier/providers/pushover/pushover.go | 3 +- .../notifier/providers/pushplus/pushplus.go | 3 +- .../providers/serverchan/serverchan.go | 3 +- .../providers/telegrambot/telegrambot.go | 3 +- .../notifier/providers/wecombot/wecombot.go | 3 +- internal/pkg/sdk3rd/1panel/client.go | 42 +++++++------------ internal/pkg/sdk3rd/baishan/client.go | 22 ++++------ internal/pkg/sdk3rd/btpanel/client.go | 14 +++---- internal/pkg/sdk3rd/bunny/client.go | 18 +++----- internal/pkg/sdk3rd/cachefly/client.go | 18 +++----- internal/pkg/sdk3rd/cdnfly/client.go | 25 ++++------- internal/pkg/sdk3rd/dnsla/client.go | 21 ++++------ internal/pkg/sdk3rd/dogecloud/client.go | 4 +- internal/pkg/sdk3rd/flexcdn/client.go | 33 +++++++-------- internal/pkg/sdk3rd/goedge/client.go | 33 +++++++-------- internal/pkg/sdk3rd/lecdn/v3/client/client.go | 31 +++++++------- internal/pkg/sdk3rd/lecdn/v3/master/client.go | 31 +++++++------- internal/pkg/sdk3rd/netlify/client.go | 19 ++++----- internal/pkg/sdk3rd/rainyun/client.go | 17 +++----- internal/pkg/sdk3rd/ratpanel/client.go | 8 +--- internal/pkg/sdk3rd/safeline/client.go | 15 +++---- internal/pkg/sdk3rd/upyun/console/client.go | 31 +++++++------- internal/pkg/sdk3rd/wangsu/openapi/client.go | 6 +-- 27 files changed, 169 insertions(+), 247 deletions(-) diff --git a/internal/pkg/core/deployer/providers/webhook/webhook.go b/internal/pkg/core/deployer/providers/webhook/webhook.go index 3a21717b..49b07b47 100644 --- a/internal/pkg/core/deployer/providers/webhook/webhook.go +++ b/internal/pkg/core/deployer/providers/webhook/webhook.go @@ -176,7 +176,7 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPE } // 发送请求 - resp, err := req.SetDebug(true).Send() + resp, err := req.Send() if err != nil { return nil, fmt.Errorf("failed to send webhook request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/bark/bark.go b/internal/pkg/core/notifier/providers/bark/bark.go index 4d1902f2..97ece0be 100644 --- a/internal/pkg/core/notifier/providers/bark/bark.go +++ b/internal/pkg/core/notifier/providers/bark/bark.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -65,7 +64,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "body": message, "device_key": n.config.DeviceKey, }) - resp, err := req.Execute(http.MethodPost, serverUrl) + resp, err := req.Post(serverUrl) if err != nil { return nil, fmt.Errorf("bark api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/gotify/gotify.go b/internal/pkg/core/notifier/providers/gotify/gotify.go index 05b2f919..81dcb8ad 100644 --- a/internal/pkg/core/notifier/providers/gotify/gotify.go +++ b/internal/pkg/core/notifier/providers/gotify/gotify.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "strings" "github.com/go-resty/resty/v2" @@ -64,7 +63,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "message": message, "priority": n.config.Priority, }) - resp, err := req.Execute(http.MethodPost, fmt.Sprintf("%s/message", serverUrl)) + resp, err := req.Post(fmt.Sprintf("%s/message", serverUrl)) if err != nil { return nil, fmt.Errorf("gotify api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/mattermost/mattermost.go b/internal/pkg/core/notifier/providers/mattermost/mattermost.go index 4f1c3e9c..a9b2f4d6 100644 --- a/internal/pkg/core/notifier/providers/mattermost/mattermost.go +++ b/internal/pkg/core/notifier/providers/mattermost/mattermost.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "strings" "github.com/go-resty/resty/v2" @@ -64,7 +63,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "login_id": n.config.Username, "password": n.config.Password, }) - loginResp, err := loginReq.Execute(http.MethodPost, fmt.Sprintf("%s/api/v4/users/login", serverUrl)) + loginResp, err := loginReq.Post(fmt.Sprintf("%s/api/v4/users/login", serverUrl)) if err != nil { return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err) } else if loginResp.IsError() { @@ -88,7 +87,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s }, }, }) - postResp, err := postReq.Execute(http.MethodPost, fmt.Sprintf("%s/api/v4/posts", serverUrl)) + postResp, err := postReq.Post(fmt.Sprintf("%s/api/v4/posts", serverUrl)) if err != nil { return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err) } else if postResp.IsError() { diff --git a/internal/pkg/core/notifier/providers/pushover/pushover.go b/internal/pkg/core/notifier/providers/pushover/pushover.go index 459a4950..b7f74bba 100644 --- a/internal/pkg/core/notifier/providers/pushover/pushover.go +++ b/internal/pkg/core/notifier/providers/pushover/pushover.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -59,7 +58,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "token": n.config.Token, "user": n.config.User, }) - resp, err := req.Execute(http.MethodPost, "https://api.pushover.net/1/messages.json") + resp, err := req.Post("https://api.pushover.net/1/messages.json") if err != nil { return nil, fmt.Errorf("pushover api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/pushplus/pushplus.go b/internal/pkg/core/notifier/providers/pushplus/pushplus.go index a8a95ac7..834f9683 100644 --- a/internal/pkg/core/notifier/providers/pushplus/pushplus.go +++ b/internal/pkg/core/notifier/providers/pushplus/pushplus.go @@ -5,7 +5,6 @@ import ( "encoding/json" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -57,7 +56,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "content": message, "token": n.config.Token, }) - resp, err := req.Execute(http.MethodPost, "https://www.pushplus.plus/send") + resp, err := req.Post("https://www.pushplus.plus/send") if err != nil { return nil, fmt.Errorf("pushplus api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/serverchan/serverchan.go b/internal/pkg/core/notifier/providers/serverchan/serverchan.go index cd77ce83..d74b2fcc 100644 --- a/internal/pkg/core/notifier/providers/serverchan/serverchan.go +++ b/internal/pkg/core/notifier/providers/serverchan/serverchan.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -55,7 +54,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "text": subject, "desp": message, }) - resp, err := req.Execute(http.MethodPost, n.config.ServerUrl) + resp, err := req.Post(n.config.ServerUrl) if err != nil { return nil, fmt.Errorf("serverchan api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/telegrambot/telegrambot.go b/internal/pkg/core/notifier/providers/telegrambot/telegrambot.go index a324ee00..99b86a38 100644 --- a/internal/pkg/core/notifier/providers/telegrambot/telegrambot.go +++ b/internal/pkg/core/notifier/providers/telegrambot/telegrambot.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -57,7 +56,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "chat_id": n.config.ChatId, "text": subject + "\n" + message, }) - resp, err := req.Execute(http.MethodPost, fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", n.config.BotToken)) + resp, err := req.Post(fmt.Sprintf("https://api.telegram.org/bot%s/sendMessage", n.config.BotToken)) if err != nil { return nil, fmt.Errorf("telegram api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/core/notifier/providers/wecombot/wecombot.go b/internal/pkg/core/notifier/providers/wecombot/wecombot.go index daa771e0..36c179d4 100644 --- a/internal/pkg/core/notifier/providers/wecombot/wecombot.go +++ b/internal/pkg/core/notifier/providers/wecombot/wecombot.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log/slog" - "net/http" "github.com/go-resty/resty/v2" @@ -57,7 +56,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s "content": subject + "\n\n" + message, }, }) - resp, err := req.Execute(http.MethodPost, n.config.WebhookUrl) + resp, err := req.Post(n.config.WebhookUrl) if err != nil { return nil, fmt.Errorf("wecom api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/1panel/client.go b/internal/pkg/sdk3rd/1panel/client.go index 4480ae49..003203d3 100644 --- a/internal/pkg/sdk3rd/1panel/client.go +++ b/internal/pkg/sdk3rd/1panel/client.go @@ -14,25 +14,30 @@ import ( ) type Client struct { - apiHost string - apiVersion string - apiKey string + apiKey string client *resty.Client } func NewClient(apiHost, apiVersion, apiKey string) *Client { - client := resty.New() - if apiVersion == "" { apiVersion = "v1" } + client := resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/") + "/api/" + apiVersion). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + timestamp := fmt.Sprintf("%d", time.Now().Unix()) + tokenMd5 := md5.Sum([]byte("1panel" + apiKey + timestamp)) + tokenMd5Hex := hex.EncodeToString(tokenMd5[:]) + req.Header.Set("1Panel-Timestamp", timestamp) + req.Header.Set("1Panel-Token", tokenMd5Hex) + + return nil + }) + return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), - apiVersion: apiVersion, - apiKey: apiKey, - client: client, + client: client, } } @@ -46,16 +51,8 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { return c } -func (c *Client) generateToken(timestamp string) string { - tokenMd5 := md5.Sum([]byte("1panel" + c.apiKey + timestamp)) - tokenMd5Hex := hex.EncodeToString(tokenMd5[:]) - return tokenMd5Hex -} - func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { req := c.client.R() - req.Method = method - req.URL = c.apiHost + "/api/" + c.apiVersion + path if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -71,17 +68,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) } - timestamp := fmt.Sprintf("%d", time.Now().Unix()) - token := c.generateToken(timestamp) - req.SetHeader("1Panel-Timestamp", timestamp) - req.SetHeader("1Panel-Token", token) - - resp, err := req.Send() + resp, err := req.Execute(method, path) if err != nil { return resp, fmt.Errorf("1panel api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/baishan/client.go b/internal/pkg/sdk3rd/baishan/client.go index 81790a21..b3e428ee 100644 --- a/internal/pkg/sdk3rd/baishan/client.go +++ b/internal/pkg/sdk3rd/baishan/client.go @@ -13,17 +13,16 @@ import ( ) type Client struct { - apiToken string - client *resty.Client } func NewClient(apiToken string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL("https://cdn.api.baishan.com"). + SetHeader("token", apiToken) return &Client{ - apiToken: apiToken, - client: client, + client: client, } } @@ -34,8 +33,6 @@ 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() - req.Method = method - req.URL = "https://cdn.api.baishan.com" + path if strings.EqualFold(method, http.MethodGet) { qs := url.Values{} if params != nil { @@ -61,17 +58,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetQueryParam("token", c.apiToken). - SetQueryParamsFromValues(qs) + req = req.SetQueryParamsFromValues(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetQueryParam("token", c.apiToken). - 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("baishan api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/btpanel/client.go b/internal/pkg/sdk3rd/btpanel/client.go index e09eed13..1da625da 100644 --- a/internal/pkg/sdk3rd/btpanel/client.go +++ b/internal/pkg/sdk3rd/btpanel/client.go @@ -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() { diff --git a/internal/pkg/sdk3rd/bunny/client.go b/internal/pkg/sdk3rd/bunny/client.go index 0f0dbea5..8d50e1fc 100644 --- a/internal/pkg/sdk3rd/bunny/client.go +++ b/internal/pkg/sdk3rd/bunny/client.go @@ -11,17 +11,16 @@ import ( ) type Client struct { - apiToken string - client *resty.Client } func NewClient(apiToken string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL("https://api.bunny.net"). + SetHeader("AccessKey", apiToken) return &Client{ - apiToken: apiToken, - client: client, + client: client, } } @@ -32,9 +31,6 @@ 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() - req.Method = method - req.URL = "https://api.bunny.net" + path - req = req.SetHeader("AccessKey", c.apiToken) if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -50,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("bunny api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/cachefly/client.go b/internal/pkg/sdk3rd/cachefly/client.go index 37a82ccc..342e329d 100644 --- a/internal/pkg/sdk3rd/cachefly/client.go +++ b/internal/pkg/sdk3rd/cachefly/client.go @@ -11,17 +11,16 @@ import ( ) type Client struct { - apiToken string - client *resty.Client } func NewClient(apiToken string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL("https://api.cachefly.com/api/2.5"). + SetHeader("x-cf-authorization", "Bearer "+apiToken) return &Client{ - apiToken: apiToken, - client: client, + client: client, } } @@ -32,9 +31,6 @@ 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() - req.Method = method - req.URL = "https://api.cachefly.com/api/2.5" + path - req = req.SetHeader("x-cf-authorization", "Bearer "+c.apiToken) if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -50,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("cachefly api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/cdnfly/client.go b/internal/pkg/sdk3rd/cdnfly/client.go index 5d80395c..c8753ed5 100644 --- a/internal/pkg/sdk3rd/cdnfly/client.go +++ b/internal/pkg/sdk3rd/cdnfly/client.go @@ -12,21 +12,17 @@ import ( ) type Client struct { - apiHost string - apiKey string - apiSecret string - client *resty.Client } func NewClient(apiHost, apiKey, apiSecret string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/")). + SetHeader("api-key", apiKey). + SetHeader("api-secret", apiSecret) return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), - apiKey: apiKey, - apiSecret: apiSecret, - client: client, + client: client, } } @@ -42,11 +38,6 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { req := c.client.R() - req.Method = method - req.URL = c.apiHost + path - req = req. - SetHeader("api-key", c.apiKey). - SetHeader("api-secret", c.apiSecret) if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -62,12 +53,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("cdnfly api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/dnsla/client.go b/internal/pkg/sdk3rd/dnsla/client.go index a0659011..d9a86fc5 100644 --- a/internal/pkg/sdk3rd/dnsla/client.go +++ b/internal/pkg/sdk3rd/dnsla/client.go @@ -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() { diff --git a/internal/pkg/sdk3rd/dogecloud/client.go b/internal/pkg/sdk3rd/dogecloud/client.go index 806b0ea9..75342907 100644 --- a/internal/pkg/sdk3rd/dogecloud/client.go +++ b/internal/pkg/sdk3rd/dogecloud/client.go @@ -164,8 +164,8 @@ func (c *Client) sendReq(method string, path string, data map[string]interface{} if err != nil { return nil, err } - req.Header.Add("Content-Type", mime) - req.Header.Add("Authorization", auth) + req.Header.Set("Content-Type", mime) + req.Header.Set("Authorization", auth) client := http.Client{} resp, err := client.Do(req) diff --git a/internal/pkg/sdk3rd/flexcdn/client.go b/internal/pkg/sdk3rd/flexcdn/client.go index 6b5626ee..beae469a 100644 --- a/internal/pkg/sdk3rd/flexcdn/client.go +++ b/internal/pkg/sdk3rd/flexcdn/client.go @@ -13,7 +13,6 @@ import ( ) type Client struct { - apiHost string apiRole string accessKeyId string accessKey string @@ -26,15 +25,22 @@ type Client struct { } func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client { - client := resty.New() - - return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), + client := &Client{ apiRole: apiRole, accessKeyId: accessKeyId, accessKey: accessKey, - client: client, } + client.client = resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/")). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + if client.accessToken != "" { + req.Header.Set("X-Cloud-Access-Token", client.accessToken) + } + + return nil + }) + + return client } func (c *Client) WithTimeout(timeout time.Duration) *Client { @@ -48,9 +54,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { } func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { - req := c.client.R().SetBasicAuth(c.accessKeyId, c.accessKey) - req.Method = method - req.URL = c.apiHost + path + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -64,17 +68,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetHeader("X-Cloud-Access-Token", c.accessToken). - SetQueryParams(qs) + req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetHeader("X-Cloud-Access-Token", c.accessToken). - 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("flexcdn api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/goedge/client.go b/internal/pkg/sdk3rd/goedge/client.go index 39ad8900..3dc961e3 100644 --- a/internal/pkg/sdk3rd/goedge/client.go +++ b/internal/pkg/sdk3rd/goedge/client.go @@ -13,7 +13,6 @@ import ( ) type Client struct { - apiHost string apiRole string accessKeyId string accessKey string @@ -26,15 +25,22 @@ type Client struct { } func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client { - client := resty.New() - - return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), + client := &Client{ apiRole: apiRole, accessKeyId: accessKeyId, accessKey: accessKey, - client: client, } + client.client = resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/")). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + if client.accessToken != "" { + req.Header.Set("X-Edge-Access-Token", client.accessToken) + } + + return nil + }) + + return client } func (c *Client) WithTimeout(timeout time.Duration) *Client { @@ -48,9 +54,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { } func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { - req := c.client.R().SetBasicAuth(c.accessKeyId, c.accessKey) - req.Method = method - req.URL = c.apiHost + path + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -64,17 +68,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetHeader("X-Edge-Access-Token", c.accessToken). - SetQueryParams(qs) + req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetHeader("X-Edge-Access-Token", c.accessToken). - 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("goedge api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/lecdn/v3/client/client.go b/internal/pkg/sdk3rd/lecdn/v3/client/client.go index 218499fa..ad3a752e 100644 --- a/internal/pkg/sdk3rd/lecdn/v3/client/client.go +++ b/internal/pkg/sdk3rd/lecdn/v3/client/client.go @@ -13,7 +13,6 @@ import ( ) type Client struct { - apiHost string username string password string @@ -24,14 +23,21 @@ type Client struct { } func NewClient(apiHost, username, password string) *Client { - client := resty.New() - - return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), + client := &Client{ username: username, password: password, - client: client, } + client.client = resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/") + "/prod-api"). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + if client.accessToken != "" { + req.Header.Set("Authorization", "Bearer "+client.accessToken) + } + + return nil + }) + + return client } func (c *Client) WithTimeout(timeout time.Duration) *Client { @@ -45,7 +51,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { } func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { - req := c.client.R().SetBasicAuth(c.username, c.password) + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -59,17 +65,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetHeader("Authorization", "Bearer "+c.accessToken). - SetQueryParams(qs) + req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetHeader("Authorization", "Bearer "+c.accessToken). - SetBody(params) + req = req.SetHeader("Content-Type", "application/json").SetBody(params) } - resp, err := req.Execute(method, c.apiHost+"/prod-api"+path) + resp, err := req.Execute(method, path) if err != nil { return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/lecdn/v3/master/client.go b/internal/pkg/sdk3rd/lecdn/v3/master/client.go index 2f0cae4e..2da0c0c4 100644 --- a/internal/pkg/sdk3rd/lecdn/v3/master/client.go +++ b/internal/pkg/sdk3rd/lecdn/v3/master/client.go @@ -13,7 +13,6 @@ import ( ) type Client struct { - apiHost string username string password string @@ -24,14 +23,21 @@ type Client struct { } func NewClient(apiHost, username, password string) *Client { - client := resty.New() - - return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), + client := &Client{ username: username, password: password, - client: client, } + client.client = resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/") + "/prod-api"). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + if client.accessToken != "" { + req.Header.Set("Authorization", "Bearer "+client.accessToken) + } + + return nil + }) + + return client } func (c *Client) WithTimeout(timeout time.Duration) *Client { @@ -45,7 +51,7 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { } func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { - req := c.client.R().SetBasicAuth(c.username, c.password) + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -59,17 +65,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetHeader("Authorization", "Bearer "+c.accessToken). - SetQueryParams(qs) + req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetHeader("Authorization", "Bearer "+c.accessToken). - SetBody(params) + req = req.SetHeader("Content-Type", "application/json").SetBody(params) } - resp, err := req.Execute(method, c.apiHost+"/prod-api"+path) + resp, err := req.Execute(method, path) if err != nil { return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/netlify/client.go b/internal/pkg/sdk3rd/netlify/client.go index e3110077..d270e35e 100644 --- a/internal/pkg/sdk3rd/netlify/client.go +++ b/internal/pkg/sdk3rd/netlify/client.go @@ -11,17 +11,16 @@ import ( ) type Client struct { - apiToken string - client *resty.Client } func NewClient(apiToken string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL("https://api.netlify.com/api/v1"). + SetHeader("Authorization", "Bearer "+apiToken) return &Client{ - apiToken: apiToken, - client: client, + client: client, } } @@ -31,9 +30,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client { } func (c *Client) sendRequest(method string, path string, queryParams interface{}, payloadParams interface{}) (*resty.Response, error) { - req := c.client.R().SetHeader("Authorization", "Bearer "+c.apiToken) - req.Method = method - req.URL = "https://api.netlify.com/api/v1" + path + req := c.client.R() if queryParams != nil { qs := make(map[string]string) @@ -63,12 +60,10 @@ func (c *Client) sendRequest(method string, path string, queryParams interface{} req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetBody(payloadParams) + req = req.SetHeader("Content-Type", "application/json").SetBody(payloadParams) } - resp, err := req.Send() + resp, err := req.Execute(method, path) if err != nil { return resp, fmt.Errorf("netlify api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/rainyun/client.go b/internal/pkg/sdk3rd/rainyun/client.go index eb34efd1..80113f0d 100644 --- a/internal/pkg/sdk3rd/rainyun/client.go +++ b/internal/pkg/sdk3rd/rainyun/client.go @@ -11,16 +11,15 @@ import ( ) type Client struct { - apiKey string - client *resty.Client } func NewClient(apiKey string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL("https://api.v2.rainyun.com"). + SetHeader("x-api-key", apiKey) return &Client{ - apiKey: apiKey, client: client, } } @@ -31,21 +30,17 @@ 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().SetHeader("x-api-key", c.apiKey) - req.Method = method - req.URL = "https://api.v2.rainyun.com" + path + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { if params != nil { jsonb, _ := json.Marshal(params) req = req.SetQueryParam("options", string(jsonb)) } } 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("rainyun api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/ratpanel/client.go b/internal/pkg/sdk3rd/ratpanel/client.go index ac2c4062..47202a04 100644 --- a/internal/pkg/sdk3rd/ratpanel/client.go +++ b/internal/pkg/sdk3rd/ratpanel/client.go @@ -81,8 +81,6 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) { req := c.client.R() - req.Method = method - req.URL = path if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -98,12 +96,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("ratpanel api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/safeline/client.go b/internal/pkg/sdk3rd/safeline/client.go index 0412514c..efcd3bd6 100644 --- a/internal/pkg/sdk3rd/safeline/client.go +++ b/internal/pkg/sdk3rd/safeline/client.go @@ -11,19 +11,16 @@ import ( ) type Client struct { - apiHost string - apiToken string - client *resty.Client } func NewClient(apiHost, apiToken string) *Client { - client := resty.New() + client := resty.New(). + SetBaseURL(strings.TrimRight(apiHost, "/")). + SetHeader("X-SLCE-API-TOKEN", apiToken) return &Client{ - apiHost: strings.TrimRight(apiHost, "/"), - apiToken: apiToken, - client: client, + client: client, } } @@ -38,12 +35,10 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client { } func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) { - url := c.apiHost + path req := c.client.R(). SetHeader("Content-Type", "application/json"). - SetHeader("X-SLCE-API-TOKEN", c.apiToken). SetBody(params) - resp, err := req.Post(url) + resp, err := req.Post(path) if err != nil { return resp, fmt.Errorf("safeline api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/upyun/console/client.go b/internal/pkg/sdk3rd/upyun/console/client.go index 6b560adc..b207549e 100644 --- a/internal/pkg/sdk3rd/upyun/console/client.go +++ b/internal/pkg/sdk3rd/upyun/console/client.go @@ -20,13 +20,21 @@ type Client struct { } func NewClient(username, password string) *Client { - client := resty.New() - - return &Client{ + client := &Client{ username: username, password: password, - client: client, } + client.client = resty.New(). + SetBaseURL("https://console.upyun.com"). + SetPreRequestHook(func(c *resty.Client, req *http.Request) error { + if client.loginCookie != "" { + req.Header.Set("Cookie", client.loginCookie) + } + + return nil + }) + + return client } func (c *Client) WithTimeout(timeout time.Duration) *Client { @@ -35,9 +43,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.username, c.password) - req.Method = method - req.URL = "https://console.upyun.com" + path + req := c.client.R() if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -51,17 +57,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r } } - req = req. - SetHeader("Cookie", c.loginCookie). - SetQueryParams(qs) + req = req.SetQueryParams(qs) } else { - req = req. - SetHeader("Content-Type", "application/json"). - SetHeader("Cookie", c.loginCookie). - 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("upyun api error: failed to send request: %w", err) } else if resp.IsError() { diff --git a/internal/pkg/sdk3rd/wangsu/openapi/client.go b/internal/pkg/sdk3rd/wangsu/openapi/client.go index 0bb141d8..a8f4f2af 100644 --- a/internal/pkg/sdk3rd/wangsu/openapi/client.go +++ b/internal/pkg/sdk3rd/wangsu/openapi/client.go @@ -134,8 +134,6 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client { func (c *Client) sendRequest(method string, path string, params interface{}, configureReq ...func(req *resty.Request)) (*resty.Response, error) { req := c.client.R() - req.Method = method - req.URL = path if strings.EqualFold(method, http.MethodGet) { qs := make(map[string]string) if params != nil { @@ -151,7 +149,7 @@ func (c *Client) sendRequest(method string, path string, params interface{}, con req = req.SetQueryParams(qs) } else { - req = req.SetBody(params) + req = req.SetHeader("Content-Type", "application/json").SetBody(params) } if configureReq != nil { @@ -160,7 +158,7 @@ func (c *Client) sendRequest(method string, path string, params interface{}, con } } - resp, err := req.Send() + resp, err := req.Execute(method, path) if err != nil { return resp, fmt.Errorf("wangsu api error: failed to send request: %w", err) } else if resp.IsError() {