2025-02-18 15:16:24 +08:00

101 lines
2.3 KiB
Go

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 map[string]any) (*resty.Response, error) {
if params == nil {
params = make(map[string]any)
}
data := make(map[string]string)
for k, v := range params {
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 nil, fmt.Errorf("gname api error: failed to send request: %w", err)
} else if resp.IsError() {
return nil, fmt.Errorf("gname api error: unexpected status code: %d, %s", resp.StatusCode(), resp.Body())
}
return resp, nil
}
func (c *Client) sendRequestWithResult(path string, params map[string]any, 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 result.GetCode() != 1 {
return fmt.Errorf("gname api error: %d - %s", result.GetCode(), result.GetMsg())
}
return nil
}