mirror of
https://github.com/certimate-go/certimate.git
synced 2026-09-21 12:43:46 +08:00
101 lines
2.3 KiB
Go
101 lines
2.3 KiB
Go
// A simple SDK client for AxisNow.
|
|
// API documentation: https://developers.axisnow.io/api-reference/main/client/v1/#/
|
|
package axisnow
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/go-resty/resty/v2"
|
|
|
|
"github.com/certimate-go/certimate/internal/app"
|
|
)
|
|
|
|
type Client struct {
|
|
rc *resty.Client
|
|
}
|
|
|
|
func NewClient(optFns ...OptionsFunc) (*Client, error) {
|
|
opts := &Options{}
|
|
for _, fn := range optFns {
|
|
fn(opts)
|
|
}
|
|
|
|
if opts.ApiToken == "" {
|
|
return nil, fmt.Errorf("sdkerr: unset apiToken")
|
|
}
|
|
|
|
httper := resty.New().
|
|
SetBaseURL("https://api.axisnow.io/client/v1").
|
|
SetHeader("Accept", "application/json").
|
|
SetHeader("Authorization", "Bearer "+opts.ApiToken).
|
|
SetHeader("Content-Type", "application/json").
|
|
SetHeader("User-Agent", app.AppUserAgent)
|
|
|
|
return &Client{rc: httper}, nil
|
|
}
|
|
|
|
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
|
c.rc.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.rc.R()
|
|
req.Method = method
|
|
req.URL = path
|
|
|
|
// WARN:
|
|
// DO NOT CALL `req.SetResult` or `req.SetError` AGAIN! USE `doRequestWithResult` INSTEAD.
|
|
return req, nil
|
|
}
|
|
|
|
func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
|
|
if req == nil {
|
|
return nil, fmt.Errorf("sdkerr: nil request")
|
|
}
|
|
|
|
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 sdkResponse) (*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 (resp: %s)", err, resp.String())
|
|
} else {
|
|
if err := res.GetAPIError(); err != nil {
|
|
return resp, fmt.Errorf("sdkerr: api error: %s", err.Error())
|
|
}
|
|
}
|
|
}
|
|
|
|
return resp, nil
|
|
}
|