refactor: clean code

This commit is contained in:
Fu Diwei
2025-03-23 10:19:24 +08:00
parent 445541c38f
commit 12102ef641
28 changed files with 166 additions and 245 deletions

View File

@@ -5,28 +5,19 @@ import (
)
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
resp := CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
resp := &CreateCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/certificate", req, resp)
return resp, err
}
func (c *Client) GetDomainConfig(req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
resp := GetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
resp := &GetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodGet, "/v2/domain/config", req, resp)
return resp, err
}
func (c *Client) SetDomainConfig(req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
resp := SetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", req, &resp)
if err != nil {
return nil, err
}
return &resp, nil
resp := &SetDomainConfigResponse{}
err := c.sendRequestWithResult(http.MethodPost, "/v2/domain/config", req, resp)
return resp, err
}

View File

@@ -63,7 +63,7 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
req = req.
SetQueryParam("token", c.apiToken).
SetQueryParamsFromValues(qs).SetDebug(true)
SetQueryParamsFromValues(qs)
} else {
req = req.
SetHeader("Content-Type", "application/json").
@@ -73,9 +73,9 @@ func (c *Client) sendRequest(method string, path string, params interface{}) (*r
resp, err := req.Send()
if err != nil {
return nil, 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() {
return nil, fmt.Errorf("baishan api error: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
return resp, fmt.Errorf("baishan api error: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
}
return resp, nil

View File

@@ -6,16 +6,22 @@ type BaseResponse interface {
}
type baseResponse struct {
Code int `json:"code"`
Message string `json:"message"`
Code *int `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
}
func (r *baseResponse) GetCode() int {
return r.Code
if r.Code != nil {
return *r.Code
}
return 0
}
func (r *baseResponse) GetMessage() string {
return r.Message
if r.Message != nil {
return *r.Message
}
return ""
}
type CreateCertificateRequest struct {
@@ -26,7 +32,7 @@ type CreateCertificateRequest struct {
type CreateCertificateResponse struct {
baseResponse
Data *DomainCertificate `json:"data"`
Data *DomainCertificate `json:"data,omitempty"`
}
type GetDomainConfigRequest struct {
@@ -39,7 +45,7 @@ type GetDomainConfigResponse struct {
Data []*struct {
Domain string `json:"domain"`
Config *DomainConfig `json:"config"`
} `json:"data"`
} `json:"data,omitempty"`
}
type SetDomainConfigRequest struct {
@@ -51,7 +57,7 @@ type SetDomainConfigResponse struct {
baseResponse
Data *struct {
Config *DomainConfig `json:"config"`
} `json:"data"`
} `json:"data,omitempty"`
}
type DomainCertificate struct {