chore: move '/internal/pkg' to '/pkg'
This commit is contained in:
49
pkg/sdk3rd/baishan/api_get_domain_config.go
Normal file
49
pkg/sdk3rd/baishan/api_get_domain_config.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package baishan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type GetDomainConfigRequest struct {
|
||||
Domains *string `json:"domains,omitempty"`
|
||||
Config *[]string `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
type GetDomainConfigResponse struct {
|
||||
apiResponseBase
|
||||
|
||||
Data []*struct {
|
||||
Domain string `json:"domain"`
|
||||
Config *DomainConfig `json:"config"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) GetDomainConfig(req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
|
||||
return c.GetDomainConfigWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) GetDomainConfigWithContext(ctx context.Context, req *GetDomainConfigRequest) (*GetDomainConfigResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodGet, "/v2/domain/config")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
if req.Domains != nil {
|
||||
httpreq.SetQueryParam("domains", *req.Domains)
|
||||
}
|
||||
if req.Config != nil {
|
||||
for _, config := range *req.Config {
|
||||
httpreq.QueryParam.Add("config[]", config)
|
||||
}
|
||||
}
|
||||
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &GetDomainConfigResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
40
pkg/sdk3rd/baishan/api_set_domain_certificate.go
Normal file
40
pkg/sdk3rd/baishan/api_set_domain_certificate.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package baishan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type SetDomainCertificateRequest struct {
|
||||
CertificateId *string `json:"cert_id,omitempty"`
|
||||
Certificate *string `json:"certificate,omitempty"`
|
||||
Key *string `json:"key,omitempty"`
|
||||
Name *string `json:"name,omitempty"`
|
||||
}
|
||||
|
||||
type SetDomainCertificateResponse struct {
|
||||
apiResponseBase
|
||||
|
||||
Data *DomainCertificate `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) SetDomainCertificate(req *SetDomainCertificateRequest) (*SetDomainCertificateResponse, error) {
|
||||
return c.SetDomainCertificateWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) SetDomainCertificateWithContext(ctx context.Context, req *SetDomainCertificateRequest) (*SetDomainCertificateResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v2/domain/certificate")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &SetDomainCertificateResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
40
pkg/sdk3rd/baishan/api_set_domain_config.go
Normal file
40
pkg/sdk3rd/baishan/api_set_domain_config.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package baishan
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type SetDomainConfigRequest struct {
|
||||
Domains *string `json:"domains,omitempty"`
|
||||
Config *DomainConfig `json:"config,omitempty"`
|
||||
}
|
||||
|
||||
type SetDomainConfigResponse struct {
|
||||
apiResponseBase
|
||||
|
||||
Data *struct {
|
||||
Config *DomainConfig `json:"config"`
|
||||
} `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
func (c *Client) SetDomainConfig(req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
|
||||
return c.SetDomainConfigWithContext(context.Background(), req)
|
||||
}
|
||||
|
||||
func (c *Client) SetDomainConfigWithContext(ctx context.Context, req *SetDomainConfigRequest) (*SetDomainConfigResponse, error) {
|
||||
httpreq, err := c.newRequest(http.MethodPost, "/v2/domain/config")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
} else {
|
||||
httpreq.SetBody(req)
|
||||
httpreq.SetContext(ctx)
|
||||
}
|
||||
|
||||
result := &SetDomainConfigResponse{}
|
||||
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
|
||||
return result, err
|
||||
}
|
||||
|
||||
return result, nil
|
||||
}
|
||||
91
pkg/sdk3rd/baishan/client.go
Normal file
91
pkg/sdk3rd/baishan/client.go
Normal file
@@ -0,0 +1,91 @@
|
||||
package baishan
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
"github.com/go-resty/resty/v2"
|
||||
)
|
||||
|
||||
type Client struct {
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(apiToken string) (*Client, error) {
|
||||
if apiToken == "" {
|
||||
return nil, fmt.Errorf("sdkerr: unset apiToken")
|
||||
}
|
||||
|
||||
client := resty.New().
|
||||
SetBaseURL("https://cdn.api.baishan.com").
|
||||
SetHeader("Accept", "application/json").
|
||||
SetHeader("Content-Type", "application/json").
|
||||
SetHeader("User-Agent", "certimate").
|
||||
SetHeader("Token", apiToken)
|
||||
|
||||
return &Client{client}, nil
|
||||
}
|
||||
|
||||
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
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("sdkerr: failed to send request: %w", err)
|
||||
} else if resp.IsError() {
|
||||
return resp, fmt.Errorf("sdkerr: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.String())
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
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(), &res)
|
||||
}
|
||||
return resp, err
|
||||
}
|
||||
|
||||
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 resp, nil
|
||||
}
|
||||
49
pkg/sdk3rd/baishan/types.go
Normal file
49
pkg/sdk3rd/baishan/types.go
Normal file
@@ -0,0 +1,49 @@
|
||||
package baishan
|
||||
|
||||
import "encoding/json"
|
||||
|
||||
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)
|
||||
|
||||
type DomainCertificate struct {
|
||||
CertId json.Number `json:"cert_id"`
|
||||
Name string `json:"name"`
|
||||
CertStartTime string `json:"cert_start_time"`
|
||||
CertExpireTime string `json:"cert_expire_time"`
|
||||
}
|
||||
|
||||
type DomainConfig struct {
|
||||
Https *DomainConfigHttps `json:"https"`
|
||||
}
|
||||
|
||||
type DomainConfigHttps struct {
|
||||
CertId json.Number `json:"cert_id"`
|
||||
ForceHttps *string `json:"force_https,omitempty"`
|
||||
EnableHttp2 *string `json:"http2,omitempty"`
|
||||
EnableOcsp *string `json:"ocsp,omitempty"`
|
||||
}
|
||||
Reference in New Issue
Block a user