Debian Codegraph @

dnsimple/registrar.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 )
6
7 // RegistrarService handles communication with the registrar related
8 // methods of the DNSimple API.
9 //
10 // DNSimple API docs: http://developer.dnsimple.com/registrar/
11 type RegistrarService struct {
12 client *Client
13 }
14
15 // ExtendedAttributes maps the additional attributes required by some registries.
16 type ExtendedAttributes map[string]string
17
18 // ExtendedAttributes represents the transfer information.
19 type TransferOrder struct {
20 AuthCode string `json:"authinfo,omitempty"`
21 }
22
23 // registrationRequest represents the body of a register or transfer request.
24 type registrationRequest struct {
25 Domain Domain `json:"domain"`
26 ExtendedAttributes *ExtendedAttributes `json:"extended_attribute,omitempty"`
27 TransferOrder *TransferOrder `json:"transfer_order,omitempty"`
28 }
29
30 // IsAvailable checks if the domain is available or registered.
31 //
32 // See: http://developer.dnsimple.com/registrar/#check
33 func (s *RegistrarService) IsAvailable(domain string) (bool, error) {
34 path := fmt.Sprintf("%s/check", domainPath(domain))
35
36 res, err := s.client.get(path, nil)
37 if err != nil && res != nil && res.StatusCode != 404 {
38 return false, err
39 }
40
41 return res.StatusCode == 404, nil
42 }
43
44 // Register a domain.
45 //
46 // DNSimple API docs: http://developer.dnsimple.com/registrar/#register
47 func (s *RegistrarService) Register(domain string, registrantID int, extendedAttributes *ExtendedAttributes) (Domain, *Response, error) {
48 request := registrationRequest{
49 Domain: Domain{Name: domain, RegistrantId: registrantID},
50 ExtendedAttributes: extendedAttributes,
51 }
52 returnedDomain := domainWrapper{}
53
54 res, err := s.client.post("domain_registrations", request, &returnedDomain)
55 if err != nil {
56 return Domain{}, res, err
57 }
58
59 return returnedDomain.Domain, res, nil
60 }
61
62 // Transfer a domain.
63 //
64 // DNSimple API docs: http://developer.dnsimple.com/registrar/#transfer
65 func (s *RegistrarService) Transfer(domain string, registrantID int, authCode string, extendedAttributes *ExtendedAttributes) (Domain, *Response, error) {
66 request := registrationRequest{
67 Domain: Domain{Name: domain, RegistrantId: registrantID},
68 ExtendedAttributes: extendedAttributes,
69 TransferOrder: &TransferOrder{AuthCode: authCode},
70 }
71 returnedDomain := domainWrapper{}
72
73 res, err := s.client.post("domain_transfers", request, &returnedDomain)
74 if err != nil {
75 return Domain{}, res, err
76 }
77
78 return returnedDomain.Domain, res, nil
79 }
80
81 // renewDomain represents the body of a Renew request.
82 type renewDomain struct {
83 Name string `json:"name,omitempty"`
84 RenewWhoisPrivacy bool `json:"renew_whois_privacy,omitempty"`
85 }
86
87 // Renew the domain, optionally renewing WHOIS privacy service.
88 //
89 // DNSimple API docs: http://developer.dnsimple.com/registrar/#renew
90 func (s *RegistrarService) Renew(domain string, renewWhoisPrivacy bool) (Domain, *Response, error) {
91 request := domainRequest{Domain: renewDomain{
92 Name: domain,
93 RenewWhoisPrivacy: renewWhoisPrivacy,
94 }}
95 returnedDomain := domainWrapper{}
96
97 res, err := s.client.post("domain_renewals", request, &returnedDomain)
98 if err != nil {
99 return Domain{}, res, err
100 }
101
102 return returnedDomain.Domain, res, nil
103 }
104
105 // EnableAutoRenewal enables the auto-renewal feature for the domain.
106 //
107 // DNSimple API docs: http://developer.dnsimple.com/registrar/autorenewal/#enable
108 func (s *RegistrarService) EnableAutoRenewal(domain interface{}) (*Response, error) {
109 path := fmt.Sprintf("%s/auto_renewal", domainPath(domain))
110
111 res, err := s.client.post(path, nil, nil)
112 if err != nil {
113 return res, err
114 }
115
116 return res, nil
117 }
118
119 // DisableAutoRenewal disables the auto-renewal feature for the domain.
120 //
121 // DNSimple API docs: http://developer.dnsimple.com/registrar/autorenewal/#disable
122 func (s *RegistrarService) DisableAutoRenewal(domain interface{}) (*Response, error) {
123 path := fmt.Sprintf("%s/auto_renewal", domainPath(domain))
124
125 res, err := s.client.delete(path, nil)
126 if err != nil {
127 return res, err
128 }
129
130 return res, nil
131 }