forked from nordcloud/go-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pingdom.go
188 lines (162 loc) · 4.83 KB
/
pingdom.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package pingdom
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"os"
"strings"
)
const (
defaultBaseURL = "https://api.pingdom.com/api/3.1"
)
// Client represents a client to the Pingdom API.
type Client struct {
APIToken string
BaseURL *url.URL
client *http.Client
Checks *CheckService
Contacts *ContactService
Maintenances *MaintenanceService
Occurrences *OccurrenceService
Probes *ProbeService
Teams *TeamService
TMSCheck *TMSCheckService
}
// ClientConfig represents a configuration for a pingdom client.
type ClientConfig struct {
APIToken string
BaseURL string
HTTPClient *http.Client
}
// NewClientWithConfig returns a Pingdom client.
func NewClientWithConfig(config ClientConfig) (*Client, error) {
var baseURL *url.URL
var err error
if config.BaseURL != "" {
baseURL, err = url.Parse(config.BaseURL)
} else {
baseURL, err = url.Parse(defaultBaseURL)
}
if err != nil {
return nil, err
}
c := &Client{
BaseURL: baseURL,
}
if config.APIToken == "" {
if envAPIToken, set := os.LookupEnv("PINGDOM_API_TOKEN"); set {
c.APIToken = envAPIToken
}
} else {
c.APIToken = config.APIToken
}
if config.HTTPClient != nil {
c.client = config.HTTPClient
} else {
c.client = http.DefaultClient
}
c.Checks = &CheckService{client: c}
c.Contacts = &ContactService{client: c}
c.Maintenances = &MaintenanceService{client: c}
c.Occurrences = &OccurrenceService{client: c}
c.Probes = &ProbeService{client: c}
c.Teams = &TeamService{client: c}
c.TMSCheck = &TMSCheckService{client: c}
return c, nil
}
// NewRequest makes a new HTTP Request. The method param should be an HTTP method in
// all caps such as GET, POST, PUT, DELETE. The rsc param should correspond with
// a restful resource. Params can be passed in as a map of strings
// Usually users of the client can use one of the convenience methods such as
// ListChecks, etc but this method is provided to allow for making other
// API calls that might not be built in.
func (pc *Client) NewRequest(method string, rsc string, params map[string]string) (*http.Request, error) {
baseURL, err := url.Parse(pc.BaseURL.String() + rsc)
if err != nil {
return nil, err
}
if params != nil {
ps := url.Values{}
for k, v := range params {
ps.Set(k, v)
}
baseURL.RawQuery = ps.Encode()
}
req, err := http.NewRequest(method, baseURL.String(), nil)
req.Header.Add("Authorization", "Bearer "+pc.APIToken)
return req, err
}
func (pc *Client) NewRequestMultiParamValue(method string, rsc string, params map[string][]string) (*http.Request, error) {
baseURL, err := url.Parse(pc.BaseURL.String() + rsc)
if err != nil {
return nil, err
}
if params != nil {
ps := url.Values{}
for k, mv := range params {
for _, v := range mv {
ps.Add(k, v)
}
}
baseURL.RawQuery = ps.Encode()
}
req, err := http.NewRequest(method, baseURL.String(), nil)
req.Header.Add("Authorization", "Bearer "+pc.APIToken)
return req, err
}
// NewJSONRequest makes a new HTTP Request. The method param should be an HTTP method in
// all caps such as GET, POST, PUT, DELETE. The rsc param should correspond with
// a restful resource. Params should be a json formatted string.
func (pc *Client) NewJSONRequest(method string, rsc string, params string) (*http.Request, error) {
baseURL, err := url.Parse(pc.BaseURL.String() + rsc)
if err != nil {
return nil, err
}
reqBody := strings.NewReader(params)
req, err := http.NewRequest(method, baseURL.String(), reqBody)
req.Header.Add("Authorization", "Bearer "+pc.APIToken)
req.Header.Add("Content-Type", "application/json")
return req, err
}
// Do makes an HTTP request and will unmarshal the JSON response in to the
// passed in interface. If the HTTP response is outside of the 2xx range the
// response will be returned along with the error.
func (pc *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
resp, err := pc.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := validateResponse(resp); err != nil {
return resp, err
}
err = decodeResponse(resp, v)
return resp, err
}
func decodeResponse(r *http.Response, v interface{}) error {
if v == nil {
return fmt.Errorf("nil interface provided to decodeResponse")
}
bodyBytes, _ := ioutil.ReadAll(r.Body)
bodyString := string(bodyBytes)
err := json.Unmarshal([]byte(bodyString), &v)
return err
}
// Takes an HTTP response and determines whether it was successful.
// Returns nil if the HTTP status code is within the 2xx range. Returns
// an error otherwise.
func validateResponse(r *http.Response) error {
if c := r.StatusCode; 200 <= c && c <= 299 {
return nil
}
bodyBytes, _ := ioutil.ReadAll(r.Body)
bodyString := string(bodyBytes)
m := &errorJSONResponse{}
err := json.Unmarshal([]byte(bodyString), &m)
if err != nil {
return err
}
return m.Error
}