refactor: re-impl sdk3rd

This commit is contained in:
Fu Diwei
2025-06-17 10:39:39 +08:00
parent 9421da2cde
commit 299a722aa9
280 changed files with 6923 additions and 4658 deletions

View File

@@ -1,7 +0,0 @@
package safeline
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
resp := &UpdateCertificateResponse{}
err := c.sendRequestWithResult("/api/open/cert", req, resp)
return resp, err
}

View File

@@ -0,0 +1,37 @@
package safeline
import (
"context"
"net/http"
)
type UpdateCertificateRequest struct {
Id int32 `json:"id"`
Type int32 `json:"type"`
Manual *CertificateManul `json:"manual"`
}
type UpdateCertificateResponse struct {
apiResponseBase
}
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
return c.UpdateCertificateWithContext(context.Background(), req)
}
func (c *Client) UpdateCertificateWithContext(ctx context.Context, req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/api/open/cert")
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &UpdateCertificateResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -4,6 +4,7 @@ import (
"crypto/tls"
"encoding/json"
"fmt"
"net/url"
"strings"
"time"
@@ -14,58 +15,91 @@ type Client struct {
client *resty.Client
}
func NewClient(serverUrl, apiToken string) *Client {
func NewClient(serverUrl, apiToken string) (*Client, error) {
if serverUrl == "" {
return nil, fmt.Errorf("sdkerr: unset serverUrl")
}
if _, err := url.Parse(serverUrl); err != nil {
return nil, fmt.Errorf("sdkerr: invalid serverUrl: %w", err)
}
if apiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
}
client := resty.New().
SetBaseURL(strings.TrimRight(serverUrl, "/")).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate").
SetHeader("X-SLCE-API-TOKEN", apiToken)
return &Client{
client: client,
}
return &Client{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) WithTLSConfig(config *tls.Config) *Client {
func (c *Client) SetTLSConfig(config *tls.Config) *Client {
c.client.SetTLSClientConfig(config)
return c
}
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
req := c.client.R().SetBody(params)
resp, err := req.Post(path)
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("safeline 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("safeline 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(path string, params interface{}, result BaseResponse) error {
resp, err := c.sendRequest(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 err
return resp, err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("safeline api error: failed to unmarshal response: %w", err)
} else if errcode := result.GetErrCode(); errcode != nil && *errcode != "" {
if result.GetErrMsg() == nil {
return fmt.Errorf("safeline api error: code='%s'", *errcode)
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 {
return fmt.Errorf("safeline api error: code='%s', message='%s'", *errcode, *result.GetErrMsg())
if terrcode := res.GetErrCode(); terrcode != "" {
return resp, fmt.Errorf("sdkerr: err='%s', msg='%s'", terrcode, res.GetErrMsg())
}
}
}
return nil
return resp, nil
}

View File

@@ -1,34 +0,0 @@
package safeline
type BaseResponse interface {
GetErrCode() *string
GetErrMsg() *string
}
type baseResponse struct {
ErrCode *string `json:"err,omitempty"`
ErrMsg *string `json:"msg,omitempty"`
}
func (r *baseResponse) GetErrCode() *string {
return r.ErrCode
}
func (r *baseResponse) GetErrMsg() *string {
return r.ErrMsg
}
type UpdateCertificateRequest struct {
Id int32 `json:"id"`
Type int32 `json:"type"`
Manual *UpdateCertificateRequestBodyManul `json:"manual"`
}
type UpdateCertificateRequestBodyManul struct {
Crt string `json:"crt"`
Key string `json:"key"`
}
type UpdateCertificateResponse struct {
baseResponse
}

View File

@@ -0,0 +1,34 @@
package safeline
type apiResponse interface {
GetErrCode() string
GetErrMsg() string
}
type apiResponseBase struct {
ErrCode *string `json:"err,omitempty"`
ErrMsg *string `json:"msg,omitempty"`
}
func (r *apiResponseBase) GetErrCode() string {
if r.ErrCode == nil {
return ""
}
return *r.ErrCode
}
func (r *apiResponseBase) GetErrMsg() string {
if r.ErrMsg == nil {
return ""
}
return *r.ErrMsg
}
var _ apiResponse = (*apiResponseBase)(nil)
type CertificateManul struct {
Crt string `json:"crt"`
Key string `json:"key"`
}