forked from nordcloud/go-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
contact.go
116 lines (95 loc) · 2.52 KB
/
contact.go
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
package pingdom
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
// ContactService provides an interface to Pingdom contacts.
type ContactService struct {
client *Client
}
// ContactAPI is an interface representing a Pingdom Contact.
type ContactAPI interface {
RenderForJSONAPI() string
ValidContact() error
}
// List returns a list of all contacts and their contact details.
func (cs *ContactService) List() ([]Contact, error) {
req, err := cs.client.NewRequest("GET", "/alerting/contacts", nil)
if err != nil {
return nil, err
}
resp, err := cs.client.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := validateResponse(resp); err != nil {
return nil, err
}
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
u := &listContactsJSONResponse{}
err = json.Unmarshal([]byte(bodyString), &u)
return u.Contacts, err
}
// Read return a contact object from Pingdom.
func (cs *ContactService) Read(contactID int) (*Contact, error) {
req, err := cs.client.NewRequest("GET", "/alerting/contacts/"+strconv.Itoa(contactID), nil)
if err != nil {
return nil, err
}
c := &contactDetailsJSONResponse{}
_, err = cs.client.Do(req, c)
if err != nil {
return nil, err
}
return c.Contact, nil
}
// Create adds a new contact.
func (cs *ContactService) Create(contact ContactAPI) (*Contact, error) {
if err := contact.ValidContact(); err != nil {
return nil, err
}
req, err := cs.client.NewJSONRequest("POST", "/alerting/contacts", contact.RenderForJSONAPI())
if err != nil {
return nil, err
}
m := &createContactJSONResponse{}
_, err = cs.client.Do(req, m)
if err != nil {
fmt.Println(err)
return nil, err
}
return m.Contact, err
}
// Update a contact's core properties not contact targets.
func (cs *ContactService) Update(id int, contact ContactAPI) (*PingdomResponse, error) {
if err := contact.ValidContact(); err != nil {
return nil, err
}
req, err := cs.client.NewJSONRequest("PUT", "/alerting/contacts/"+strconv.Itoa(id), contact.RenderForJSONAPI())
if err != nil {
return nil, err
}
m := &PingdomResponse{}
_, err = cs.client.Do(req, m)
if err != nil {
return nil, err
}
return m, err
}
// Delete removes a contact from Pingdom.
func (cs *ContactService) Delete(id int) (*PingdomResponse, error) {
req, err := cs.client.NewRequest("DELETE", "/alerting/contacts/"+strconv.Itoa(id), nil)
if err != nil {
return nil, err
}
m := &PingdomResponse{}
_, err = cs.client.Do(req, m)
if err != nil {
return nil, err
}
return m, err
}