-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.go
211 lines (170 loc) · 4.38 KB
/
client.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package healthchecksio
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
)
const baseURL string = "https://healthchecks.io/api/v1"
// Client provides access to create, read, update and delete healthchecks.io resources
type Client struct {
APIKey string
BaseURL string
ContentType string
HTTPClient *http.Client
}
type apiResponse HealthcheckResponse
type apiListResponse struct {
Data []*HealthcheckResponse `json:"checks"`
}
type apiListChannelsResponse struct {
Data []*HealthcheckChannelResponse `json:"channels"`
}
type apiErrorResponse struct {
Message string `json:"error"`
}
// NewClient creates a new client
func NewClient(apiKey string) *Client {
return &Client{
APIKey: apiKey,
BaseURL: baseURL,
ContentType: "application/json",
HTTPClient: &http.Client{},
}
}
func (c *Client) request(method string, path string, reader io.Reader) ([]byte, error) {
url := c.BaseURL + path
req, _ := http.NewRequest(method, url, reader)
req.Header.Set("Content-Type", c.ContentType)
req.Header.Set("X-Api-Key", c.APIKey)
log.Printf("[DEBUG] HTTP %s %s", method, url)
resp, err := c.HTTPClient.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if resp.StatusCode >= 300 {
errorResp := new(apiErrorResponse)
if err = json.Unmarshal(body, &errorResp); err != nil {
return nil, fmt.Errorf("%s. %s %s >> %s", err, method, url, resp.Status)
}
return nil, fmt.Errorf("%s. %s %s >> %s", errorResp.Message, method, url, resp.Status)
}
return body, err
}
func (c *Client) get(path string) ([]byte, error) {
return c.request("GET", path, nil)
}
func (c *Client) post(path string, body io.Reader) ([]byte, error) {
return c.request("POST", path, body)
}
func (c *Client) delete(path string) ([]byte, error) {
return c.request("DELETE", path, nil)
}
// GetAll returns all healthchecks
func (c *Client) GetAll() ([]*HealthcheckResponse, error) {
body, err := c.get("/checks/")
if err != nil {
return nil, err
}
r, err := toAPIListResponse(body)
if err != nil {
return nil, err
}
resp := (*r).Data
return resp, nil
}
// Create creates a new healthcheck
func (c *Client) Create(check Healthcheck) (*HealthcheckResponse, error) {
json, err := check.ToJSON()
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte(json))
body, err := c.post("/checks/", buf)
if err != nil {
return nil, err
}
r, err := toAPIResponse(body)
if err != nil {
return nil, err
}
resp := HealthcheckResponse(*r)
return &resp, nil
}
// Update updates an existing healthcheck
func (c *Client) Update(id string, check Healthcheck) (*HealthcheckResponse, error) {
json, err := check.ToJSON()
if err != nil {
return nil, err
}
buf := bytes.NewBuffer([]byte(json))
body, err := c.post(fmt.Sprintf("/checks/%s", id), buf)
if err != nil {
return nil, err
}
r, err := toAPIResponse(body)
if err != nil {
return nil, err
}
resp := HealthcheckResponse(*r)
return &resp, nil
}
// Pause pauses monitoring on existing healthcheck
func (c *Client) Pause(id string) (*HealthcheckResponse, error) {
body, err := c.post(fmt.Sprintf("/checks/%s/pause", id), nil)
if err != nil {
return nil, err
}
r, err := toAPIResponse(body)
if err != nil {
return nil, err
}
resp := HealthcheckResponse(*r)
return &resp, nil
}
// Delete deletes an existing healthcheck
func (c *Client) Delete(id string) (*HealthcheckResponse, error) {
body, err := c.delete(fmt.Sprintf("/checks/%s", id))
if err != nil {
return nil, err
}
r, err := toAPIResponse(body)
if err != nil {
return nil, err
}
resp := HealthcheckResponse(*r)
return &resp, nil
}
// GetAllChannels returns all channels
func (c *Client) GetAllChannels() ([]*HealthcheckChannelResponse, error) {
body, err := c.get("/channels/")
if err != nil {
return nil, err
}
r, err := toAPIListChannelsResponse(body)
if err != nil {
return nil, err
}
resp := (*r).Data
return resp, nil
}
func toAPIListResponse(body []byte) (*apiListResponse, error) {
var s = new(apiListResponse)
err := json.Unmarshal(body, &s)
return s, err
}
func toAPIResponse(body []byte) (*apiResponse, error) {
var s = new(apiResponse)
err := json.Unmarshal(body, &s)
return s, err
}
func toAPIListChannelsResponse(body []byte) (*apiListChannelsResponse, error) {
var s = new(apiListChannelsResponse)
err := json.Unmarshal(body, &s)
return s, err
}