-
Notifications
You must be signed in to change notification settings - Fork 0
/
uptimerobot.go
72 lines (60 loc) · 1.54 KB
/
uptimerobot.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
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
)
const UPTIMEROBOT_API_URL = "https://api.uptimerobot.com/v2"
type GetMetricsResponse struct {
Stat string `json:"stat"`
Pagination Pagination `json:"pagination"`
Monitors []Monitor `json:"monitors"`
}
type Pagination struct {
Limit int `json:"limit"`
Offset int `json:"offset"`
Total int `json:"total"`
}
type Monitor struct {
ID int `json:"id"`
FriendlyName string `json:"friendly_name"`
URL string `json:"url"`
Type int `json:"type"`
Status int `json:"status"`
}
// UptimerobotClient is a client for Uptimerobot API
type UptimerobotClient struct {
APIKey string
Client *http.Client
}
// NewUptimerobotClient returns a new UptimerobotClient
func NewUptimerobotClient(apiKey string) *UptimerobotClient {
return &UptimerobotClient{
APIKey: apiKey,
Client: &http.Client{},
}
}
// GetMonitors returns a list of monitors
func (c *UptimerobotClient) GetMonitors() ([]Monitor, error) {
url := fmt.Sprintf("%s/getMonitors?api_key=%s&format=json", UPTIMEROBOT_API_URL, c.APIKey)
req, err := http.NewRequest(http.MethodPost, url, nil)
if err != nil {
return nil, err
}
res, err := c.Client.Do(req)
if err != nil {
return nil, err
}
defer res.Body.Close()
if res.StatusCode != http.StatusOK {
b, _ := io.ReadAll(res.Body)
return nil, fmt.Errorf(string(b))
}
var decoded GetMetricsResponse
err = json.NewDecoder(res.Body).Decode(&decoded)
if err != nil {
return nil, err
}
return decoded.Monitors, nil
}