This repository was archived by the owner on Aug 16, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
/
Copy pathdomains.go
162 lines (136 loc) · 4.95 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
package dnspod
import (
"encoding/json"
"fmt"
"strconv"
// "time"
)
// 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 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"`
}
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"`
}
// domainRequest represents a generic wrapper for a domain request,
// when domainWrapper cannot be used because of type constraint on Domain.
type domainRequest struct {
Domain interface{} `json:"domain"`
}
// domainAction generates the resource path for given domain.
func domainAction(action string) string {
if len(action) > 0 {
return fmt.Sprintf("Domain.%s", action)
}
return "Domain.List"
}
// List the domains.
//
// dnspod API docs: https://www.dnspod.cn/docs/domains.html#domain-list
func (s *DomainsService) List() ([]Domain, *Response, error) {
path := domainAction("List")
returnedDomains := domainListWrapper{}
payload := newPayLoad(s.client.CommonParams)
res, err := s.client.post(path, payload, &returnedDomains)
if err != nil {
return []Domain{}, res, err
}
domains := []Domain{}
if returnedDomains.Status.Code != "1" {
return domains, nil, fmt.Errorf("Could not get domains: %s", returnedDomains.Status.Message)
}
for _, domain := range returnedDomains.Domains {
domains = append(domains, domain)
}
return 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) {
path := domainAction("Create")
returnedDomain := domainWrapper{}
payload := newPayLoad(s.client.CommonParams)
payload.Set("domain", domainAttributes.Name)
payload.Set("group_id", domainAttributes.GroupID.String())
payload.Set("is_mark", domainAttributes.IsMark)
res, err := s.client.post(path, 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) {
path := domainAction("Info")
returnedDomain := domainWrapper{}
payload := newPayLoad(s.client.CommonParams)
payload.Set("domain_id", strconv.FormatInt(int64(ID), 10))
res, err := s.client.post(path, 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) {
path := domainAction("Remove")
returnedDomain := domainWrapper{}
payload := newPayLoad(s.client.CommonParams)
payload.Set("domain_id", strconv.FormatInt(int64(ID), 10))
res, err := s.client.post(path, payload, &returnedDomain)
if err != nil {
return res, err
}
return res, nil
}