refactor: clean code

This commit is contained in:
Fu Diwei
2025-04-22 21:18:16 +08:00
parent 8fe942d8d5
commit 3189e65bad
243 changed files with 545 additions and 507 deletions

View File

@@ -0,0 +1,89 @@
package console
import (
"encoding/json"
"errors"
"fmt"
"net/http"
)
func (c *Client) getCookie() error {
req := &signinRequest{Username: c.username, Password: c.password}
res, err := c.sendRequest(http.MethodPost, "/accounts/signin/", req)
if err != nil {
return err
}
resp := &signinResponse{}
if err := json.Unmarshal(res.Body(), &resp); err != nil {
return fmt.Errorf("upyun api error: failed to parse response: %w", err)
} else if !resp.Data.Result {
return errors.New("upyun console signin failed")
}
c.loginCookie = res.Header().Get("Set-Cookie")
return nil
}
func (c *Client) UploadHttpsCertificate(req *UploadHttpsCertificateRequest) (*UploadHttpsCertificateResponse, error) {
if c.loginCookie == "" {
if err := c.getCookie(); err != nil {
return nil, err
}
}
resp := &UploadHttpsCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/api/https/certificate/", req, resp)
return resp, err
}
func (c *Client) GetHttpsCertificateManager(certificateId string) (*GetHttpsCertificateManagerResponse, error) {
if c.loginCookie == "" {
if err := c.getCookie(); err != nil {
return nil, err
}
}
req := &GetHttpsCertificateManagerRequest{CertificateId: certificateId}
resp := &GetHttpsCertificateManagerResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/api/https/certificate/manager/", req, resp)
return resp, err
}
func (c *Client) UpdateHttpsCertificateManager(req *UpdateHttpsCertificateManagerRequest) (*UpdateHttpsCertificateManagerResponse, error) {
if c.loginCookie == "" {
if err := c.getCookie(); err != nil {
return nil, err
}
}
resp := &UpdateHttpsCertificateManagerResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/api/https/certificate/manager", req, resp)
return resp, err
}
func (c *Client) GetHttpsServiceManager(domain string) (*GetHttpsServiceManagerResponse, error) {
if c.loginCookie == "" {
if err := c.getCookie(); err != nil {
return nil, err
}
}
req := &GetHttpsServiceManagerRequest{Domain: domain}
resp := &GetHttpsServiceManagerResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/api/https/services/manager", req, resp)
return resp, err
}
func (c *Client) MigrateHttpsDomain(req *MigrateHttpsDomainRequest) (*MigrateHttpsDomainResponse, error) {
if c.loginCookie == "" {
if err := c.getCookie(); err != nil {
return nil, err
}
}
resp := &MigrateHttpsDomainResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/api/https/migrate/domain", req, resp)
return resp, err
}

View File

