Debian Codegraph @

dnsimple/domains.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 // DomainsService handles communication with the domain related
9 // methods of the DNSimple API.
10 //
11 // DNSimple API docs: http://developer.dnsimple.com/domains/
12 type DomainsService struct {
13 client *Client
14 }
15
16 type Domain struct {
17 Id int `json:"id,omitempty"`
18 UserId int `json:"user_id,omitempty"`
19 RegistrantId int `json:"registrant_id,omitempty"`
20 Name string `json:"name,omitempty"`
21 UnicodeName string `json:"unicode_name,omitempty"`
22 Token string `json:"token,omitempty"`
23 State string `json:"state,omitempty"`
24 Language string `json:"language,omitempty"`
25 Lockable bool `json:"lockable,omitempty"`
26 AutoRenew bool `json:"auto_renew,omitempty"`
27 WhoisProtected bool `json:"whois_protected,omitempty"`
28 RecordCount int `json:"record_count,omitempty"`
29 ServiceCount int `json:"service_count,omitempty"`
30 ExpiresOn *Date `json:"expires_on,omitempty"`
31 CreatedAt *time.Time `json:"created_at,omitempty"`
32 UpdatedAt *time.Time `json:"updated_at,omitempty"`
33 }
34
35 type domainWrapper struct {
36 Domain Domain `json:"domain"`
37 }
38
39 // domainRequest represents a generic wrapper for a domain request,
40 // when domainWrapper cannot be used because of type constraint on Domain.
41 type domainRequest struct {
42 Domain interface{} `json:"domain"`
43 }
44
45 func domainIdentifier(value interface{}) string {
46 switch value := value.(type) {
47 case string:
48 return value
49 case int:
50 return fmt.Sprintf("%d", value)
51 }
52 return ""
53 }
54
55 // domainPath generates the resource path for given domain.
56 func domainPath(domain interface{}) string {
57 if domain != nil {
58 return fmt.Sprintf("domains/%s", domainIdentifier(domain))
59 }
60 return "domains"
61 }
62
63 // List the domains.
64 //
65 // DNSimple API docs: http://developer.dnsimple.com/domains/#list
66 func (s *DomainsService) List() ([]Domain, *Response, error) {
67 path := domainPath(nil)
68 returnedDomains := []domainWrapper{}
69
70 res, err := s.client.get(path, &returnedDomains)
71 if err != nil {
72 return []Domain{}, res, err
73 }
74
75 domains := []Domain{}
76 for _, domain := range returnedDomains {
77 domains = append(domains, domain.Domain)
78 }
79
80 return domains, res, nil
81 }
82
83 // Create a new domain.
84 //
85 // DNSimple API docs: http://developer.dnsimple.com/domains/#create
86 func (s *DomainsService) Create(domainAttributes Domain) (Domain, *Response, error) {
87 path := domainPath(nil)
88 wrappedDomain := domainWrapper{Domain: domainAttributes}
89 returnedDomain := domainWrapper{}
90
91 res, err := s.client.post(path, wrappedDomain, &returnedDomain)
92 if err != nil {
93 return Domain{}, res, err
94 }
95
96 return returnedDomain.Domain, res, nil
97 }
98
99 // Get fetches a domain.
100 //
101 // DNSimple API docs: http://developer.dnsimple.com/domains/#get
102 func (s *DomainsService) Get(domain interface{}) (Domain, *Response, error) {
103 path := domainPath(domain)
104 returnedDomain := domainWrapper{}
105
106 res, err := s.client.get(path, &returnedDomain)
107 if err != nil {
108 return Domain{}, res, err
109 }
110
111 return returnedDomain.Domain, res, nil
112 }
113
114 // Delete a domain.
115 //
116 // DNSimple API docs: http://developer.dnsimple.com/domains/#delete
117 func (s *DomainsService) Delete(domain interface{}) (*Response, error) {
118 path := domainPath(domain)
119
120 return s.client.delete(path, nil)
121 }