refactor: optimize third-party sdks

This commit is contained in:
Fu Diwei
2025-05-16 11:10:43 +08:00
parent dcd646b465
commit f3bbb9e8b2
43 changed files with 215 additions and 437 deletions

View File

@@ -6,12 +6,8 @@ import (
"fmt"
"log/slog"
"github.com/go-openapi/runtime"
"github.com/go-openapi/strfmt"
"github.com/netlify/open-api/v2/go/porcelain"
porcelainctx "github.com/netlify/open-api/v2/go/porcelain/context"
"github.com/usual2970/certimate/internal/pkg/core/deployer"
netlifysdk "github.com/usual2970/certimate/internal/pkg/sdk3rd/netlify"
certutil "github.com/usual2970/certimate/internal/pkg/utils/cert"
)
@@ -23,10 +19,9 @@ type DeployerConfig struct {
}
type DeployerProvider struct {
config *DeployerConfig
logger *slog.Logger
sdkClient *porcelain.Netlify
sdkClientAuther runtime.ClientAuthInfoWriter
config *DeployerConfig
logger *slog.Logger
sdkClient *netlifysdk.Client
}
var _ deployer.Deployer = (*DeployerProvider)(nil)
@@ -36,16 +31,15 @@ func NewDeployer(config *DeployerConfig) (*DeployerProvider, error) {
panic("config is nil")
}
client, clientAuther, err := createSdkClient(config.ApiToken)
client, err := createSdkClient(config.ApiToken)
if err != nil {
return nil, fmt.Errorf("failed to create sdk client: %w", err)
}
return &DeployerProvider{
config: config,
logger: slog.Default(),
sdkClient: client,
sdkClientAuther: clientAuther,
config: config,
logger: slog.Default(),
sdkClient: client,
}, nil
}
@@ -71,14 +65,13 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPE
// 上传网站证书
// REF: https://open-api.netlify.com/#tag/sniCertificate/operation/provisionSiteTLSCertificate
configureSiteTLSCertificateCtx := porcelainctx.WithAuthInfo(context.TODO(), d.sdkClientAuther)
configureSiteTLSCertificateReq := &porcelain.CustomTLSCertificate{
provisionSiteTLSCertificateReq := &netlifysdk.ProvisionSiteTLSCertificateParams{
Certificate: serverCertPEM,
CACertificates: intermediaCertPEM,
Key: privkeyPEM,
}
configureSiteTLSCertificateResp, err := d.sdkClient.ConfigureSiteTLSCertificate(configureSiteTLSCertificateCtx, d.config.SiteId, configureSiteTLSCertificateReq)
d.logger.Debug("sdk request 'netlify.provisionSiteTLSCertificate'", slog.String("siteId", d.config.SiteId), slog.Any("request", configureSiteTLSCertificateReq), slog.Any("response", configureSiteTLSCertificateResp))
provisionSiteTLSCertificateResp, err := d.sdkClient.ProvisionSiteTLSCertificate(d.config.SiteId, provisionSiteTLSCertificateReq)
d.logger.Debug("sdk request 'netlify.provisionSiteTLSCertificate'", slog.String("siteId", d.config.SiteId), slog.Any("request", provisionSiteTLSCertificateReq), slog.Any("response", provisionSiteTLSCertificateResp))
if err != nil {
return nil, fmt.Errorf("failed to execute sdk request 'netlify.provisionSiteTLSCertificate': %w", err)
}
@@ -86,16 +79,11 @@ func (d *DeployerProvider) Deploy(ctx context.Context, certPEM string, privkeyPE
return &deployer.DeployResult{}, nil
}
func createSdkClient(apiToken string) (*porcelain.Netlify, runtime.ClientAuthInfoWriter, error) {
func createSdkClient(apiToken string) (*netlifysdk.Client, error) {
if apiToken == "" {
return nil, nil, errors.New("invalid netlify api token")
return nil, errors.New("invalid netlify api token")
}
creds := runtime.ClientAuthInfoWriterFunc(func(r runtime.ClientRequest, _ strfmt.Registry) error {
r.SetHeaderParam("User-Agent", "Certimate")
r.SetHeaderParam("Authorization", "Bearer "+apiToken)
return nil
})
return porcelain.Default, creds, nil
client := netlifysdk.NewClient(apiToken)
return client, nil
}

View File

@@ -1,4 +1,4 @@
package onepanelsdk
package onepanel
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package onepanelsdk
package onepanel
import (
"crypto/md5"
@@ -97,7 +97,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("1panel api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode/100 != 2 {
return fmt.Errorf("1panel api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("1panel api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package onepanelsdk
package onepanel
type BaseResponse interface {
GetCode() int32

View File

@@ -1,4 +1,4 @@
package baishansdk
package baishan
import (
"net/http"

View File

@@ -1,4 +1,4 @@
package baishansdk
package baishan
import (
"encoding/json"
@@ -93,7 +93,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("baishan api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != 0 {
return fmt.Errorf("baishan api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("baishan api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package baishansdk
package baishan
import "encoding/json"

View File

@@ -1,4 +1,4 @@
package btpanelsdk
package btpanel
func (c *Client) ConfigSavePanelSSL(req *ConfigSavePanelSSLRequest) (*ConfigSavePanelSSLResponse, error) {
resp := &ConfigSavePanelSSLResponse{}

View File

@@ -1,4 +1,4 @@
package btpanelsdk
package btpanel
import (
"crypto/md5"
@@ -104,7 +104,7 @@ func (c *Client) sendRequestWithResult(path string, params interface{}, result B
if result.GetMessage() == nil {
return fmt.Errorf("baota api error: unknown error")
} else {
return fmt.Errorf("baota api error: %s", *result.GetMessage())
return fmt.Errorf("baota api error: message='%s'", *result.GetMessage())
}
}

View File

@@ -1,4 +1,4 @@
package btpanelsdk
package btpanel
type BaseResponse interface {
GetStatus() *bool

View File

@@ -1,4 +1,4 @@
package bunnysdk
package bunny
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package bunnysdk
package bunny
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package bunnysdk
package bunny
type AddCustomCertificateRequest struct {
Hostname string `json:"Hostname"`

View File

@@ -1,4 +1,4 @@
package cacheflysdk
package cachefly
import (
"net/http"

View File

@@ -1,4 +1,4 @@
package cacheflysdk
package cachefly
import (
"encoding/json"

View File

@@ -1,4 +1,4 @@
package cacheflysdk
package cachefly
type BaseResponse interface {
GetMessage() string

View File

@@ -1,4 +1,4 @@
package cdnflysdk
package cdnfly
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package cdnflysdk
package cdnfly
import (
"crypto/tls"
@@ -89,7 +89,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("cdnfly api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != "" && errcode != "0" {
return fmt.Errorf("cdnfly api error: %s - %s", errcode, result.GetMessage())
return fmt.Errorf("cdnfly api error: code='%s', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package cdnflysdk
package cdnfly
import "fmt"

View File

@@ -1,4 +1,4 @@
package dnslasdk
package dnsla
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package dnslasdk
package dnsla
import (
"encoding/json"
@@ -78,7 +78,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("dnsla api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode/100 != 2 {
return fmt.Errorf("dnsla api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("dnsla api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package dnslasdk
package dnsla
type BaseResponse interface {
GetCode() int32

View File

@@ -1,4 +1,4 @@
package dogecloudsdk
package dogecloud
import (
"crypto/hmac"

View File

@@ -1,4 +1,4 @@
package dogecloudsdk
package dogecloud
type BaseResponse struct {
Code *int `json:"code,omitempty"`

View File

@@ -1,4 +1,4 @@
package gnamesdk
package gname
func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
resp := &AddDomainResolutionResponse{}

View File

@@ -1,4 +1,4 @@
package gnamesdk
package gname
import (
"crypto/md5"
@@ -97,7 +97,7 @@ func (c *Client) sendRequestWithResult(path string, params interface{}, result B
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("gname api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != 1 {
return fmt.Errorf("gname api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("gname api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package gnamesdk
package gname
import "encoding/json"

View File

@@ -96,7 +96,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("goedge api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != 200 {
return fmt.Errorf("goedge api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("goedge api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -0,0 +1,17 @@
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,97 @@
package netlify
import (
"encoding/json"
"fmt"
"net/http"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
apiToken string
client *resty.Client
}
func NewClient(apiToken string) *Client {
client := resty.New()
return &Client{
apiToken: apiToken,
client: client,
}
}
func (c *Client) WithTimeout(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) {
req := c.client.R().SetHeader("Authorization", "Bearer "+c.apiToken)
req.Method = method
req.URL = "https://api.netlify.com/api/v1" + path
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)
}
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)
}
}
}
req = req.SetQueryParams(qs)
} else {
req = req.
SetHeader("Content-Type", "application/json").
SetBody(payloadParams)
}
resp, err := req.Send()
if err != nil {
return resp, fmt.Errorf("netlify api error: 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.Body())
}
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)
if err != nil {
if resp != nil {
json.Unmarshal(resp.Body(), &result)
}
return err
}
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("netlify api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode != 0 {
return fmt.Errorf("netlify api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil
}

View File

@@ -0,0 +1,40 @@
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:"key"`
Key string `json:"ca_certificates"`
}
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

@@ -1,4 +1,4 @@
package qiniusdk
package qiniu
import (
"net/http"

View File

@@ -1,4 +1,4 @@
package qiniusdk
package qiniu
import (
"context"

View File

@@ -1,4 +1,4 @@
package qiniusdk
package qiniu
type BaseResponse struct {
Code *int `json:"code,omitempty"`

View File

@@ -1,4 +1,4 @@
package rainyunsdk
package rainyun
import (
"fmt"

View File

@@ -1,4 +1,4 @@
package rainyunsdk
package rainyun
import (
"encoding/json"
@@ -67,7 +67,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
if err := json.Unmarshal(resp.Body(), &result); err != nil {
return fmt.Errorf("rainyun api error: failed to parse response: %w", err)
} else if errcode := result.GetCode(); errcode/100 != 2 {
return fmt.Errorf("rainyun api error: %d - %s", errcode, result.GetMessage())
return fmt.Errorf("rainyun api error: code='%d', message='%s'", errcode, result.GetMessage())
}
return nil

View File

@@ -1,4 +1,4 @@
package rainyunsdk
package rainyun
type BaseResponse interface {
GetCode() int32

View File

@@ -1,4 +1,4 @@
package safelinesdk
package safeline
func (c *Client) UpdateCertificate(req *UpdateCertificateRequest) (*UpdateCertificateResponse, error) {
resp := &UpdateCertificateResponse{}

View File

@@ -1,4 +1,4 @@
package safelinesdk
package safeline
import (
"crypto/tls"
@@ -66,9 +66,9 @@ func (c *Client) sendRequestWithResult(path string, params interface{}, result B
return fmt.Errorf("safeline api error: failed to parse response: %w", err)
} else if errcode := result.GetErrCode(); errcode != nil && *errcode != "" {
if result.GetErrMsg() == nil {
return fmt.Errorf("safeline api error: %s", *errcode)
return fmt.Errorf("safeline api error: code='%s'", *errcode)
} else {
return fmt.Errorf("safeline api error: %s - %s", *errcode, *result.GetErrMsg())
return fmt.Errorf("safeline api error: code='%s', message='%s'", *errcode, *result.GetErrMsg())
}
}

View File

@@ -1,4 +1,4 @@
package safelinesdk
package safeline
type BaseResponse interface {
GetErrCode() *string

View File

@@ -90,7 +90,7 @@ func (c *Client) sendRequestWithResult(method string, path string, params interf
} else if tdata := tresp.GetData(); tdata == nil {
return fmt.Errorf("upyun api error: empty data")
} else if errcode := tdata.GetErrorCode(); errcode > 0 {
return fmt.Errorf("upyun api error: %d - %s", errcode, tdata.GetErrorMessage())
return fmt.Errorf("upyun api error: code='%d', message='%s'", errcode, tdata.GetErrorMessage())
}
return nil