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,46 @@
package dnsla
import (
"context"
"net/http"
)
type CreateRecordRequest struct {
DomainId string `json:"domainId"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type int32 `json:"type"`
Host string `json:"host"`
Data string `json:"data"`
Ttl int32 `json:"ttl"`
Weight *int32 `json:"weight,omitempty"`
Preference *int32 `json:"preference,omitempty"`
}
type CreateRecordResponse struct {
apiResponseBase
Data *struct {
Id string `json:"id"`
} `json:"data,omitempty"`
}
func (c *Client) CreateRecord(req *CreateRecordRequest) (*CreateRecordResponse, error) {
return c.CreateRecordWithContext(context.Background(), req)
}
func (c *Client) CreateRecordWithContext(ctx context.Context, req *CreateRecordRequest) (*CreateRecordResponse, error) {
httpreq, err := c.newRequest(http.MethodPost, "/record")
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &CreateRecordResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,36 @@
package dnsla
import (
"context"
"fmt"
"net/http"
)
type DeleteRecordResponse struct {
apiResponseBase
}
func (c *Client) DeleteRecord(recordId string) (*DeleteRecordResponse, error) {
return c.DeleteRecordWithContext(context.Background(), recordId)
}
func (c *Client) DeleteRecordWithContext(ctx context.Context, recordId string) (*DeleteRecordResponse, error) {
if recordId == "" {
return nil, fmt.Errorf("sdkerr: unset recordId")
}
httpreq, err := c.newRequest(http.MethodDelete, "/record")
if err != nil {
return nil, err
} else {
httpreq.SetQueryParam("id", recordId)
httpreq.SetContext(ctx)
}
result := &DeleteRecordResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,51 @@
package dnsla
import (
"context"
"net/http"
"strconv"
)
type ListDomainsRequest struct {
GroupId *string `json:"groupId,omitempty"`
PageIndex *int32 `json:"pageIndex,omitempty"`
PageSize *int32 `json:"pageSize,omitempty"`
}
type ListDomainsResponse struct {
apiResponseBase
Data *struct {
Total int32 `json:"total"`
Results []*DomainRecord `json:"results"`
} `json:"data,omitempty"`
}
func (c *Client) ListDomains(req *ListDomainsRequest) (*ListDomainsResponse, error) {
return c.ListDomainsWithContext(context.Background(), req)
}
func (c *Client) ListDomainsWithContext(ctx context.Context, req *ListDomainsRequest) (*ListDomainsResponse, error) {
httpreq, err := c.newRequest(http.MethodGet, "/domainList")
if err != nil {
return nil, err
} else {
if req.GroupId != nil {
httpreq.SetQueryParam("groupId", *req.GroupId)
}
if req.PageIndex != nil {
httpreq.SetQueryParam("pageIndex", strconv.Itoa(int(*req.PageIndex)))
}
if req.PageSize != nil {
httpreq.SetQueryParam("pageSize", strconv.Itoa(int(*req.PageSize)))
}
httpreq.SetContext(ctx)
}
result := &ListDomainsResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,71 @@
package dnsla
import (
"context"
"net/http"
"strconv"
)
type ListRecordsRequest struct {
DomainId *string `json:"domainId,omitempty"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type *int32 `json:"type,omitempty"`
Host *string `json:"host,omitempty"`
Data *string `json:"data,omitempty"`
PageIndex *int32 `json:"pageIndex,omitempty"`
PageSize *int32 `json:"pageSize,omitempty"`
}
type ListRecordsResponse struct {
apiResponseBase
Data *struct {
Total int32 `json:"total"`
Results []*DnsRecord `json:"results"`
} `json:"data,omitempty"`
}
func (c *Client) ListRecords(req *ListRecordsRequest) (*ListRecordsResponse, error) {
return c.ListRecordsWithContext(context.Background(), req)
}
func (c *Client) ListRecordsWithContext(ctx context.Context, req *ListRecordsRequest) (*ListRecordsResponse, error) {
httpreq, err := c.newRequest(http.MethodGet, "/recordList")
if err != nil {
return nil, err
} else {
if req.DomainId != nil {
httpreq.SetQueryParam("domainId", *req.DomainId)
}
if req.GroupId != nil {
httpreq.SetQueryParam("groupId", *req.GroupId)
}
if req.LineId != nil {
httpreq.SetQueryParam("lineId", *req.LineId)
}
if req.Type != nil {
httpreq.SetQueryParam("type", strconv.Itoa(int(*req.Type)))
}
if req.Host != nil {
httpreq.SetQueryParam("host", *req.Host)
}
if req.Data != nil {
httpreq.SetQueryParam("data", *req.Data)
}
if req.PageIndex != nil {
httpreq.SetQueryParam("pageIndex", strconv.Itoa(int(*req.PageIndex)))
}
if req.PageSize != nil {
httpreq.SetQueryParam("pageSize", strconv.Itoa(int(*req.PageSize)))
}
httpreq.SetContext(ctx)
}
result := &ListRecordsResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,43 @@
package dnsla
import (
"context"
"net/http"
)
type UpdateRecordRequest struct {
Id string `json:"id"`
GroupId *string `json:"groupId,omitempty"`
LineId *string `json:"lineId,omitempty"`
Type *int32 `json:"type,omitempty"`
Host *string `json:"host,omitempty"`
Data *string `json:"data,omitempty"`
Ttl *int32 `json:"ttl,omitempty"`
Weight *int32 `json:"weight,omitempty"`
Preference *int32 `json:"preference,omitempty"`
}
type UpdateRecordResponse struct {
apiResponseBase
}
func (c *Client) UpdateRecord(req *UpdateRecordRequest) (*UpdateRecordResponse, error) {
return c.UpdateRecordWithContext(context.Background(), req)
}
func (c *Client) UpdateRecordWithContext(ctx context.Context, req *UpdateRecordRequest) (*UpdateRecordResponse, error) {
httpreq, err := c.newRequest(http.MethodPut, "/record")
if err != nil {
return nil, err
} else {
httpreq.SetBody(req)
httpreq.SetContext(ctx)
}
result := &UpdateRecordResponse{}
if _, err := c.doRequestWithResult(httpreq, result); err != nil {
return result, err
}
return result, nil
}

View File

@@ -0,0 +1,94 @@
package dnsla
import (
"encoding/json"
"fmt"
"time"
"github.com/go-resty/resty/v2"
)
type Client struct {
client *resty.Client
}
func NewClient(apiId, apiSecret string) (*Client, error) {
if apiId == "" {
return nil, fmt.Errorf("sdkerr: unset apiId")
}
if apiSecret == "" {
return nil, fmt.Errorf("sdkerr: unset apiSecret")
}
client := resty.New().
SetBaseURL("https://api.dns.la/api").
SetBasicAuth(apiId, apiSecret).
SetHeader("Accept", "application/json").
SetHeader("Content-Type", "application/json").
SetHeader("User-Agent", "certimate")
return &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) {
if method == "" {
return nil, fmt.Errorf("sdkerr: unset method")
}
if path == "" {
return nil, fmt.Errorf("sdkerr: unset path")
}
req := c.client.R()
req.Method = method
req.URL = path
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.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/100 != 2 {
return resp, fmt.Errorf("sdkerr: code='%d', message='%s'", tcode, res.GetMessage())
}
}
}
return resp, nil
}

