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,42 @@
package gname
import (
"context"
"encoding/json"
"net/http"
)
type AddDomainResolutionRequest struct {
ZoneName *string `json:"ym,omitempty"`
RecordType *string `json:"lx,omitempty"`
RecordName *string `json:"zj,omitempty"`
RecordValue *string `json:"jlz,omitempty"`
MX *int32 `json:"mx,omitempty"`
TTL *int32 `json:"ttl,omitempty"`
}
type AddDomainResolutionResponse struct {
apiResponseBase
Data json.Number `json:"data"`
}
func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
return c.AddDomainResolutionWithContext(context.Background(), req)
}
func (c *Client) AddDomainResolutionWithContext(ctx context.Context, req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/api/resolution/add", req)
if err != nil {
return nil, err
} else {
httpreq.SetContext(ctx)
}
result := &AddDomainResolutionResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,35 @@
package gname
import (
"context"
"net/http"
)
type DeleteDomainResolutionRequest struct {
ZoneName *string `json:"ym,omitempty"`
RecordID *int64 `json:"jxid,omitempty"`
}
type DeleteDomainResolutionResponse struct {
apiResponseBase
}
func (c *Client) DeleteDomainResolution(req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
return c.DeleteDomainResolutionWithContext(context.Background(), req)
}
func (c *Client) DeleteDomainResolutionWithContext(ctx context.Context, req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/api/resolution/delete", req)
if err != nil {
return nil, err
} else {
httpreq.SetContext(ctx)
}
result := &DeleteDomainResolutionResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,41 @@
package gname
import (
"context"
"net/http"
)
type ListDomainResolutionRequest struct {
ZoneName *string `json:"ym,omitempty"`
Page *int32 `json:"page,omitempty"`
PageSize *int32 `json:"limit,omitempty"`
}
type ListDomainResolutionResponse struct {
apiResponseBase
Count int32 `json:"count"`
Data []*DomainResolutionRecordord `json:"data"`
Page int32 `json:"page"`
PageSize int32 `json:"pagesize"`
}
func (c *Client) ListDomainResolution(req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
return c.ListDomainResolutionWithContext(context.Background(), req)
}
func (c *Client) ListDomainResolutionWithContext(ctx context.Context, req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/api/resolution/list", req)
if err != nil {
return nil, err
} else {
httpreq.SetContext(ctx)
}
result := &ListDomainResolutionResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,40 @@
package gname
import (
"context"
"net/http"
)
type ModifyDomainResolutionRequest struct {
ID *int64 `json:"jxid,omitempty"`
ZoneName *string `json:"ym,omitempty"`
RecordType *string `json:"lx,omitempty"`
RecordName *string `json:"zj,omitempty"`
RecordValue *string `json:"jlz,omitempty"`
MX *int32 `json:"mx,omitempty"`
TTL *int32 `json:"ttl,omitempty"`
}
type ModifyDomainResolutionResponse struct {
apiResponseBase
}
func (c *Client) ModifyDomainResolution(req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
return c.ModifyDomainResolutionWithContext(context.Background(), req)
}
func (c *Client) ModifyDomainResolutionWithContext(ctx context.Context, req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/api/resolution/edit", req)
if err != nil {
return nil, err
} else {
httpreq.SetContext(ctx)
}
result := &ModifyDomainResolutionResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

148
pkg/sdk3rd/gname/client.go Normal file
View File

@@ -0,0 +1,148 @@
package gname
import (
"crypto/md5"
"encoding/json"
"fmt"
"net/url"
"sort"
"strings"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
appId string
appKey string
client *resty.Client
}
func NewClient(appId, appKey string) (*Client, error) {
if appId == "" {
return nil, fmt.Errorf("sdkerr: unset appId")
}
if appKey == "" {
return nil, fmt.Errorf("sdkerr: unset appKey")
}
client := resty.New().
SetBaseURL("http://api.gname.com").
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetHeader("User-Agent", "certimate")
return &Client{
appId: appId,
appKey: appKey,
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, params any) (*resty.Request, error) {
if method == "" {
return nil, fmt.Errorf("sdkerr: unset method")
}
if path == "" {
return nil, fmt.Errorf("sdkerr: unset path")
}
data := 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 {
continue
}
data[k] = fmt.Sprintf("%v", v)
}
}
data["appid"] = c.appId
data["gntime"] = fmt.Sprintf("%d", time.Now().Unix())
data["gntoken"] = generateSignature(data, c.appKey)
req := c.client.R()
req.Method = method
req.URL = path
req.SetFormData(data)
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.SetBody` or `req.SetFormData` HERE! USE `newRequest` INSTEAD.
// 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 != 1 {
return resp, fmt.Errorf("sdkerr: api error: code='%d', message='%s'", tcode, res.GetMessage())
}
}
}
return resp, nil
}
func generateSignature(params map[string]string, appKey string) string {
// Step 1: Sort parameters by ASCII order
var keys []string
for k := range params {
keys = append(keys, k)
}
sort.Strings(keys)
// Step 2: Create string A with URL-encoded values
var pairs []string
for _, k := range keys {
encodedValue := url.QueryEscape(params[k])
pairs = append(pairs, fmt.Sprintf("%s=%s", k, encodedValue))
}
stringA := strings.Join(pairs, "&")
// Step 3: Append appkey to create string B
stringB := stringA + appKey
// Step 4: Calculate MD5 and convert to uppercase
hash := md5.Sum([]byte(stringB))
return strings.ToUpper(fmt.Sprintf("%x", hash))
}

32
pkg/sdk3rd/gname/types.go Normal file
View File

@@ -0,0 +1,32 @@
package gname
import "encoding/json"
type apiResponse interface {
GetCode() int32
GetMessage() string
}
type apiResponseBase struct {
Code int32 `json:"code"`
Message string `json:"msg"`
}
func (r *apiResponseBase) GetCode() int32 {
return r.Code
}
func (r *apiResponseBase) GetMessage() string {
return r.Message
}
var _ apiResponse = (*apiResponseBase)(nil)
type DomainResolutionRecordord struct {
ID json.Number `json:"id"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zjt"`
RecordValue string `json:"jxz"`
MX int32 `json:"mx"`
}