refactor: clean code

This commit is contained in:
Fu Diwei
2025-04-22 21:18:16 +08:00
parent 8fe942d8d5
commit 3189e65bad
243 changed files with 545 additions and 507 deletions

View File

@@ -0,0 +1,25 @@
package gnamesdk
func (c *Client) AddDomainResolution(req *AddDomainResolutionRequest) (*AddDomainResolutionResponse, error) {
resp := &AddDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/add", req, resp)
return resp, err
}
func (c *Client) ModifyDomainResolution(req *ModifyDomainResolutionRequest) (*ModifyDomainResolutionResponse, error) {
resp := &ModifyDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/edit", req, resp)
return resp, err
}
func (c *Client) DeleteDomainResolution(req *DeleteDomainResolutionRequest) (*DeleteDomainResolutionResponse, error) {
resp := &DeleteDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/delete", req, resp)
return resp, err
}
func (c *Client) ListDomainResolution(req *ListDomainResolutionRequest) (*ListDomainResolutionResponse, error) {
resp := &ListDomainResolutionResponse{}
err := c.sendRequestWithResult("/api/resolution/list", req, resp)
return resp, err
}

View File

@@ -0,0 +1,104 @@
package gnamesdk
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 {
client := resty.New()
return &Client{
appId: appId,
appKey: appKey,
client: client,
}
}
func (c *Client) WithTimeout(timeout time.Duration) *Client {
c.client.SetTimeout(timeout)
return c
}
func (c *Client) generateSignature(params map[string]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 + c.appKey
// Step 4: Calculate MD5 and convert to uppercase
hash := md5.Sum([]byte(stringB))
return strings.ToUpper(fmt.Sprintf("%x", hash))
}
func (c *Client) sendRequest(path string, params interface{}) (*resty.Response, error) {
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 {
data[k] = fmt.Sprintf("%v", v)
}
}
}
data["appid"] = c.appId
data["gntime"] = fmt.Sprintf("%d", time.Now().Unix())
data["gntoken"] = c.generateSignature(data)
url := "http://api.gname.com" + path
req := c.client.R().
SetHeader("Content-Type", "application/x-www-form-urlencoded").
SetFormData(data)
resp, err := req.Post(url)
if err != nil {
return resp, fmt.Errorf("gname api error: failed to send request: %w", err)
} else if resp.IsError() {
return resp, fmt.Errorf("gname api error: unexpected status code: %d, resp: %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(path string, params interface{}, result BaseResponse) error {
resp, err := c.sendRequest(path, params)
if err != nil {
return err
}
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 nil
}

View File

@@ -0,0 +1,81 @@
package gnamesdk
import "encoding/json"
type BaseResponse interface {
GetCode() int32
GetMessage() string
}
type baseResponse struct {
Code int32 `json:"code"`
Message string `json:"msg"`
}
func (r *baseResponse) GetCode() int32 {
return r.Code
}
func (r *baseResponse) GetMessage() string {
return r.Message
}
type AddDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int32 `json:"mx"`
TTL int32 `json:"ttl"`
}
type AddDomainResolutionResponse struct {
baseResponse
Data json.Number `json:"data"`
}
type ModifyDomainResolutionRequest struct {
ID int64 `json:"jxid"`
ZoneName string `json:"ym"`
RecordType string `json:"lx"`
RecordName string `json:"zj"`
RecordValue string `json:"jlz"`
MX int32 `json:"mx"`
TTL int32 `json:"ttl"`
}
type ModifyDomainResolutionResponse struct {
baseResponse
}
type DeleteDomainResolutionRequest struct {
ZoneName string `json:"ym"`
RecordID int64 `json:"jxid"`
}
type DeleteDomainResolutionResponse struct {
baseResponse
}
type ListDomainResolutionRequest struct {
ZoneName string `json:"ym"`
Page *int32 `json:"page,omitempty"`
PageSize *int32 `json:"limit,omitempty"`
}
type ListDomainResolutionResponse struct {
baseResponse
Count int32 `json:"count"`
Data []*ResolutionRecord `json:"data"`
Page int32 `json:"page"`
PageSize int32 `json:"pagesize"`
}
type ResolutionRecord 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"`
}