59
pkg/sdk3rd/dnsla/types.go Normal file
View File

@@ -0,0 +1,59 @@
package dnsla
type apiResponse interface {
GetCode() int32
GetMessage() string
}
type apiResponseBase struct {
Code *int32 `json:"code,omitempty"`
Message *string `json:"message,omitempty"`
}
func (r *apiResponseBase) GetCode() int32 {
if r.Code == nil {
return 0
}
return *r.Code
}
func (r *apiResponseBase) GetMessage() string {
if r.Message == nil {
return ""
}
return *r.Message
}
var _ apiResponse = (*apiResponseBase)(nil)
type DomainRecord struct {
Id string `json:"id"`
GroupId string `json:"groupId"`
GroupName string `json:"groupName"`
Domain string `json:"domain"`
DisplayDomain string `json:"displayDomain"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}
type DnsRecord struct {
Id string `json:"id"`
DomainId string `json:"domainId"`
GroupId string `json:"groupId"`
GroupName string `json:"groupName"`
LineId string `json:"lineId"`
LineCode string `json:"lineCode"`
LineName string `json:"lineName"`
Type int32 `json:"type"`
Host string `json:"host"`
DisplayHost string `json:"displayHost"`
Data string `json:"data"`
DisplayData string `json:"displayData"`
Ttl int32 `json:"ttl"`
Weight int32 `json:"weight"`
Preference int32 `json:"preference"`
CreatedAt int64 `json:"createdAt"`
UpdatedAt int64 `json:"updatedAt"`
}