Debian Codegraph @

dnsimple/contacts.go

raw
1 package dnsimple
2
3 import (
4 "fmt"
5 "time"
6 )
7
8 // ContactsService handles communication with the contact related
9 // methods of the DNSimple API.
10 //
11 // DNSimple API docs: http://developer.dnsimple.com/contacts/
12 type ContactsService struct {
13 client *Client
14 }
15
16 type Contact struct {
17 Id int `json:"id,omitempty"`
18 Label string `json:"label,omitempty"`
19 FirstName string `json:"first_name,omitempty"`
20 LastName string `json:"last_name,omitempty"`
21 JobTitle string `json:"job_title,omitempty"`
22 Organization string `json:"organization_name,omitempty"`
23 Email string `json:"email_address,omitempty"`
24 Phone string `json:"phone,omitempty"`
25 Fax string `json:"fax,omitempty"`
26 Address1 string `json:"address1,omitempty"`
27 Address2 string `json:"address2,omitempty"`
28 City string `json:"city,omitempty"`
29 Zip string `json:"postal_code,omitempty"`
30 Country string `json:"country,omitempty"`
31 CreatedAt *time.Time `json:"created_at,omitempty"`
32 UpdatedAt *time.Time `json:"updated_at,omitempty"`
33 }
34
35 type contactWrapper struct {
36 Contact Contact `json:"contact"`
37 }
38
39 // contactPath generates the resource path for given contact.
40 func contactPath(contact interface{}) string {
41 if contact != nil {
42 return fmt.Sprintf("contacts/%d", contact)
43 }
44 return "contacts"
45 }
46
47 // List the contacts.
48 //
49 // DNSimple API docs: http://developer.dnsimple.com/contacts/#list
50 func (s *ContactsService) List() ([]Contact, *Response, error) {
51 path := contactPath(nil)
52 wrappedContacts := []contactWrapper{}
53
54 res, err := s.client.get(path, &wrappedContacts)
55 if err != nil {
56 return []Contact{}, res, err
57 }
58
59 contacts := []Contact{}
60 for _, contact := range wrappedContacts {
61 contacts = append(contacts, contact.Contact)
62 }
63
64 return contacts, res, nil
65 }
66
67 // Create a new contact.
68 //
69 // DNSimple API docs: http://developer.dnsimple.com/contacts/#create
70 func (s *ContactsService) Create(contactAttributes Contact) (Contact, *Response, error) {
71 path := contactPath(nil)
72 wrappedContact := contactWrapper{Contact: contactAttributes}
73 returnedContact := contactWrapper{}
74
75 res, err := s.client.post(path, wrappedContact, &returnedContact)
76 if err != nil {
77 return Contact{}, res, err
78 }
79
80 return returnedContact.Contact, res, nil
81 }
82
83 // Get fetches a contact.
84 //
85 // DNSimple API docs: http://developer.dnsimple.com/contacts/#get
86 func (s *ContactsService) Get(contactID int) (Contact, *Response, error) {
87 path := contactPath(contactID)
88 wrappedContact := contactWrapper{}
89
90 res, err := s.client.get(path, &wrappedContact)
91 if err != nil {
92 return Contact{}, res, err
93 }
94
95 return wrappedContact.Contact, res, nil
96 }
97
98 // Update a contact.
99 //
100 // DNSimple API docs: http://developer.dnsimple.com/contacts/#update
101 func (s *ContactsService) Update(contactID int, contactAttributes Contact) (Contact, *Response, error) {
102 path := contactPath(contactID)
103 wrappedContact := contactWrapper{Contact: contactAttributes}
104 returnedContact := contactWrapper{}
105
106 res, err := s.client.put(path, wrappedContact, &returnedContact)
107 if err != nil {
108 return Contact{}, res, err
109 }
110
111 return returnedContact.Contact, res, nil
112 }
113
114 // Delete a contact.
115 //
116 // DNSimple API docs: http://developer.dnsimple.com/contacts/#delete
117 func (s *ContactsService) Delete(contactID int) (*Response, error) {
118 path := contactPath(contactID)
119
120 return s.client.delete(path, nil)
121 }