domains.go raw
1 package namecom
2
3 import (
4 "bytes"
5 "encoding/json"
6 "fmt"
7 "net/url"
8 )
9
10 var _ = bytes.MinRead
11
12 // ListDomains returns all domains in the account. It omits some information that can be retrieved from GetDomain.
13 func (n *NameCom) ListDomains(request *ListDomainsRequest) (*ListDomainsResponse, error) {
14 endpoint := fmt.Sprintf("/v4/domains")
15
16 values := url.Values{}
17 if request.PerPage != 0 {
18 values.Set("perPage", fmt.Sprintf("%d", request.PerPage))
19 }
20 if request.Page != 0 {
21 values.Set("page", fmt.Sprintf("%d", request.Page))
22 }
23
24 body, err := n.get(endpoint, values)
25 if err != nil {
26 return nil, err
27 }
28
29 resp := &ListDomainsResponse{}
30
31 err = json.NewDecoder(body).Decode(resp)
32 if err != nil {
33 return nil, err
34 }
35
36 return resp, nil
37 }
38
39 // GetDomain returns details about a specific domain
40 func (n *NameCom) GetDomain(request *GetDomainRequest) (*Domain, error) {
41 endpoint := fmt.Sprintf("/v4/domains/%s", request.DomainName)
42
43 values := url.Values{}
44
45 body, err := n.get(endpoint, values)
46 if err != nil {
47 return nil, err
48 }
49
50 resp := &Domain{}
51
52 err = json.NewDecoder(body).Decode(resp)
53 if err != nil {
54 return nil, err
55 }
56
57 return resp, nil
58 }
59
60 // CreateDomain purchases a new domain. Domains that are not regularly priced require the purchase_price field to be specified.
61 func (n *NameCom) CreateDomain(request *CreateDomainRequest) (*CreateDomainResponse, error) {
62 endpoint := fmt.Sprintf("/v4/domains")
63
64 post := &bytes.Buffer{}
65 err := json.NewEncoder(post).Encode(request)
66 if err != nil {
67 return nil, err
68 }
69
70 body, err := n.post(endpoint, post)
71 if err != nil {
72 return nil, err
73 }
74
75 resp := &CreateDomainResponse{}
76
77 err = json.NewDecoder(body).Decode(resp)
78 if err != nil {
79 return nil, err
80 }
81
82 return resp, nil
83 }
84
85 // EnableWhoisPrivacy enables the domain to be private
86 func (n *NameCom) EnableWhoisPrivacy(request *EnableWhoisPrivacyForDomainRequest) (*Domain, error) {
87 endpoint := fmt.Sprintf("/v4/domains/%s:enableWhoisPrivacy", request.DomainName)
88
89 post := &bytes.Buffer{}
90 err := json.NewEncoder(post).Encode(request)
91 if err != nil {
92 return nil, err
93 }
94
95 body, err := n.post(endpoint, post)
96 if err != nil {
97 return nil, err
98 }
99
100 resp := &Domain{}
101
102 err = json.NewDecoder(body).Decode(resp)
103 if err != nil {
104 return nil, err
105 }
106
107 return resp, nil
108 }
109
110 // DisableWhoisPrivacy disables domain privacy
111 func (n *NameCom) DisableWhoisPrivacy(request *DisableWhoisPrivacyForDomainRequest) (*Domain, error) {
112 endpoint := fmt.Sprintf("/v4/domains/%s:disableWhoisPrivacy", request.DomainName)
113
114 post := &bytes.Buffer{}
115 err := json.NewEncoder(post).Encode(request)
116 if err != nil {
117 return nil, err
118 }
119
120 body, err := n.post(endpoint, post)
121 if err != nil {
122 return nil, err
123 }
124
125 resp := &Domain{}
126
127 err = json.NewDecoder(body).Decode(resp)
128 if err != nil {
129 return nil, err
130 }
131
132 return resp, nil
133 }
134
135 // EnableAutorenew enables the domain to be automatically renewed when it gets close to expiring.
136 func (n *NameCom) EnableAutorenew(request *EnableAutorenewForDomainRequest) (*Domain, error) {
137 endpoint := fmt.Sprintf("/v4/domains/%s:enableAutorenew", request.DomainName)
138
139 post := &bytes.Buffer{}
140 err := json.NewEncoder(post).Encode(request)
141 if err != nil {
142 return nil, err
143 }
144
145 body, err := n.post(endpoint, post)
146 if err != nil {
147 return nil, err
148 }
149
150 resp := &Domain{}
151
152 err = json.NewDecoder(body).Decode(resp)
153 if err != nil {
154 return nil, err
155 }
156
157 return resp, nil
158 }
159
160 // DisableAutorenew disables automatic renewals, thus requiring the domain to be renewed manually.
161 func (n *NameCom) DisableAutorenew(request *DisableAutorenewForDomainRequest) (*Domain, error) {
162 endpoint := fmt.Sprintf("/v4/domains/%s:disableAutorenew", request.DomainName)
163
164 post := &bytes.Buffer{}
165 err := json.NewEncoder(post).Encode(request)
166 if err != nil {
167 return nil, err
168 }
169
170 body, err := n.post(endpoint, post)
171 if err != nil {
172 return nil, err
173 }
174
175 resp := &Domain{}
176
177 err = json.NewDecoder(body).Decode(resp)
178 if err != nil {
179 return nil, err
180 }
181
182 return resp, nil
183 }
184
185 // RenewDomain will renew a domain. Purchase_price is required if the renewal is not regularly priced.
186 func (n *NameCom) RenewDomain(request *RenewDomainRequest) (*RenewDomainResponse, error) {
187 endpoint := fmt.Sprintf("/v4/domains/%s:renew", request.DomainName)
188
189 post := &bytes.Buffer{}
190 err := json.NewEncoder(post).Encode(request)
191 if err != nil {
192 return nil, err
193 }
194
195 body, err := n.post(endpoint, post)
196 if err != nil {
197 return nil, err
198 }
199
200 resp := &RenewDomainResponse{}
201
202 err = json.NewDecoder(body).Decode(resp)
203 if err != nil {
204 return nil, err
205 }
206
207 return resp, nil
208 }
209
210 // GetDomainPricing returns pricing of a taken or available domain.
211 func (n *NameCom) GetPricingForDomain(request *PricingRequest) (*PricingResponse, error) {
212 endpoint := fmt.Sprintf("/v4/domains/%s:getPricing", request.DomainName)
213
214 values := url.Values{}
215 if request.Years != 0 {
216 values.Set("years", fmt.Sprintf("%d", request.Years))
217 }
218
219 body, err := n.get(endpoint, values)
220 if err != nil {
221 return nil, err
222 }
223
224 resp := &PricingResponse{}
225
226 err = json.NewDecoder(body).Decode(resp)
227 if err != nil {
228 return nil, err
229 }
230
231 return resp, nil
232 }
233
234 // GetAuthCodeForDomain returns the Transfer Authorization Code for the domain.
235 func (n *NameCom) GetAuthCodeForDomain(request *AuthCodeRequest) (*AuthCodeResponse, error) {
236 endpoint := fmt.Sprintf("/v4/domains/%s:getAuthCode", request.DomainName)
237
238 values := url.Values{}
239
240 body, err := n.get(endpoint, values)
241 if err != nil {
242 return nil, err
243 }
244
245 resp := &AuthCodeResponse{}
246
247 err = json.NewDecoder(body).Decode(resp)
248 if err != nil {
249 return nil, err
250 }
251
252 return resp, nil
253 }
254
255 // PurchasePrivacy will add Whois Privacy protection to a domain or will an renew existing subscription.
256 func (n *NameCom) PurchasePrivacy(request *PrivacyRequest) (*PrivacyResponse, error) {
257 endpoint := fmt.Sprintf("/v4/domains/%s:purchasePrivacy", request.DomainName)
258
259 post := &bytes.Buffer{}
260 err := json.NewEncoder(post).Encode(request)
261 if err != nil {
262 return nil, err
263 }
264
265 body, err := n.post(endpoint, post)
266 if err != nil {
267 return nil, err
268 }
269
270 resp := &PrivacyResponse{}
271
272 err = json.NewDecoder(body).Decode(resp)
273 if err != nil {
274 return nil, err
275 }
276
277 return resp, nil
278 }
279
280 // SetNameservers will set the nameservers for the Domain.
281 func (n *NameCom) SetNameservers(request *SetNameserversRequest) (*Domain, error) {
282 endpoint := fmt.Sprintf("/v4/domains/%s:setNameservers", request.DomainName)
283
284 post := &bytes.Buffer{}
285 err := json.NewEncoder(post).Encode(request)
286 if err != nil {
287 return nil, err
288 }
289
290 body, err := n.post(endpoint, post)
291 if err != nil {
292 return nil, err
293 }
294
295 resp := &Domain{}
296
297 err = json.NewDecoder(body).Decode(resp)
298 if err != nil {
299 return nil, err
300 }
301
302 return resp, nil
303 }
304
305 // SetContacts will set the contacts for the Domain.
306 func (n *NameCom) SetContacts(request *SetContactsRequest) (*Domain, error) {
307 endpoint := fmt.Sprintf("/v4/domains/%s:setContacts", request.DomainName)
308
309 post := &bytes.Buffer{}
310 err := json.NewEncoder(post).Encode(request)
311 if err != nil {
312 return nil, err
313 }
314
315 body, err := n.post(endpoint, post)
316 if err != nil {
317 return nil, err
318 }
319
320 resp := &Domain{}
321
322 err = json.NewDecoder(body).Decode(resp)
323 if err != nil {
324 return nil, err
325 }
326
327 return resp, nil
328 }
329
330 // LockDomain will lock a domain so that it cannot be transferred to another registrar.
331 func (n *NameCom) LockDomain(request *LockDomainRequest) (*Domain, error) {
332 endpoint := fmt.Sprintf("/v4/domains/%s:lock", request.DomainName)
333
334 post := &bytes.Buffer{}
335 err := json.NewEncoder(post).Encode(request)
336 if err != nil {
337 return nil, err
338 }
339
340 body, err := n.post(endpoint, post)
341 if err != nil {
342 return nil, err
343 }
344
345 resp := &Domain{}
346
347 err = json.NewDecoder(body).Decode(resp)
348 if err != nil {
349 return nil, err
350 }
351
352 return resp, nil
353 }
354
355 // UnlockDomain will unlock a domain so that it can be transferred to another registrar.
356 func (n *NameCom) UnlockDomain(request *UnlockDomainRequest) (*Domain, error) {
357 endpoint := fmt.Sprintf("/v4/domains/%s:unlock", request.DomainName)
358
359 post := &bytes.Buffer{}
360 err := json.NewEncoder(post).Encode(request)
361 if err != nil {
362 return nil, err
363 }
364
365 body, err := n.post(endpoint, post)
366 if err != nil {
367 return nil, err
368 }
369
370 resp := &Domain{}
371
372 err = json.NewDecoder(body).Decode(resp)
373 if err != nil {
374 return nil, err
375 }
376
377 return resp, nil
378 }
379
380 // CheckAvailability will check a list of domains to see if they are purchasable. A Maximum of 50 domains can be specified.
381 func (n *NameCom) CheckAvailability(request *AvailabilityRequest) (*SearchResponse, error) {
382 endpoint := fmt.Sprintf("/v4/domains:checkAvailability")
383
384 post := &bytes.Buffer{}
385 err := json.NewEncoder(post).Encode(request)
386 if err != nil {
387 return nil, err
388 }
389
390 body, err := n.post(endpoint, post)
391 if err != nil {
392 return nil, err
393 }
394
395 resp := &SearchResponse{}
396
397 err = json.NewDecoder(body).Decode(resp)
398 if err != nil {
399 return nil, err
400 }
401
402 return resp, nil
403 }
404
405 // Search will perform a search for specified keywords.
406 func (n *NameCom) Search(request *SearchRequest) (*SearchResponse, error) {
407 endpoint := fmt.Sprintf("/v4/domains:search")
408
409 post := &bytes.Buffer{}
410 err := json.NewEncoder(post).Encode(request)
411 if err != nil {
412 return nil, err
413 }
414
415 body, err := n.post(endpoint, post)
416 if err != nil {
417 return nil, err
418 }
419
420 resp := &SearchResponse{}
421
422 err = json.NewDecoder(body).Decode(resp)
423 if err != nil {
424 return nil, err
425 }
426
427 return resp, nil
428 }
429
430 // SearchStream will return JSON encoded SearchResults as they are received from the registry. The SearchResults are separated by newlines. This can allow clients to react to results before the search is fully completed.
431 func (n *NameCom) SearchStream(request *SearchRequest) (*SearchResult, error) {
432 endpoint := fmt.Sprintf("/v4/domains:searchStream")
433
434 post := &bytes.Buffer{}
435 err := json.NewEncoder(post).Encode(request)
436 if err != nil {
437 return nil, err
438 }
439
440 body, err := n.post(endpoint, post)
441 if err != nil {
442 return nil, err
443 }
444
445 resp := &SearchResult{}
446
447 err = json.NewDecoder(body).Decode(resp)
448 if err != nil {
449 return nil, err
450 }
451
452 return resp, nil
453 }
454