chore: move '/internal/pkg' to '/pkg'

This commit is contained in:
Fu Diwei
2025-06-17 15:54:21 +08:00
parent 30840bbba5
commit 205275b52d
611 changed files with 693 additions and 693 deletions

View File

@@ -0,0 +1,44 @@
package cms
import (
"context"
"net/http"
)
type GetCertificateListRequest struct {
Status *string `json:"status,omitempty"`
Keyword *string `json:"keyword,omitempty"`
PageNum *int32 `json:"pageNum,omitempty"`
PageSize *int32 `json:"pageSize,omitempty"`
Origin *string `json:"origin,omitempty"`
}
type GetCertificateListResponse struct {
apiResponseBase
ReturnObj *struct {
List []*CertificateRecord `json:"list,omitempty"`
TotalSize int32 `json:"totalSize,omitempty"`
} `json:"returnObj,omitempty"`
}
func (c *Client) GetCertificateList(req *GetCertificateListRequest) (*GetCertificateListResponse, error) {
return c.GetCertificateListWithContext(context.Background(), req)
}
func (c *Client) GetCertificateListWithContext(ctx context.Context, req *GetCertificateListRequest) (*GetCertificateListResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/v1/certificate/list")
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &GetCertificateListResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,41 @@
package cms
import (
"context"
"net/http"
)
type UploadCertificateRequest struct {
Name *string `json:"name,omitempty"`
Certificate *string `json:"certificate,omitempty"`
CertificateChain *string `json:"certificateChain,omitempty"`
PrivateKey *string `json:"privateKey,omitempty"`
EncryptionStandard *string `json:"encryptionStandard,omitempty"`
EncCertificate *string `json:"encCertificate,omitempty"`
EncPrivateKey *string `json:"encPrivateKey,omitempty"`
}
type UploadCertificateResponse struct {
apiResponseBase
}
func (c *Client) UploadCertificate(req *UploadCertificateRequest) (*UploadCertificateResponse, error) {
return c.UploadCertificateWithContext(context.Background(), req)
}
func (c *Client) UploadCertificateWithContext(ctx context.Context, req *UploadCertificateRequest) (*UploadCertificateResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/v1/certificate/upload")
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &UploadCertificateResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,50 @@
package cms
import (
"fmt"
"time"
"github.com/go-resty/resty/v2"
"github.com/usual2970/certimate/pkg/sdk3rd/ctyun/openapi"
)
const endpoint = "https://ccms-global.ctapi.ctyun.cn"
type Client struct {
client *openapi.Client
}
func NewClient(accessKeyId, secretAccessKey string) (*Client, error) {
client, err := openapi.NewClient(endpoint, accessKeyId, secretAccessKey)
if err != nil {
return nil, err
}
return &Client{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) {
return c.client.NewRequest(method, path)
}
func (c *Client) doRequest(req *resty.Request) (*resty.Response, error) {
return c.client.DoRequest(req)
}
func (c *Client) doRequestWithResult(req *resty.Request, res apiResponse) (*resty.Response, error) {
resp, err := c.client.DoRequestWithResult(req, res)
if err == nil {
statusCode := res.GetStatusCode()
errorCode := res.GetError()
if (statusCode != "" && statusCode != "200") || errorCode != "" {
return resp, fmt.Errorf("sdkerr: api error, code='%s', message='%s', errorCode='%s', errorMessage='%s'", statusCode, res.GetMessage(), res.GetMessage(), res.GetErrorMessage())
}
}
return resp, err
}

View File

@@ -0,0 +1,94 @@
package cms
import (
"bytes"
"encoding/json"
"strconv"
)
type apiResponse interface {
GetStatusCode() string
GetMessage() string
GetError() string
GetErrorMessage() string
}
type apiResponseBase struct {
StatusCode json.RawMessage `json:"statusCode,omitempty"`
Message *string `json:"message,omitempty"`
Error *string `json:"error,omitempty"`
ErrorMessage *string `json:"errorMessage,omitempty"`
RequestId *string `json:"requestId,omitempty"`
}
func (r *apiResponseBase) GetStatusCode() string {
if r.StatusCode == nil {
return ""
}
decoder := json.NewDecoder(bytes.NewReader(r.StatusCode))
token, err := decoder.Token()
if err != nil {
return ""
}
switch t := token.(type) {
case string:
return t
case float64:
return strconv.FormatFloat(t, 'f', -1, 64)
case json.Number:
return t.String()
default:
return ""
}
}
func (r *apiResponseBase) GetMessage() string {
if r.Message == nil {
return ""
}
return *r.Message
}
func (r *apiResponseBase) GetError() string {
if r.Error == nil {
return ""
}
return *r.Error
}
func (r *apiResponseBase) GetErrorMessage() string {
if r.ErrorMessage == nil {
return ""
}
return *r.ErrorMessage
}
var _ apiResponse = (*apiResponseBase)(nil)
type CertificateRecord struct {
Id string `json:"id"`
Origin string `json:"origin"`
Type string `json:"type"`
ResourceId string `json:"resourceId"`
ResourceType string `json:"resourceType"`
CertificateId string `json:"certificateId"`
CertificateMode string `json:"certificateMode"`
Name string `json:"name"`
Status string `json:"status"`
DetailStatus string `json:"detailStatus"`
ManagedStatus string `json:"managedStatus"`
Fingerprint string `json:"fingerprint"`
IssueTime string `json:"issueTime"`
ExpireTime string `json:"expireTime"`
DomainType string `json:"domainType"`
DomainName string `json:"domainName"`
EncryptionStandard string `json:"encryptionStandard"`
EncryptionAlgorithm string `json:"encryptionAlgorithm"`
CreateTime string `json:"createTime"`
UpdateTime string `json:"updateTime"`
}