refactor: clean code
This commit is contained in:
31
internal/pkg/sdk3rd/btpanel/api.go
Normal file
31
internal/pkg/sdk3rd/btpanel/api.go
Normal file
@@ -0,0 +1,31 @@
|
||||
package btpanelsdk
|
||||
|
||||
func (c *Client) ConfigSavePanelSSL(req *ConfigSavePanelSSLRequest) (*ConfigSavePanelSSLResponse, error) {
|
||||
resp := &ConfigSavePanelSSLResponse{}
|
||||
err := c.sendRequestWithResult("/config?action=SavePanelSSL", req, resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) SiteSetSSL(req *SiteSetSSLRequest) (*SiteSetSSLResponse, error) {
|
||||
resp := &SiteSetSSLResponse{}
|
||||
err := c.sendRequestWithResult("/site?action=SetSSL", req, resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) SystemServiceAdmin(req *SystemServiceAdminRequest) (*SystemServiceAdminResponse, error) {
|
||||
resp := &SystemServiceAdminResponse{}
|
||||
err := c.sendRequestWithResult("/system?action=ServiceAdmin", req, resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) SSLCertSaveCert(req *SSLCertSaveCertRequest) (*SSLCertSaveCertResponse, error) {
|
||||
resp := &SSLCertSaveCertResponse{}
|
||||
err := c.sendRequestWithResult("/ssl/cert/save_cert", req, resp)
|
||||
return resp, err
|
||||
}
|
||||
|
||||
func (c *Client) SSLSetBatchCertToSite(req *SSLSetBatchCertToSiteRequest) (*SSLSetBatchCertToSiteResponse, error) {
|
||||
resp := &SSLSetBatchCertToSiteResponse{}
|
||||
err := c.sendRequestWithResult("/ssl?action=SetBatchCertToSite", req, resp)
|
||||
return resp, err
|
||||
}
|
||||
112
internal/pkg/sdk3rd/btpanel/client.go
Normal file
112
internal/pkg/sdk3rd/btpanel/client.go
Normal file
@@ -0,0 +1,112 @@
|
||||
package btpanelsdk
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"crypto/tls"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
apiHost string
|
||||
apiKey string
|
||||
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(apiHost, apiKey string) *Client {
|
||||
client := resty.New()
|
||||
|
||||
return &Client{
|
||||
apiHost: strings.TrimRight(apiHost, "/"),
|
||||
apiKey: apiKey,
|
||||
client: client,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) WithTLSConfig(config *tls.Config) *Client {
|
||||
c.client.SetTLSClientConfig(config)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) generateSignature(timestamp string) string {
|
||||
keyMd5 := md5.Sum([]byte(c.apiKey))
|
||||
keyMd5Hex := strings.ToLower(hex.EncodeToString(keyMd5[:]))
|
||||
|
||||
signMd5 := md5.Sum([]byte(timestamp + keyMd5Hex))
|
||||
signMd5Hex := strings.ToLower(hex.EncodeToString(signMd5[:]))
|
||||
return signMd5Hex
|
||||
}
|
||||
|
||||
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
|
||||
timestamp := time.Now().Unix()
|
||||
|
||||
data := make(map[string]string)
|
||||
if params != nil {
|
||||
temp := make(map[string]any)
|
||||
jsonb, _ := json.Marshal(params)
|
||||
json.Unmarshal(jsonb, &temp)
|
||||
for k, v := range temp {
|
||||
if v != nil {
|
||||
switch reflect.Indirect(reflect.ValueOf(v)).Kind() {
|
||||
case reflect.String:
|
||||
data[k] = v.(string)
|
||||
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64, reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Float32, reflect.Float64:
|
||||
data[k] = fmt.Sprintf("%v", v)
|
||||
default:
|
||||
if t, ok := v.(time.Time); ok {
|
||||
data[k] = t.Format(time.RFC3339)
|
||||
} else {
|
||||
jbytes, _ := json.Marshal(v)
|
||||
data[k] = string(jbytes)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
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)
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("baota api error: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return resp, fmt.Errorf("baota api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.Body())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) sendRequestWithResult(path string, params interface{}, result BaseResponse) error {
|
||||
resp, err := c.sendRequest(path, params)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("baota api error: failed to parse response: %w", err)
|
||||
} else if errstatus := result.GetStatus(); errstatus != nil && !*errstatus {
|
||||
if result.GetMessage() == nil {
|
||||
return fmt.Errorf("baota api error: unknown error")
|
||||
} else {
|
||||
return fmt.Errorf("baota api error: %s", *result.GetMessage())
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
75
internal/pkg/sdk3rd/btpanel/models.go
Normal file
75
internal/pkg/sdk3rd/btpanel/models.go
Normal file
@@ -0,0 +1,75 @@
|
||||
package btpanelsdk
|
||||
|
||||
type BaseResponse interface {
|
||||
GetStatus() *bool
|
||||
GetMessage() *string
|
||||
}
|
||||
|
||||
type baseResponse struct {
|
||||
Status *bool `json:"status,omitempty"`
|
||||
Message *string `json:"msg,omitempty"`
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetStatus() *bool {
|
||||
return r.Status
|
||||
}
|
||||
|
||||
func (r *baseResponse) GetMessage() *string {
|
||||
return r.Message
|
||||
}
|
||||
|
||||
type ConfigSavePanelSSLRequest struct {
|
||||
PrivateKey string `json:"privateKey"`
|
||||
Certificate string `json:"certPem"`
|
||||
}
|
||||
|
||||
type ConfigSavePanelSSLResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type SiteSetSSLRequest struct {
|
||||
Type string `json:"type"`
|
||||
SiteName string `json:"siteName"`
|
||||
PrivateKey string `json:"key"`
|
||||
Certificate string `json:"csr"`
|
||||
}
|
||||
|
||||
type SiteSetSSLResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type SystemServiceAdminRequest struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type"`
|
||||
}
|
||||
|
||||
type SystemServiceAdminResponse struct {
|
||||
baseResponse
|
||||
}
|
||||
|
||||
type SSLCertSaveCertRequest struct {
|
||||
PrivateKey string `json:"key"`
|
||||
Certificate string `json:"csr"`
|
||||
}
|
||||
|
||||
type SSLCertSaveCertResponse struct {
|
||||
baseResponse
|
||||
SSLHash string `json:"ssl_hash"`
|
||||
}
|
||||
|
||||
type SSLSetBatchCertToSiteRequest struct {
|
||||
BatchInfo []*SSLSetBatchCertToSiteRequestBatchInfo `json:"BatchInfo"`
|
||||
}
|
||||
|
||||
type SSLSetBatchCertToSiteRequestBatchInfo struct {
|
||||
SSLHash string `json:"ssl_hash"`
|
||||
SiteName string `json:"siteName"`
|
||||
CertName string `json:"certName"`
|
||||
}
|
||||
|
||||
type SSLSetBatchCertToSiteResponse struct {
|
||||
baseResponse
|
||||
TotalCount int32 `json:"total"`
|
||||
SuccessCount int32 `json:"success"`
|
||||
FailedCount int32 `json:"faild"`
|
||||
}
|
||||
Reference in New Issue
Block a user