-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronitor.go
176 lines (133 loc) · 3.36 KB
/
cronitor.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
package cronitor
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
)
const apiEndpoint = "https://cronitor.io/api"
const apiVersion = "2020-09-01"
type Cronitor struct {
ApiKey string
}
func (c Cronitor) PutMonitors(monitors []Monitor) error {
postData, err := json.Marshal(MonitorRequest{
Monitors: monitors,
})
if err != nil {
return err
}
request, err := http.NewRequest(http.MethodPut, apiEndpoint+"/monitors", bytes.NewReader(postData))
if err != nil {
return err
}
request.Header.Set("Content-Type", "application/json")
resp, err := c.sendHttpRequest(request)
if err != nil {
return err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 200 {
return &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Sprintf("failed to put monitor. response: %s", respBody),
}
}
return nil
}
func (c Cronitor) DeleteMonitor(key string) error {
request, err := http.NewRequest(http.MethodDelete, fmt.Sprintf("%s/monitors/%s", apiEndpoint, key), nil)
if err != nil {
return err
}
resp, err := c.sendHttpRequest(request)
if err != nil {
return err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != 204 {
return &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Sprintf("failed to delete monitor: %s. response: %s", key, respBody),
}
}
return nil
}
func (c Cronitor) GetMonitor(key string) (*Monitor, error) {
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/monitors/%s", apiEndpoint, key), nil)
if err != nil {
return nil, err
}
resp, err := c.sendHttpRequest(request)
if err != nil {
return nil, err
}
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Sprintf("failed to get monitor: %s. response: %s", key, respBody),
}
}
var monitor *Monitor
err = json.Unmarshal(respBody, &monitor)
if err != nil {
return nil, &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Errorf("failed to unmarshal response: %w", err).Error(),
}
}
return monitor, nil
}
func (c Cronitor) PauseMonitor(key string) error {
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/monitors/%s/pause", apiEndpoint, key), nil)
if err != nil {
return err
}
resp, err := c.sendHttpRequest(request)
if err != nil {
return err
}
if resp != nil && resp.StatusCode != 200 {
return &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Sprintf("Failed to pause monitor: %s .", key),
}
}
return nil
}
func (c Cronitor) UnpauseMonitor(key string) error {
request, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/monitors/%s/pause/0", apiEndpoint, key), nil)
if err != nil {
return err
}
resp, err := c.sendHttpRequest(request)
if err != nil {
return err
}
if resp != nil && resp.StatusCode != 200 {
return &StatusError{
StatusCode: resp.StatusCode,
message: fmt.Sprintf("Failed to unpause monitor: %s .", key),
}
}
return nil
}
func (c Cronitor) sendHttpRequest(request *http.Request) (*http.Response, error) {
if c.ApiKey == "" {
return nil, fmt.Errorf("API key cannot be empty")
}
request.SetBasicAuth(c.ApiKey, "")
request.Header.Set("Cronitor-Version", apiVersion)
return http.DefaultClient.Do(request)
}