File: contacts.go

package info (click to toggle)
golang-github-weppos-dnsimple-go 0.0~git20160204.0.65c1ca7-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 144 kB
  • ctags: 147
  • sloc: makefile: 2
file content (121 lines) | stat: -rw-r--r-- 3,453 bytes parent folder | download | duplicates (2)
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
package dnsimple

import (
	"fmt"
	"time"
)

// ContactsService handles communication with the contact related
// methods of the DNSimple API.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/
type ContactsService struct {
	client *Client
}

type Contact struct {
	Id           int        `json:"id,omitempty"`
	Label        string     `json:"label,omitempty"`
	FirstName    string     `json:"first_name,omitempty"`
	LastName     string     `json:"last_name,omitempty"`
	JobTitle     string     `json:"job_title,omitempty"`
	Organization string     `json:"organization_name,omitempty"`
	Email        string     `json:"email_address,omitempty"`
	Phone        string     `json:"phone,omitempty"`
	Fax          string     `json:"fax,omitempty"`
	Address1     string     `json:"address1,omitempty"`
	Address2     string     `json:"address2,omitempty"`
	City         string     `json:"city,omitempty"`
	Zip          string     `json:"postal_code,omitempty"`
	Country      string     `json:"country,omitempty"`
	CreatedAt    *time.Time `json:"created_at,omitempty"`
	UpdatedAt    *time.Time `json:"updated_at,omitempty"`
}

type contactWrapper struct {
	Contact Contact `json:"contact"`
}

// contactPath generates the resource path for given contact.
func contactPath(contact interface{}) string {
	if contact != nil {
		return fmt.Sprintf("contacts/%d", contact)
	}
	return "contacts"
}

// List the contacts.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/#list
func (s *ContactsService) List() ([]Contact, *Response, error) {
	path := contactPath(nil)
	wrappedContacts := []contactWrapper{}

	res, err := s.client.get(path, &wrappedContacts)
	if err != nil {
		return []Contact{}, res, err
	}

	contacts := []Contact{}
	for _, contact := range wrappedContacts {
		contacts = append(contacts, contact.Contact)
	}

	return contacts, res, nil
}

// Create a new contact.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/#create
func (s *ContactsService) Create(contactAttributes Contact) (Contact, *Response, error) {
	path := contactPath(nil)
	wrappedContact := contactWrapper{Contact: contactAttributes}
	returnedContact := contactWrapper{}

	res, err := s.client.post(path, wrappedContact, &returnedContact)
	if err != nil {
		return Contact{}, res, err
	}

	return returnedContact.Contact, res, nil
}

// Get fetches a contact.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/#get
func (s *ContactsService) Get(contactID int) (Contact, *Response, error) {
	path := contactPath(contactID)
	wrappedContact := contactWrapper{}

	res, err := s.client.get(path, &wrappedContact)
	if err != nil {
		return Contact{}, res, err
	}

	return wrappedContact.Contact, res, nil
}

// Update a contact.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/#update
func (s *ContactsService) Update(contactID int, contactAttributes Contact) (Contact, *Response, error) {
	path := contactPath(contactID)
	wrappedContact := contactWrapper{Contact: contactAttributes}
	returnedContact := contactWrapper{}

	res, err := s.client.put(path, wrappedContact, &returnedContact)
	if err != nil {
		return Contact{}, res, err
	}

	return returnedContact.Contact, res, nil
}

// Delete a contact.
//
// DNSimple API docs: http://developer.dnsimple.com/contacts/#delete
func (s *ContactsService) Delete(contactID int) (*Response, error) {
	path := contactPath(contactID)

	return s.client.delete(path, nil)
}