refactor: reimpl 3rd sdks
This commit is contained in:
41
internal/pkg/vendors/cdnfly-sdk/api.go
vendored
41
internal/pkg/vendors/cdnfly-sdk/api.go
vendored
@@ -1,59 +1,42 @@
|
||||
package cdnflysdk
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
func (c *Client) GetSite(req *GetSiteRequest) (*GetSiteResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := GetSiteResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodGet, fmt.Sprintf("/v1/sites/%s", req.Id), params, &result)
|
||||
resp := GetSiteResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodGet, fmt.Sprintf("/v1/sites/%s", req.Id), req, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateSite(req *UpdateSiteRequest) (*UpdateSiteResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := UpdateSiteResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/sites/%s", req.Id), params, &result)
|
||||
resp := UpdateSiteResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/sites/%s", req.Id), req, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) CreateCertificate(req *CreateCertificateRequest) (*CreateCertificateResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := CreateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPost, "/v1/certs", params, &result)
|
||||
resp := CreateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPost, "/v1/certs", req, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
|
||||
params := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(req)
|
||||
json.Unmarshal(jsonData, ¶ms)
|
||||
|
||||
result := UpdateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/certs/%s", req.Id), params, &result)
|
||||
resp := UpdateCertificateResponse{}
|
||||
err := c.sendRequestWithResult(http.MethodPut, fmt.Sprintf("/v1/certs/%s", req.Id), req, &resp)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return &result, nil
|
||||
return &resp, nil
|
||||
}
|
||||
|
||||
18
internal/pkg/vendors/cdnfly-sdk/client.go
vendored
18
internal/pkg/vendors/cdnfly-sdk/client.go
vendored
@@ -34,7 +34,7 @@ func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) sendRequest(method string, path string, params map[string]any) (*resty.Response, error) {
|
||||
func (c *Client) sendRequest(method string, path string, params interface{}) (*resty.Response, error) {
|
||||
req := c.client.R()
|
||||
req.Method = method
|
||||
req.URL = c.apiHost + path
|
||||
@@ -42,11 +42,17 @@ func (c *Client) sendRequest(method string, path string, params map[string]any)
|
||||
SetHeader("api-key", c.apiKey).
|
||||
SetHeader("api-secret", c.apiSecret)
|
||||
if strings.EqualFold(method, http.MethodGet) {
|
||||
data := make(map[string]string)
|
||||
for k, v := range params {
|
||||
data[k] = fmt.Sprintf("%v", v)
|
||||
qs := make(map[string]string)
|
||||
if params != nil {
|
||||
temp := make(map[string]any)
|
||||
jsonData, _ := json.Marshal(params)
|
||||
json.Unmarshal(jsonData, &temp)
|
||||
for k, v := range temp {
|
||||
qs[k] = fmt.Sprintf("%v", v)
|
||||
}
|
||||
}
|
||||
req = req.SetQueryParams(data)
|
||||
|
||||
req = req.SetQueryParams(qs)
|
||||
} else {
|
||||
req = req.
|
||||
SetHeader("Content-Type", "application/json").
|
||||
@@ -63,7 +69,7 @@ func (c *Client) sendRequest(method string, path string, params map[string]any)
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) sendRequestWithResult(method string, path string, params map[string]any, result BaseResponse) error {
|
||||
func (c *Client) sendRequestWithResult(method string, path string, params interface{}, result BaseResponse) error {
|
||||
resp, err := c.sendRequest(method, path, params)
|
||||
if err != nil {
|
||||
return err
|
||||
|
||||
Reference in New Issue
Block a user