@@ -0,0 +1,96 @@
package console
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
username string
password string
loginCookie string
client *resty.Client
}
func NewClient(username, password string) *Client {
client := resty.New()
return &Client{
username: username,
password: password,
client: client,
}
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
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
if strings.EqualFold(method, http.MethodGet) {
qs := 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 {
qs[k] = fmt.Sprintf("%v", v)
}
}
}
req = req.
SetQueryParams(qs).
SetHeader("Cookie", c.loginCookie)
} else {
req = req.
SetHeader("Content-Type", "application/json").
SetHeader("Cookie", c.loginCookie).
SetBody(params)
}
resp, err := req.Send()
if err != nil {
return resp, fmt.Errorf("upyun api error: failed to send request: %w", err)
} else if resp.IsError() {
return resp, fmt.Errorf("upyun api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result interface{}) error {
resp, err := c.sendRequest(method, path, params)
if err != nil {
if resp != nil {
json.Unmarshal(resp.Body(), &result)
}
return err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("upyun api error: failed to parse response: %w", err)
}
tresp := &baseResponse{}
if err := json.Unmarshal(resp.Body(), &tresp); err != nil {
return fmt.Errorf("upyun api error: failed to parse response: %w", err)
} else if tdata := tresp.GetData(); tdata == nil {
return fmt.Errorf("upyun api error: empty data")
} else if errcode := tdata.GetErrorCode(); errcode > 0 {
return fmt.Errorf("upyun api error: %d - %s", errcode, tdata.GetErrorMessage())
}
return nil
}

View File

@@ -0,0 +1,141 @@
package console
import (
"encoding/json"
)
type baseResponse struct {
Data *baseResponseData `json:"data,omitempty"`
}
func (r *baseResponse) GetData() *baseResponseData {
return r.Data
}
type baseResponseData struct {
ErrorCode json.Number `json:"error_code"`
ErrorMessage string `json:"message"`
}
func (r *baseResponseData) GetErrorCode() int32 {
if r.ErrorCode.String() == "" {
return 0
}
errcode, err := r.ErrorCode.Int64()
if err != nil {
return -1
}
return int32(errcode)
}
func (r *baseResponseData) GetErrorMessage() string {
return r.ErrorMessage
}
type signinRequest struct {
Username string `json:"username"`
Password string `json:"password"`
}
type signinResponse struct {
baseResponse
Data *struct {
baseResponseData
Result bool `json:"result"`
} `json:"data,omitempty"`
}
type UploadHttpsCertificateRequest struct {
Certificate string `json:"certificate"`
PrivateKey string `json:"private_key"`
}
type UploadHttpsCertificateResponse struct {
baseResponse
Data *struct {
baseResponseData
Status int32 `json:"status"`
Result struct {
CertificateId string `json:"certificate_id"`
CommonName string `json:"commonName"`
Serial string `json:"serial"`
} `json:"result"`
} `json:"data,omitempty"`
}
type GetHttpsCertificateManagerRequest struct {
CertificateId string `json:"certificate_id"`
}
type GetHttpsCertificateManagerResponse struct {
baseResponse
Data *struct {
baseResponseData
AuthenticateNum int32 `json:"authenticate_num"`
AuthenticateDomains []string `json:"authenticate_domain"`
Domains []HttpsCertificateManagerDomain `json:"domains"`
} `json:"data,omitempty"`
}
type HttpsCertificateManagerDomain struct {
Name string `json:"name"`
Type string `json:"type"`
BucketId int64 `json:"bucket_id"`
BucketName string `json:"bucket_name"`
}
type UpdateHttpsCertificateManagerRequest struct {
CertificateId string `json:"certificate_id"`
Domain string `json:"domain"`
Https bool `json:"https"`
ForceHttps bool `json:"force_https"`
}
type UpdateHttpsCertificateManagerResponse struct {
baseResponse
Data *struct {
baseResponseData
Status bool `json:"status"`
} `json:"data,omitempty"`
}
type GetHttpsServiceManagerRequest struct {
Domain string `json:"domain"`
}
type GetHttpsServiceManagerResponse struct {
baseResponse
Data *struct {
baseResponseData
Status int32 `json:"status"`
Domains []HttpsServiceManagerDomain `json:"result"`
} `json:"data,omitempty"`
}
type HttpsServiceManagerDomain struct {
CertificateId string `json:"certificate_id"`
CommonName string `json:"commonName"`
Https bool `json:"https"`
ForceHttps bool `json:"force_https"`
PaymentType string `json:"payment_type"`
DomainType string `json:"domain_type"`
Validity struct {
Start int64 `json:"start"`
End int64 `json:"end"`
} `json:"validity"`
}
type MigrateHttpsDomainRequest struct {
CertificateId string `json:"crt_id"`
Domain string `json:"domain_name"`
}
type MigrateHttpsDomainResponse struct {
baseResponse
Data *struct {
baseResponseData
Status bool `json:"status"`
} `json:"data,omitempty"`
}