refactor: re-impl sdk3rd
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
package gname
|
||||
|
||||
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
|
||||
}
|
||||
42
internal/pkg/sdk3rd/gname/api_add_domain_resolution.go
Normal file
42
internal/pkg/sdk3rd/gname/api_add_domain_resolution.go
Normal 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
|
||||
}
|
||||
35
internal/pkg/sdk3rd/gname/api_delete_domain_resolution.go
Normal file
35
internal/pkg/sdk3rd/gname/api_delete_domain_resolution.go
Normal 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
|
||||
}
|
||||
41
internal/pkg/sdk3rd/gname/api_list_domain_resolution.go
Normal file
41
internal/pkg/sdk3rd/gname/api_list_domain_resolution.go
Normal 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
|
||||
}
|
||||
40
internal/pkg/sdk3rd/gname/api_modify_domain_resolution.go
Normal file
40
internal/pkg/sdk3rd/gname/api_modify_domain_resolution.go
Normal 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
|
||||
}
|
||||
@@ -19,9 +19,17 @@ type Client struct {
|
||||
client *resty.Client
|
||||
}
|
||||
|
||||
func NewClient(appId, appKey string) *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")
|
||||
|
||||
@@ -29,15 +37,93 @@ func NewClient(appId, appKey string) *Client {
|
||||
appId: appId,
|
||||
appKey: appKey,
|
||||
client: client,
|
||||
}
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (c *Client) WithTimeout(timeout time.Duration) *Client {
|
||||
func (c *Client) SetTimeout(timeout time.Duration) *Client {
|
||||
c.client.SetTimeout(timeout)
|
||||
return c
|
||||
}
|
||||
|
||||
func (c *Client) generateSignature(params map[string]string) string {
|
||||
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 {
|
||||
@@ -54,51 +140,9 @@ func (c *Client) generateSignature(params map[string]string) string {
|
||||
stringA := strings.Join(pairs, "&")
|
||||
|
||||
// Step 3: Append appkey to create string B
|
||||
stringB := stringA + c.appKey
|
||||
stringB := stringA + 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)
|
||||
|
||||
req := c.client.R().SetFormData(data)
|
||||
resp, err := req.Post(path)
|
||||
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.String())
|
||||
}
|
||||
|
||||
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 unmarshal response: %w", err)
|
||||
} else if errcode := result.GetCode(); errcode != 1 {
|
||||
return fmt.Errorf("gname api error: code='%d', message='%s'", errcode, result.GetMessage())
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,81 +0,0 @@
|
||||
package gname
|
||||
|
||||
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"`
|
||||
}
|
||||
32
internal/pkg/sdk3rd/gname/types.go
Normal file
32
internal/pkg/sdk3rd/gname/types.go
Normal 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"`
|
||||
}
|
||||
Reference in New Issue
Block a user