refactor: re-impl sdk3rd
This commit is contained in:
@@ -2,9 +2,10 @@ package console
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
@@ -14,18 +15,28 @@ type Client struct {
|
||||
username string
|
||||
password string
|
||||
|
||||
loginCookie string
|
||||
loginCookie string
|
||||
loginCookieMtx sync.Mutex
|
||||
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(username, password string) *Client {
|
||||
func NewClient(username, password string) (*Client, error) {
|
||||
if username == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset username")
|
||||
}
|
||||
if password == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset password")
|
||||
}
|
||||
|
||||
client := &Client{
|
||||
username: username,
|
||||
password: password,
|
||||
}
|
||||
client.client = resty.New().
|
||||
SetBaseURL("https://console.upyun.com").
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("User-Agent", "certimate").
|
||||
SetPreRequestHook(func(c *resty.Client, req *http.Request) error {
|
||||
if client.loginCookie != "" {
|
||||
@@ -35,64 +46,107 @@ func NewClient(username, password string) *Client {
|
||||
return nil
|
||||
})
|
||||
|
||||
return client
|
||||
return client, nil
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
func (c *Client) SetTimeout(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()
|
||||
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)
|
||||
} else {
|
||||
req = req.SetHeader("Content-Type", "application/json").SetBody(params)
|
||||
func (c *Client) newRequest(method string, path string) (*resty.Request, error) {
|
||||
if method == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset method")
|
||||
}
|
||||
if path == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset path")
|
||||
}
|
||||
|
||||
resp, err := req.Execute(method, path)
|
||||
req := c.client.R()
|
||||
req.Method = method
|
||||
req.URL = path
|
||||
return req, nil
|
||||
}
|
||||
|
||||
func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("sdkerr: nil request")
|
||||
}
|
||||
|
||||
// WARN:
|
||||
// PLEASE DO NOT USE `req.SetResult` or `req.SetError` HERE! USE `doRequestWithResult` INSTEAD.
|
||||
|
||||
resp, err := req.Send()
|
||||
if err != nil {
|
||||
return resp, fmt.Errorf("upyun api error: failed to send request: %w", err)
|
||||
return resp, fmt.Errorf("sdkerr: 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.String())
|
||||
return resp, fmt.Errorf("sdkerr: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result interface{}) error {
|
||||
resp, err := c.sendRequest(method, path, params)
|
||||
func (c *Client) doRequestWithResult(req *resty.Request, res apiResponse) (*resty.Response, error) {
|
||||
if req == nil {
|
||||
return nil, fmt.Errorf("sdkerr: nil request")
|
||||
}
|
||||
|
||||
resp, err := c.doRequest(req)
|
||||
if err != nil {
|
||||
if resp != nil {
|
||||
json.Unmarshal(resp.Body(), &result)
|
||||
json.Unmarshal(resp.Body(), &res)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
if len(resp.Body()) != 0 {
|
||||
if err := json.Unmarshal(resp.Body(), &res); err != nil {
|
||||
return resp, fmt.Errorf("sdkerr: failed to unmarshal response: %w", err)
|
||||
} else {
|
||||
if tdata := res.GetData(); tdata == nil {
|
||||
return resp, fmt.Errorf("sdkerr: empty data")
|
||||
} else if terrcode := tdata.GetErrorCode(); terrcode != 0 {
|
||||
return resp, fmt.Errorf("sdkerr: code='%d', message='%s'", terrcode, tdata.GetMessage())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) ensureCookieExists() error {
|
||||
c.loginCookieMtx.Lock()
|
||||
defer c.loginCookieMtx.Unlock()
|
||||
if c.loginCookie != "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/accounts/signin/")
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
httpreq.SetBody(map[string]string{
|
||||
"username": c.username,
|
||||
"password": c.password,
|
||||
})
|
||||
}
|
||||
|
||||
if err := json.Unmarshal(resp.Body(), &result); err != nil {
|
||||
return fmt.Errorf("upyun api error: failed to unmarshal response: %w", err)
|
||||
type signinResponse struct {
|
||||
apiResponseBase
|
||||
Data *struct {
|
||||
apiResponseBaseData
|
||||
Result bool `json:"result"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
tresp := &baseResponse{}
|
||||
if err := json.Unmarshal(resp.Body(), &tresp); err != nil {
|
||||
return fmt.Errorf("upyun api error: failed to unmarshal 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: code='%d', message='%s'", errcode, tdata.GetErrorMessage())
|
||||
result := &signinResponse{}
|
||||
httpresp, err := c.doRequestWithResult(httpreq, result)
|
||||
if err != nil {
|
||||
return err
|
||||
} else if !result.Data.Result {
|
||||
return errors.New("sdkerr: failed to signin upyun console")
|
||||
} else {
|
||||
c.loginCookie = httpresp.Header().Get("Set-Cookie")
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user