refactor: re-impl sdk3rd
This commit is contained in:
@@ -1,16 +0,0 @@
|
||||
package bunny
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
func (c *Client) AddCustomCertificate(req *AddCustomCertificateRequest) ([]byte, error) {
|
||||
if req.PullZoneId == "" {
|
||||
return nil, fmt.Errorf("bunny api error: invalid parameter: PullZoneId")
|
||||
}
|
||||
|
||||
resp, err := c.sendRequest(http.MethodPost, fmt.Sprintf("/pullzone/%s/addCertificate", url.PathEscape(req.PullZoneId)), req)
|
||||
return resp.Body(), err
|
||||
}
|
||||
38
internal/pkg/sdk3rd/bunny/api_add_custom_certificate.go
Normal file
38
internal/pkg/sdk3rd/bunny/api_add_custom_certificate.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package bunny
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
)
|
||||
|
||||
type AddCustomCertificateRequest struct {
|
||||
Hostname string `json:"Hostname"`
|
||||
Certificate string `json:"Certificate"`
|
||||
CertificateKey string `json:"CertificateKey"`
|
||||
}
|
||||
|
||||
func (c *Client) AddCustomCertificate(pullZoneId string, req *AddCustomCertificateRequest) error {
|
||||
return c.AddCustomCertificateWithContext(context.Background(), pullZoneId, req)
|
||||
}
|
||||
|
||||
func (c *Client) AddCustomCertificateWithContext(ctx context.Context, pullZoneId string, req *AddCustomCertificateRequest) error {
|
||||
if pullZoneId == "" {
|
||||
return fmt.Errorf("sdkerr: unset pullZoneId")
|
||||
}
|
||||
|
||||
httpreq, err := c.newRequest(http.MethodPost, fmt.Sprintf("/pullzone/%s/addCertificate", url.PathEscape(pullZoneId)))
|
||||
if err != nil {
|
||||
return err
|
||||
} else {
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
if _, err := c.doRequest(httpreq); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -1,10 +1,7 @@
|
||||
package bunny
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
@@ -14,47 +11,53 @@ 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.bunny.net").
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("User-Agent", "certimate").
|
||||
SetHeader("AccessKey", 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) 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("bunny 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("bunny 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
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
package bunny
|
||||
|
||||
type AddCustomCertificateRequest struct {
|
||||
Hostname string `json:"Hostname"`
|
||||
PullZoneId string `json:"-"`
|
||||
Certificate string `json:"Certificate"`
|
||||
CertificateKey string `json:"CertificateKey"`
|
||||
}
|
||||
Reference in New Issue
Block a user