-
Notifications
You must be signed in to change notification settings - Fork 14
/
domain.go
307 lines (263 loc) · 9.01 KB
/
domain.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
package goinwx
import (
"time"
"fmt"
"errors"
"github.com/fatih/structs"
"github.com/mitchellh/mapstructure"
)
const (
methodDomainCheck = "domain.check"
methodDomainCreate = "domain.create"
methodDomainDelete = "domain.delete"
methodDomainGetPrices = "domain.getPrices"
methodDomainGetRules = "domain.getRules"
methodDomainInfo = "domain.info"
methodDomainList = "domain.list"
methodDomainLog = "domain.log"
methodDomainPush = "domain.push"
methodDomainRenew = "domain.renew"
methodDomainRestore = "domain.restore"
methodDomainStats = "domain.stats"
methodDomainTrade = "domain.trade"
methodDomainTransfer = "domain.transfer"
methodDomainTransferOut = "domain.transferOut"
methodDomainUpdate = "domain.update"
methodDomainWhois = "domain.whois"
)
type DomainService interface {
Check(domains []string) ([]DomainCheckResponse, error)
Register(request *DomainRegisterRequest) (*DomainRegisterResponse, error)
Delete(domain string, scheduledDate time.Time) error
Info(domain string, roId int) (*DomainInfoResponse, error)
GetPrices(tlds []string) ([]DomainPriceResponse, error)
List(*DomainListRequest) (*DomainList, error)
Whois(domain string) (string, error)
}
type DomainServiceOp struct {
client *Client
}
var _ DomainService = &DomainServiceOp{}
type domainCheckResponseRoot struct {
Domains []DomainCheckResponse `mapstructure:"domain"`
}
type DomainCheckResponse struct {
Available int `mapstructure:"avail"`
Status string `mapstructure:"status"`
Name string `mapstructure:"name"`
Domain string `mapstructure:"domain"`
TLD string `mapstructure:"tld"`
CheckMethod string `mapstructure:"checkmethod"`
Price float32 `mapstructure:"price"`
CheckTime float32 `mapstructure:"checktime"`
}
type domainPriceResponseRoot struct {
Prices []DomainPriceResponse `mapstructure:"price"`
}
type DomainPriceResponse struct {
Tld string `mapstructure:"tld"`
Currency string `mapstructure:"currency"`
CreatePrice float32 `mapstructure:"createPrice"`
MonthlyCreatePrice float32 `mapstructure:"monthlyCreatePrice"`
TransferPrice float32 `mapstructure:"transferPrice"`
RenewalPrice float32 `mapstructure:"renewalPrice"`
MonthlyRenewalPrice float32 `mapstructure:"monthlyRenewalPrice"`
UpdatePrice float32 `mapstructure:"updatePrice"`
TradePrice float32 `mapstructure:"tradePrice"`
TrusteePrice float32 `mapstructure:"trusteePrice"`
MonthlyTrusteePrice float32 `mapstructure:"monthlyTrusteePrice"`
CreatePeriod int `mapstructure:"createPeriod"`
TransferPeriod int `mapstructure:"transferPeriod"`
RenewalPeriod int `mapstructure:"renewalPeriod"`
TradePeriod int `mapstructure:"tradePeriod"`
}
type DomainRegisterRequest struct {
Domain string `structs:"domain"`
Period string `structs:"period,omitempty"`
Registrant int `structs:"registrant"`
Admin int `structs:"admin"`
Tech int `structs:"tech"`
Billing int `structs:"billing"`
Nameservers []string `structs:"ns,omitempty"`
TransferLock string `structs:"transferLock,omitempty"`
RenewalMode string `structs:"renewalMode,omitempty"`
WhoisProvider string `structs:"whoisProvider,omitempty"`
WhoisUrl string `structs:"whoisUrl,omitempty"`
ScDate string `structs:"scDate,omitempty"`
ExtDate string `structs:"extDate,omitempty"`
Asynchron string `structs:"asynchron,omitempty"`
Voucher string `structs:"voucher,omitempty"`
Testing string `structs:"testing,omitempty"`
}
type DomainRegisterResponse struct {
RoId int
Price float32
Currency string
}
type DomainInfoResponse struct {
RoId int `mapstructure:"roId"`
Domain string `mapstructure:"domain"`
DomainAce string `mapstructure:"domainAce"`
Period string `mapstructure:"period"`
CrDate time.Time `mapstructure:"crDate"`
ExDate time.Time `mapstructure:"exDate"`
UpDate time.Time `mapstructure:"upDate"`
ReDate time.Time `mapstructure:"reDate"`
ScDate time.Time `mapstructure:"scDate"`
TransferLock int `mapstructure:"transferLock"`
Status string `mapstructure:"status"`
AuthCode string `mapstructure:"authCode"`
RenewalMode string `mapstructure:"renewalMode"`
TransferMode string `mapstructure:"transferMode"`
Registrant int `mapstructure:"registrant"`
Admin int `mapstructure:"admin"`
Tech int `mapstructure:"tech"`
Billing int `mapstructure:"billing"`
Nameservers []string `mapstructure:"ns"`
NoDelegation string `mapstructure:"noDelegation"`
Contacts map[string]Contact `mapstructure:"contact"`
}
type Contact struct {
RoId int
Id string
Type string
Name string
Org string
Street string
City string
PostalCode string `mapstructure:"pc"`
StateProvince string `mapstructure:"sp"`
Country string `mapstructure:"cc"`
Phone string `mapstructure:"voice"`
Fax string
Email string
Remarks string
Protection int
}
type DomainListRequest struct {
Domain string `structs:"domain,omitempty"`
RoId int `structs:"roId,omitempty"`
Status int `structs:"status,omitempty"`
Registrant int `structs:"registrant,omitempty"`
Admin int `structs:"admin,omitempty"`
Tech int `structs:"tech,omitempty"`
Billing int `structs:"billing,omitempty"`
RenewalMode int `structs:"renewalMode,omitempty"`
TransferLock int `structs:"transferLock,omitempty"`
NoDelegation int `structs:"noDelegation,omitempty"`
Tag int `structs:"tag,omitempty"`
Order int `structs:"order,omitempty"`
Page int `structs:"page,omitempty"`
Pagelimit int `structs:"pagelimit,omitempty"`
}
type DomainList struct {
Count int
Domains []DomainInfoResponse `mapstructure:"domain"`
}
func (s *DomainServiceOp) Check(domains []string) ([]DomainCheckResponse, error) {
req := s.client.NewRequest(methodDomainCheck, map[string]interface{}{
"domain": domains,
"wide": "2",
})
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
root := new(domainCheckResponseRoot)
err = mapstructure.Decode(*resp, &root)
if err != nil {
return nil, err
}
return root.Domains, nil
}
func (s *DomainServiceOp) GetPrices(tlds []string) ([]DomainPriceResponse, error) {
req := s.client.NewRequest(methodDomainGetPrices, map[string]interface{}{
"tld": tlds,
"vat": false,
})
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
root := new(domainPriceResponseRoot)
err = mapstructure.Decode(*resp, &root)
if err != nil {
return nil, err
}
return root.Prices, nil
}
func (s *DomainServiceOp) Register(request *DomainRegisterRequest) (*DomainRegisterResponse, error) {
req := s.client.NewRequest(methodDomainCreate, structs.Map(request))
//fmt.Println("Args", req.Args)
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result DomainRegisterResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *DomainServiceOp) Delete(domain string, scheduledDate time.Time) error {
req := s.client.NewRequest(methodDomainDelete, map[string]interface{}{
"domain": domain,
"scDate": scheduledDate.Format(time.RFC3339),
})
_, err := s.client.Do(*req)
return err
}
func (s *DomainServiceOp) Info(domain string, roId int) (*DomainInfoResponse, error) {
req := s.client.NewRequest(methodDomainInfo, map[string]interface{}{
"domain": domain,
"wide": "2",
})
if roId != 0 {
req.Args["roId"] = roId
}
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result DomainInfoResponse
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
fmt.Println("Response", result)
return &result, nil
}
func (s *DomainServiceOp) List(request *DomainListRequest) (*DomainList, error) {
if request == nil {
return nil, errors.New("Request can't be nil")
}
requestMap := structs.Map(request)
requestMap["wide"] = "2"
req := s.client.NewRequest(methodDomainList, requestMap)
resp, err := s.client.Do(*req)
if err != nil {
return nil, err
}
var result DomainList
err = mapstructure.Decode(*resp, &result)
if err != nil {
return nil, err
}
return &result, nil
}
func (s *DomainServiceOp) Whois(domain string) (string, error) {
req := s.client.NewRequest(methodDomainWhois, map[string]interface{}{
"domain": domain,
})
resp, err := s.client.Do(*req)
if err != nil {
return "", err
}
var result map[string]string
err = mapstructure.Decode(*resp, &result)
if err != nil {
return "", err
}
return result["whois"], nil
}