forked from yagilm/pingdom2stats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
checkname.go
54 lines (51 loc) · 1.3 KB
/
checkname.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
// CheckNameResponse describes the response of
// https://api.pingdom.com/api/2.0/checks/$checkid
type CheckNameResponse struct {
Check struct {
ID int `json:"id"`
Name string `json:"name"`
} `json:"check"`
}
// getCheckName extracts the name of a checkid
// replacing spaces with _
func getCheckName() string {
req, err := http.NewRequest("GET",
fmt.Sprintf(
"https://api.pingdom.com/api/2.0/checks/%s",
Config.checkid), nil)
if err != nil {
panic(err.Error())
}
req.SetBasicAuth(Config.usermail, Config.pass)
req.Header.Set("app-key", Config.headerXappkey)
req.Header.Set("Content-Type", "application/json")
res, err := http.DefaultClient.Do(req)
if err != nil {
panic(err.Error())
}
if res.StatusCode != http.StatusOK {
panic(err.Error())
}
body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err.Error())
}
var responseName CheckNameResponse
err = json.Unmarshal(body, &responseName)
if err != nil {
panic(err.Error())
}
checkidName := strings.Replace(responseName.Check.Name, " ", "_", -1)
checkidName = strings.Replace(checkidName, "/", "_", -1)
checkidName = strings.Replace(checkidName, ".", "_", -1)
checkidName = strings.Replace(checkidName, "__", "_", -1)
return checkidName
}