mirror of
https://github.com/woodchen-ink/certimate.git
synced 2025-07-18 01:11:55 +08:00
92 lines
2.1 KiB
Go
92 lines
2.1 KiB
Go
package baishan
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
)
|
|
|
|
type Client struct {
|
|
client *resty.Client
|
|
}
|
|
|
|
func NewClient(apiToken string) (*Client, error) {
|
|
if apiToken == "" {
|
|
return nil, fmt.Errorf("sdkerr: unset apiToken")
|
|
}
|
|
|
|
client := resty.New().
|
|
SetBaseURL("https://cdn.api.baishan.com").
|
|
SetHeader("Accept", "application/json").
|
|
SetHeader("Content-Type", "application/json").
|
|
SetHeader("User-Agent", "certimate").
|
|
SetQueryParam("token", apiToken)
|
|
|
|
return &Client{client}, nil
|
|
}
|
|
|
|
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
|
c.client.SetTimeout(timeout)
|
|
return c
|
|
}
|
|
|
|
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")
|
|
}
|
|
|
|
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("sdkerr: failed to send request: %w", err)
|
|
} else if resp.IsError() {
|
|
return resp, fmt.Errorf("sdkerr: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
|
}
|
|
|
|
return resp, nil
|
|
}
|
|
|
|
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(), &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 tcode := res.GetCode(); tcode != 0 {
|
|
return resp, fmt.Errorf("sdkerr: code='%d', message='%s'", tcode, res.GetMessage())
|
|
}
|
|
}
|
|
}
|
|
|
|
return resp, nil
|
|
}
|