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,17 +0,0 @@
package netlify
import (
"fmt"
"net/http"
"net/url"
)
func (c *Client) ProvisionSiteTLSCertificate(siteId string, params *ProvisionSiteTLSCertificateParams) (*ProvisionSiteTLSCertificateResponse, error) {
if siteId == "" {
return nil, fmt.Errorf("netlify api error: invalid parameter: SiteId")
}
resp := &ProvisionSiteTLSCertificateResponse{}
err := c.sendRequestWithResult(http.MethodPost, fmt.Sprintf("/sites/%s/ssl", url.PathEscape(siteId)), params, nil, resp)
return resp, err
}

View File

@@ -0,0 +1,52 @@
package netlify
import (
"context"
"fmt"
"net/http"
"net/url"
)
type ProvisionSiteTLSCertificateParams struct {
Certificate string `json:"certificate"`
CACertificates string `json:"ca_certificates"`
Key string `json:"key"`
}
type ProvisionSiteTLSCertificateResponse struct {
apiResponseBase
Domains []string `json:"domains,omitempty"`
State string `json:"state,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}
func (c *Client) ProvisionSiteTLSCertificate(siteId string, req *ProvisionSiteTLSCertificateParams) (*ProvisionSiteTLSCertificateResponse, error) {
return c.ProvisionSiteTLSCertificateWithContext(context.Background(), siteId, req)
}
func (c *Client) ProvisionSiteTLSCertificateWithContext(ctx context.Context, siteId string, req *ProvisionSiteTLSCertificateParams) (*ProvisionSiteTLSCertificateResponse, error) {
if siteId == "" {
return nil, fmt.Errorf("sdkerr: unset siteId")
}
httpreq, err := c.newRequest(http.MethodPost, fmt.Sprintf("/sites/%s/ssl", url.PathEscape(siteId)))
if err != nil {
return nil, err
} else {
httpreq.SetQueryParams(map[string]string{
"certificate": req.Certificate,
"ca_certificates": req.CACertificates,
"key": req.Key,
})
httpreq.SetContext(ctx)
}
result := &ProvisionSiteTLSCertificateResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -3,8 +3,6 @@ package netlify
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
@@ -14,80 +12,80 @@ type Client struct {
client *resty.Client
}
func NewClient(apiToken string) *Client {
func NewClient(apiToken string) (*Client, error) {
if apiToken == "" {
return nil, fmt.Errorf("sdkerr: unset apiToken")
}
client := resty.New().
SetBaseURL("https://api.netlify.com/api/v1").
SetHeader("Accept", "application/json").
SetHeader("Authorization", "Bearer "+apiToken).
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate")
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) sendRequest(method string, path string, queryParams interface{}, payloadParams interface{}) (*resty.Response, error) {
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
}
if queryParams != nil {
qs := make(map[string]string)
temp := make(map[string]any)
jsonb, _ := json.Marshal(queryParams)
json.Unmarshal(jsonb, &temp)
for k, v := range temp {
if v != nil {
qs[k] = fmt.Sprintf("%v", v)
}
}
req = req.SetQueryParams(qs)
func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
if req == nil {
return nil, fmt.Errorf("sdkerr: nil request")
}
if strings.EqualFold(method, http.MethodGet) {
qs := make(map[string]string)
if payloadParams != nil {
temp := make(map[string]any)
jsonb, _ := json.Marshal(payloadParams)
json.Unmarshal(jsonb, &temp)
for k, v := range temp {
if v != nil {
qs[k] = fmt.Sprintf("%v", v)
}
}
}
// WARN:
// PLEASE DO NOT USE `req.SetResult` or `req.SetError` HERE! USE `doRequestWithResult` INSTEAD.
req = req.SetQueryParams(qs)
} else {
req = req.SetHeader("Content-Type", "application/json").SetBody(payloadParams)
}
resp, err := req.Execute(method, path)
resp, err := req.Send()
if err != nil {
return resp, fmt.Errorf("netlify 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("netlify 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, queryParams interface{}, payloadParams interface{}, result BaseResponse) error {
resp, err := c.sendRequest(method, path, queryParams, payloadParams)
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("netlify api error: failed to unmarshal response: %w", err)
} else if errcode := result.GetCode(); errcode != 0 {
return fmt.Errorf("netlify api error: code='%d', message='%s'", errcode, result.GetMessage())
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 nil
return resp, nil
}

View File

@@ -1,40 +0,0 @@
package netlify
type BaseResponse interface {
GetCode() int32
GetMessage() string
}
type baseResponse struct {
Code *int32 `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
}
func (r *baseResponse) GetCode() int32 {
if r.Code != nil {
return *r.Code
}
return 0
}
func (r *baseResponse) GetMessage() string {
if r.Message != nil {
return *r.Message
}
return ""
}
type ProvisionSiteTLSCertificateParams struct {
Certificate string `json:"certificate"`
CACertificates string `json:"ca_certificates"`
Key string `json:"key"`
}
type ProvisionSiteTLSCertificateResponse struct {
baseResponse
Domains []string `json:"domains,omitempty"`
State string `json:"state,omitempty"`
ExpiresAt string `json:"expires_at,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
}

View File

@@ -0,0 +1,29 @@
package netlify
type apiResponse interface {
GetCode() int32
GetMessage() string
}
type apiResponseBase struct {
Code *int32 `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
}
func (r *apiResponseBase) GetCode() int32 {
if r.Code == nil {
return 0
}
return *r.Code
}
func (r *apiResponseBase) GetMessage() string {
if r.Message == nil {
return ""
}
return *r.Message
}
var _ apiResponse = (*apiResponseBase)(nil)