-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdomains.go
272 lines (253 loc) · 7.07 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
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
package zeit
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"net/http"
)
type User struct {
Id string `json:"id"`
Username string `json:"username"`
Email string `json:"email"`
CustomerId string `json:"customerId"`
}
type Aliases struct {
Id string
Alias string
Created *Time
}
type Certs struct {
Id string
Cns []string
Created *Time
}
type Domain struct {
Id string `json:"id"`
Name string `json:"name"`
ServiceType string `json:"serviceType"`
NsVerifiedAt *Time `json:"nsVerifiedAt"`
TxtVerifiedAt *Time `json:"txtVerifiedAt"`
CdnEnabled bool `json:"cdnEnabled"`
CreatedAt *Time `json:"createdAt"`
ExpiresAt *Time `json:"expiresAt"`
BoughtAt *Time `json:"boughtAt"`
VerifiedRecord string `string:"verifiedRecord"`
Verified bool `json:"verified"`
Nameservers []string `json:"nameservers"`
IntendedNameservers []string `json:"intendedNameservers"`
Creator User `json:"creator"`
Suffix bool `json:"suffix,omitempty"`
Aliases []Aliases `json:"aliases,omitempty"`
Certs []Certs `json:"certs,omitempty"`
}
// GetAllDomains will return a slice of domains registered with the user.
func (c Client) ListAllDomains() ([]Domain, error) {
resp, err := c.makeAndDoRequest(http.MethodGet, "v4/domains", nil)
if err != nil {
return nil, err
}
defer closeResponseBody(resp)
domains := struct {
Domains []Domain `json:"domains"`
}{}
err = json.NewDecoder(resp.Body).Decode(&domains)
if err != nil {
return nil, err
}
return domains.Domains, nil
}
// AddDomain will add a specified domain name to ZEIT, either as an external or internal domain.
func (c Client) AddDomain(name string) (*Domain, error) {
parameters := struct {
Name string `json:"name"`
}{name}
body, err := json.Marshal(parameters)
if err != nil {
return nil, err
}
resp, err := c.makeAndDoRequest(http.MethodPost, "v4/domains", bytes.NewBuffer(body))
defer closeResponseBody(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
domain := Domain{}
err = json.NewDecoder(resp.Body).Decode(&domain)
if err != nil {
return nil, err
}
return &domain, nil
}
// TransferInDomain will initiate a domain transfer request from an external Registrar to ZEIT.
func (c Client) TransferInDomain(name, authCode string, expectedPrice int) (*Domain, error) {
parameters := struct {
Method string `json:"method"`
Name string `json:"name"`
AuthCode string `json:"authCode"`
ExpectedPrice int `json:"expectedPrice"`
}{"transfer-in", name, authCode, expectedPrice}
body, err := json.Marshal(parameters)
if err != nil {
return nil, err
}
resp, err := c.makeAndDoRequest(http.MethodPost, "v4/domains", bytes.NewBuffer(body))
defer closeResponseBody(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
return nil, errors.New(resp.Status)
}
domain := Domain{}
err = json.NewDecoder(resp.Body).Decode(&domain)
if err != nil {
return nil, err
}
return &domain, nil
}
// VerifyDomain will check if the domain either has the correct nameservers for ZEIT defined or if the DNS TXT
// verification is set.
func (c Client) VerifyDomain(name string) (*Domain, error) {
endpoint := fmt.Sprintf("v4/domains/%s/verify", name)
resp, err := c.makeAndDoRequest(http.MethodPost, endpoint, nil)
defer closeResponseBody(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
verificationError := VerificationError{}
err := json.NewDecoder(resp.Body).Decode(&struct {
Error VerificationError
}{verificationError})
if err != nil {
return nil, err
}
return nil, verificationError
}
domain := Domain{}
err = json.NewDecoder(resp.Body).Decode(&struct {
Domain Domain
}{domain})
if err != nil {
return nil, err
}
return &domain, nil
}
// GetDomain will return specific information for one domain.
func (c Client) GetDomain(name string) (*Domain, error) {
endpoint := fmt.Sprintf("v4/domains/%s", name)
resp, err := c.makeAndDoRequest(http.MethodGet, endpoint, nil)
defer closeResponseBody(resp)
if err != nil {
return nil, err
}
if resp.StatusCode != http.StatusOK {
getError := GetError{}
err := json.NewDecoder(resp.Body).Decode(&struct {
Error GetError
}{getError})
if err != nil {
return nil, err
}
return nil, getError
}
domain := Domain{}
err = json.NewDecoder(resp.Body).Decode(&struct {
Domain Domain
}{domain})
if err != nil {
return nil, err
}
return &domain, nil
}
// RemoveDomain will remove a domain from the ZEIT DNS server.
func (c Client) RemoveDomain(name string) (string, error) {
endpoint := fmt.Sprintf("v4/domains/%s", name)
resp, err := c.makeAndDoRequest(http.MethodDelete, endpoint, nil)
if err != nil {
return "", err
}
if resp.StatusCode != http.StatusOK {
return "", errors.New(resp.Status)
}
var uid string
err = json.NewDecoder(resp.Body).Decode(&struct {
Uid *string
}{&uid})
if err != nil {
return "", err
}
return uid, nil
}
// CheckDomainAvailability will check if the specified domain is available for sale.
func (c Client) CheckDomainAvailability(name string) (bool, error) {
endpoint := fmt.Sprintf("v4/domains/status?%s", name)
resp, err := c.makeAndDoRequest(http.MethodGet, endpoint, nil)
if err != nil {
return false, err
}
if resp.StatusCode != http.StatusOK {
return false, errors.New(resp.Status)
}
var available bool
err = json.NewDecoder(resp.Body).Decode(&struct {
Available *bool
}{&available})
if err != nil {
return false, err
}
return available, nil
}
// CheckDomainPrice will check how much a domain will cost to purchase and will return the price and period of purchase.
func (c Client) CheckDomainPrice(name string) (int, int, error) {
endpoint := fmt.Sprintf("v4/domains/price?%s", name)
resp, err := c.makeAndDoRequest(http.MethodGet, endpoint, nil)
if err != nil {
return 0, 0, err
}
if resp.StatusCode != http.StatusOK {
return 0, 0, errors.New(resp.Status)
}
var price, period int
err = json.NewDecoder(resp.Body).Decode(&struct {
Price *int
Period *int
}{&price, &period})
if err != nil {
return 0, 0, err
}
return price, period, nil
}
// BuyDomain will buy a domain name at the expectedPrice.
func (c Client) BuyDomain(name string, expectedPrice int) error {
parameters := struct {
Name string `json:"name"`
ExpectedPrice int `json:"expectedPrice"`
}{name, expectedPrice}
body, err := json.Marshal(parameters)
if err != nil {
return err
}
resp, err := c.makeAndDoRequest(http.MethodPost, "v4/domains/buy", bytes.NewBuffer(body))
if err != nil {
return err
}
if resp.StatusCode == http.StatusForbidden {
buyError := BasicError{}
err := json.NewDecoder(resp.Body).Decode(&struct {
Error BasicError
}{buyError})
if err != nil {
return err
}
defer closeResponseBody(resp)
return buyError
}
if resp.StatusCode != http.StatusOK {
return errors.New(resp.Status)
}
return nil
}