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

@ -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 { if err != nil {
return nil, fmt.Errorf("failed to send webhook request: %w", err) return nil, fmt.Errorf("failed to send webhook request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -65,7 +64,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
"body": message, "body": message,
"device_key": n.config.DeviceKey, "device_key": n.config.DeviceKey,
}) })
resp, err := req.Execute(http.MethodPost, serverUrl) resp, err := req.Post(serverUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("bark api error: failed to send request: %w", err) return nil, fmt.Errorf("bark api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"strings" "strings"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -64,7 +63,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
"message": message, "message": message,
"priority": n.config.Priority, "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 { if err != nil {
return nil, fmt.Errorf("gotify api error: failed to send request: %w", err) return nil, fmt.Errorf("gotify api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"strings" "strings"
"github.com/go-resty/resty/v2" "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, "login_id": n.config.Username,
"password": n.config.Password, "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 { if err != nil {
return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err) return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err)
} else if loginResp.IsError() { } 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 { if err != nil {
return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err) return nil, fmt.Errorf("mattermost api error: failed to send request: %w", err)
} else if postResp.IsError() { } else if postResp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "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, "token": n.config.Token,
"user": n.config.User, "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 { if err != nil {
return nil, fmt.Errorf("pushover api error: failed to send request: %w", err) return nil, fmt.Errorf("pushover api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -5,7 +5,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -57,7 +56,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
"content": message, "content": message,
"token": n.config.Token, "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 { if err != nil {
return nil, fmt.Errorf("pushplus api error: failed to send request: %w", err) return nil, fmt.Errorf("pushplus api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "github.com/go-resty/resty/v2"
@ -55,7 +54,7 @@ func (n *NotifierProvider) Notify(ctx context.Context, subject string, message s
"text": subject, "text": subject,
"desp": message, "desp": message,
}) })
resp, err := req.Execute(http.MethodPost, n.config.ServerUrl) resp, err := req.Post(n.config.ServerUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("serverchan api error: failed to send request: %w", err) return nil, fmt.Errorf("serverchan api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "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, "chat_id": n.config.ChatId,
"text": subject + "\n" + message, "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 { if err != nil {
return nil, fmt.Errorf("telegram api error: failed to send request: %w", err) return nil, fmt.Errorf("telegram api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log/slog" "log/slog"
"net/http"
"github.com/go-resty/resty/v2" "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, "content": subject + "\n\n" + message,
}, },
}) })
resp, err := req.Execute(http.MethodPost, n.config.WebhookUrl) resp, err := req.Post(n.config.WebhookUrl)
if err != nil { if err != nil {
return nil, fmt.Errorf("wecom api error: failed to send request: %w", err) return nil, fmt.Errorf("wecom api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -14,25 +14,30 @@ import (
) )
type Client struct { type Client struct {
apiHost string apiKey string
apiVersion string
apiKey string
client *resty.Client client *resty.Client
} }
func NewClient(apiHost, apiVersion, apiKey string) *Client { func NewClient(apiHost, apiVersion, apiKey string) *Client {
client := resty.New()
if apiVersion == "" { if apiVersion == "" {
apiVersion = "v1" 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{ return &Client{
apiHost: strings.TrimRight(apiHost, "/"), client: client,
apiVersion: apiVersion,
apiKey: apiKey,
client: client,
} }
} }
@ -46,16 +51,8 @@ func (c *Client) WithTLSConfig(config *tls.Config) *Client {
return c 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() req := c.client.R()
req.Method = method
req.URL = c.apiHost + "/api/" + c.apiVersion + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -71,17 +68,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
timestamp := fmt.Sprintf("%d", time.Now().Unix()) resp, err := req.Execute(method, path)
token := c.generateToken(timestamp)
req.SetHeader("1Panel-Timestamp", timestamp)
req.SetHeader("1Panel-Token", token)
resp, err := req.Send()
if err != nil { if err != nil {
return resp, fmt.Errorf("1panel api error: failed to send request: %w", err) return resp, fmt.Errorf("1panel api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -13,17 +13,16 @@ import (
) )
type Client struct { type Client struct {
apiToken string
client *resty.Client client *resty.Client
} }
func NewClient(apiToken string) *Client { func NewClient(apiToken string) *Client {
client := resty.New() client := resty.New().
SetBaseURL("https://cdn.api.baishan.com").
SetHeader("token", apiToken)
return &Client{ 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() req := c.client.R()
req.Method = method
req.URL = "https://cdn.api.baishan.com" + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := url.Values{} qs := url.Values{}
if params != nil { if params != nil {
@ -61,17 +58,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParamsFromValues(qs)
SetQueryParam("token", c.apiToken).
SetQueryParamsFromValues(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetQueryParam("token", c.apiToken).
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("baishan api error: failed to send request: %w", err) return resp, fmt.Errorf("baishan api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

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

View File

@ -11,17 +11,16 @@ import (
) )
type Client struct { type Client struct {
apiToken string
client *resty.Client client *resty.Client
} }
func NewClient(apiToken string) *Client { func NewClient(apiToken string) *Client {
client := resty.New() client := resty.New().
SetBaseURL("https://api.bunny.net").
SetHeader("AccessKey", apiToken)
return &Client{ 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() 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) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -50,12 +46,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("bunny api error: failed to send request: %w", err) return resp, fmt.Errorf("bunny api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -11,17 +11,16 @@ import (
) )
type Client struct { type Client struct {
apiToken string
client *resty.Client client *resty.Client
} }
func NewClient(apiToken string) *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{ 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() 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) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -50,12 +46,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("cachefly api error: failed to send request: %w", err) return resp, fmt.Errorf("cachefly api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -12,21 +12,17 @@ import (
) )
type Client struct { type Client struct {
apiHost string
apiKey string
apiSecret string
client *resty.Client client *resty.Client
} }
func NewClient(apiHost, apiKey, apiSecret string) *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{ return &Client{
apiHost: strings.TrimRight(apiHost, "/"), client: client,
apiKey: apiKey,
apiSecret: apiSecret,
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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() 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) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -62,12 +53,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("cdnfly api error: failed to send request: %w", err) return resp, fmt.Errorf("cdnfly api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

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

View File

@ -164,8 +164,8 @@ func (c *Client) sendReq(method string, path string, data map[string]interface{}
if err != nil { if err != nil {
return nil, err return nil, err
} }
req.Header.Add("Content-Type", mime) req.Header.Set("Content-Type", mime)
req.Header.Add("Authorization", auth) req.Header.Set("Authorization", auth)
client := http.Client{} client := http.Client{}
resp, err := client.Do(req) resp, err := client.Do(req)

View File

@ -13,7 +13,6 @@ import (
) )
type Client struct { type Client struct {
apiHost string
apiRole string apiRole string
accessKeyId string accessKeyId string
accessKey string accessKey string
@ -26,15 +25,22 @@ type Client struct {
} }
func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client { func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client {
client := resty.New() client := &Client{
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
apiRole: apiRole, apiRole: apiRole,
accessKeyId: accessKeyId, accessKeyId: accessKeyId,
accessKey: accessKey, 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 { 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetBasicAuth(c.accessKeyId, c.accessKey) req := c.client.R()
req.Method = method
req.URL = c.apiHost + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -64,17 +68,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParams(qs)
SetHeader("X-Cloud-Access-Token", c.accessToken).
SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetHeader("X-Cloud-Access-Token", c.accessToken).
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("flexcdn api error: failed to send request: %w", err) return resp, fmt.Errorf("flexcdn api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -13,7 +13,6 @@ import (
) )
type Client struct { type Client struct {
apiHost string
apiRole string apiRole string
accessKeyId string accessKeyId string
accessKey string accessKey string
@ -26,15 +25,22 @@ type Client struct {
} }
func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client { func NewClient(apiHost, apiRole, accessKeyId, accessKey string) *Client {
client := resty.New() client := &Client{
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
apiRole: apiRole, apiRole: apiRole,
accessKeyId: accessKeyId, accessKeyId: accessKeyId,
accessKey: accessKey, 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 { 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetBasicAuth(c.accessKeyId, c.accessKey) req := c.client.R()
req.Method = method
req.URL = c.apiHost + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -64,17 +68,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParams(qs)
SetHeader("X-Edge-Access-Token", c.accessToken).
SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetHeader("X-Edge-Access-Token", c.accessToken).
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("goedge api error: failed to send request: %w", err) return resp, fmt.Errorf("goedge api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -13,7 +13,6 @@ import (
) )
type Client struct { type Client struct {
apiHost string
username string username string
password string password string
@ -24,14 +23,21 @@ type Client struct {
} }
func NewClient(apiHost, username, password string) *Client { func NewClient(apiHost, username, password string) *Client {
client := resty.New() client := &Client{
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
username: username, username: username,
password: password, 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 { 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) { 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) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -59,17 +65,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParams(qs)
SetHeader("Authorization", "Bearer "+c.accessToken).
SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+c.accessToken).
SetBody(params)
} }
resp, err := req.Execute(method, c.apiHost+"/prod-api"+path) resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err) return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -13,7 +13,6 @@ import (
) )
type Client struct { type Client struct {
apiHost string
username string username string
password string password string
@ -24,14 +23,21 @@ type Client struct {
} }
func NewClient(apiHost, username, password string) *Client { func NewClient(apiHost, username, password string) *Client {
client := resty.New() client := &Client{
return &Client{
apiHost: strings.TrimRight(apiHost, "/"),
username: username, username: username,
password: password, 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 { 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) { 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) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -59,17 +65,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParams(qs)
SetHeader("Authorization", "Bearer "+c.accessToken).
SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetHeader("Authorization", "Bearer "+c.accessToken).
SetBody(params)
} }
resp, err := req.Execute(method, c.apiHost+"/prod-api"+path) resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err) return resp, fmt.Errorf("lecdn api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -11,17 +11,16 @@ import (
) )
type Client struct { type Client struct {
apiToken string
client *resty.Client client *resty.Client
} }
func NewClient(apiToken string) *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{ 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) { 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 := c.client.R()
req.Method = method
req.URL = "https://api.netlify.com/api/v1" + path
if queryParams != nil { if queryParams != nil {
qs := make(map[string]string) qs := make(map[string]string)
@ -63,12 +60,10 @@ func (c *Client) sendRequest(method string, path string, queryParams interface{}
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(payloadParams)
SetHeader("Content-Type", "application/json").
SetBody(payloadParams)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("netlify api error: failed to send request: %w", err) return resp, fmt.Errorf("netlify api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -11,16 +11,15 @@ import (
) )
type Client struct { type Client struct {
apiKey string
client *resty.Client client *resty.Client
} }
func NewClient(apiKey string) *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{ return &Client{
apiKey: apiKey,
client: client, 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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetHeader("x-api-key", c.apiKey) req := c.client.R()
req.Method = method
req.URL = "https://api.v2.rainyun.com" + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
if params != nil { if params != nil {
jsonb, _ := json.Marshal(params) jsonb, _ := json.Marshal(params)
req = req.SetQueryParam("options", string(jsonb)) req = req.SetQueryParam("options", string(jsonb))
} }
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("rainyun api error: failed to send request: %w", err) return resp, fmt.Errorf("rainyun api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -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) { func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
req := c.client.R() req := c.client.R()
req.Method = method
req.URL = path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -98,12 +96,10 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("ratpanel api error: failed to send request: %w", err) return resp, fmt.Errorf("ratpanel api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -11,19 +11,16 @@ import (
) )
type Client struct { type Client struct {
apiHost string
apiToken string
client *resty.Client client *resty.Client
} }
func NewClient(apiHost, apiToken string) *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{ return &Client{
apiHost: strings.TrimRight(apiHost, "/"), client: client,
apiToken: apiToken,
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) { func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
url := c.apiHost + path
req := c.client.R(). req := c.client.R().
SetHeader("Content-Type", "application/json"). SetHeader("Content-Type", "application/json").
SetHeader("X-SLCE-API-TOKEN", c.apiToken).
SetBody(params) SetBody(params)
resp, err := req.Post(url) resp, err := req.Post(path)
if err != nil { if err != nil {
return resp, fmt.Errorf("safeline api error: failed to send request: %w", err) return resp, fmt.Errorf("safeline api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -20,13 +20,21 @@ type Client struct {
} }
func NewClient(username, password string) *Client { func NewClient(username, password string) *Client {
client := resty.New() client := &Client{
return &Client{
username: username, username: username,
password: password, 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 { 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) { 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()
req.Method = method
req.URL = "https://console.upyun.com" + path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -51,17 +57,12 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
} }
} }
req = req. req = req.SetQueryParams(qs)
SetHeader("Cookie", c.loginCookie).
SetQueryParams(qs)
} else { } else {
req = req. req = req.SetHeader("Content-Type", "application/json").SetBody(params)
SetHeader("Content-Type", "application/json").
SetHeader("Cookie", c.loginCookie).
SetBody(params)
} }
resp, err := req.Send() resp, err := req.Execute(method, path)
if err != nil { if err != nil {
return resp, fmt.Errorf("upyun api error: failed to send request: %w", err) return resp, fmt.Errorf("upyun api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {

View File

@ -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) { func (c *Client) sendRequest(method string, path string, params interface{}, configureReq ...func(req *resty.Request)) (*resty.Response, error) {
req := c.client.R() req := c.client.R()
req.Method = method
req.URL = path
if strings.EqualFold(method, http.MethodGet) { if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string) qs := make(map[string]string)
if params != nil { if params != nil {
@ -151,7 +149,7 @@ func (c *Client) sendRequest(method string, path string, params interface{}, con
req = req.SetQueryParams(qs) req = req.SetQueryParams(qs)
} else { } else {
req = req.SetBody(params) req = req.SetHeader("Content-Type", "application/json").SetBody(params)
} }
if configureReq != nil { 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 { if err != nil {
return resp, fmt.Errorf("wangsu api error: failed to send request: %w", err) return resp, fmt.Errorf("wangsu api error: failed to send request: %w", err)
} else if resp.IsError() { } else if resp.IsError() {