forked from liushiqi1001/dnspod-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains.go
173 lines (146 loc) · 4.99 KB
/
domains.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package dnspod
import (
"encoding/json"
"fmt"
"net/url"
"strconv"
)
const (
methodDomainList = "Domain.List"
methodDomainCreate = "Domain.Create"
methodDomainInfo = "Domain.Info"
methodDomainRemove = "Domain.Remove"
)
// DomainInfo handles domain information.
type DomainInfo struct {
DomainTotal json.Number `json:"domain_total,omitempty"`
AllTotal json.Number `json:"all_total,omitempty"`
MineTotal json.Number `json:"mine_total,omitempty"`
ShareTotal json.Number `json:"share_total,omitempty"`
VipTotal json.Number `json:"vip_total,omitempty"`
IsMarkTotal json.Number `json:"ismark_total,omitempty"`
PauseTotal json.Number `json:"pause_total,omitempty"`
ErrorTotal json.Number `json:"error_total,omitempty"`
LockTotal json.Number `json:"lock_total,omitempty"`
SpamTotal json.Number `json:"spam_total,omitempty"`
VipExpire json.Number `json:"vip_expire,omitempty"`
ShareOutTotal json.Number `json:"share_out_total,omitempty"`
}
// Domain handles domain.
type Domain struct {
ID json.Number `json:"id,omitempty"`
Name string `json:"name,omitempty"`
PunyCode string `json:"punycode,omitempty"`
Grade string `json:"grade,omitempty"`
GradeTitle string `json:"grade_title,omitempty"`
Status string `json:"status,omitempty"`
ExtStatus string `json:"ext_status,omitempty"`
Records string `json:"records,omitempty"`
GroupID json.Number `json:"group_id,omitempty"`
IsMark string `json:"is_mark,omitempty"`
Remark string `json:"remark,omitempty"`
IsVIP string `json:"is_vip,omitempty"`
SearchenginePush string `json:"searchengine_push,omitempty"`
UserID string `json:"user_id,omitempty"`
CreatedOn string `json:"created_on,omitempty"`
UpdatedOn string `json:"updated_on,omitempty"`
TTL string `json:"ttl,omitempty"`
CNameSpeedUp string `json:"cname_speedup,omitempty"`
Owner string `json:"owner,omitempty"`
AuthToAnquanBao bool `json:"auth_to_anquanbao,omitempty"`
}
type domainListWrapper struct {
Status Status `json:"status"`
Info DomainInfo `json:"info"`
Domains []Domain `json:"domains"`
}
type domainWrapper struct {
Status Status `json:"status"`
Info DomainInfo `json:"info"`
Domain Domain `json:"domain"`
}
// DomainsService handles communication with the domain related methods of the dnspod API.
//
// dnspod API docs: https://www.dnspod.cn/docs/domains.html
type DomainsService struct {
client *Client
}
type DomainListRequest struct {
CommonParams
Type string
Offset string
Length string
GroupId string
Keyword string
}
func (c *DomainListRequest) toPayLOad() url.Values {
p := c.CommonParams.toPayLoad()
if c.Type != "" {
p.Set("type", c.Type)
}
if c.Offset != "" {
p.Set("offset", c.Offset)
}
if c.Length != "" {
p.Set("length", c.Length)
}
if c.GroupId != "" {
p.Set("group_id", c.GroupId)
}
if c.Keyword != "" {
p.Set("keyword", c.Keyword)
}
return p
}
// List the domains.
//
// dnspod API docs: https://www.dnspod.cn/docs/domains.html#domain-list
func (s *DomainsService) List(request *DomainListRequest) ([]Domain, *Response, error) {
payLoad := request.toPayLOad()
returnedDomains := domainListWrapper{}
res, err := s.client.post(methodDomainList, payLoad, &returnedDomains)
if err != nil {
return nil, res, err
}
if returnedDomains.Status.Code != "1" {
return nil, nil, fmt.Errorf("could not get domains: %s", returnedDomains.Status.Message)
}
return returnedDomains.Domains, res, nil
}
// Create a new domain.
//
// dnspod API docs: https://www.dnspod.cn/docs/domains.html#domain-create
func (s *DomainsService) Create(domainAttributes Domain) (Domain, *Response, error) {
payload := s.client.CommonParams.toPayLoad()
payload.Set("domain", domainAttributes.Name)
payload.Set("group_id", domainAttributes.GroupID.String())
payload.Set("is_mark", domainAttributes.IsMark)
returnedDomain := domainWrapper{}
res, err := s.client.post(methodDomainCreate, payload, &returnedDomain)
if err != nil {
return Domain{}, res, err
}
return returnedDomain.Domain, res, nil
}
// Get fetches a domain.
//
// dnspod API docs: https://www.dnspod.cn/docs/domains.html#domain-info
func (s *DomainsService) Get(id int) (Domain, *Response, error) {
payload := s.client.CommonParams.toPayLoad()
payload.Set("domain_id", strconv.Itoa(id))
returnedDomain := domainWrapper{}
res, err := s.client.post(methodDomainInfo, payload, &returnedDomain)
if err != nil {
return Domain{}, res, err
}
return returnedDomain.Domain, res, nil
}
// Delete a domain.
//
// dnspod API docs: https://dnsapi.cn/Domain.Remove
func (s *DomainsService) Delete(id int) (*Response, error) {
payload := s.client.CommonParams.toPayLoad()
payload.Set("domain_id", strconv.Itoa(id))
returnedDomain := domainWrapper{}
return s.client.post(methodDomainRemove, payload, &returnedDomain)
}