forked from nordcloud/go-pingdom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
maintenance_occurrence.go
112 lines (94 loc) · 2.57 KB
/
maintenance_occurrence.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
package pingdom
import (
"encoding/json"
"fmt"
"io/ioutil"
"strconv"
)
type OccurrenceService struct {
client *Client
}
func (os *OccurrenceService) List(query ListOccurrenceQuery) ([]Occurrence, error) {
params := query.toParams()
req, err := os.client.NewRequest("GET", "/maintenance.occurrences", params)
if err != nil {
return nil, err
}
resp, err := os.client.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
if err := validateResponse(resp); err != nil {
return nil, err
}
bodyBytes, _ := ioutil.ReadAll(resp.Body)
bodyString := string(bodyBytes)
m := &listOccurrenceResponse{}
err = json.Unmarshal([]byte(bodyString), &m)
return m.Occurrences, err
}
func (os *OccurrenceService) Read(id int64) (*Occurrence, error) {
req, err := os.client.NewRequest("GET", "/maintenance.occurrences/"+strconv.FormatInt(id, 10), nil)
if err != nil {
return nil, err
}
t := &readOccurrenceResponse{}
_, err = os.client.Do(req, t)
if err != nil {
return nil, err
}
return &t.Occurrence, err
}
// Update is used to update an existing Occurrence. Only the 'From',
// and 'To' fields can be updated.
func (os *OccurrenceService) Update(id int64, occurrence Occurrence) (*PingdomResponse, error) {
if err := occurrence.Valid(); err != nil {
return nil, err
}
req, err := os.client.NewJSONRequest("PUT", "/maintenance.occurrences/"+strconv.FormatInt(id, 10), occurrence.RenderForJSONAPI())
if err != nil {
return nil, err
}
m := &PingdomResponse{}
_, err = os.client.Do(req, m)
if err != nil {
return nil, err
}
return m, err
}
// MultiDelete will delete the Occurrence for the given ID.
func (os *OccurrenceService) MultiDelete(ids []int64) (*PingdomResponse, error) {
if len(ids) == 0 {
return nil, fmt.Errorf("empty id list for multiple occurrence delete")
}
strIds := make([]string, 0, len(ids))
for _, id := range ids {
strIds = append(strIds, strconv.FormatInt(id, 10))
}
req, err := os.client.NewRequestMultiParamValue("DELETE", "/maintenance.occurrences", map[string][]string{
"occurrenceids": strIds,
})
if err != nil {
return nil, err
}
m := &PingdomResponse{}
_, err = os.client.Do(req, m)
if err != nil {
return nil, err
}
return m, err
}
// Delete will delete the Occurrence for the given ID.
func (os *OccurrenceService) Delete(id int64) (*PingdomResponse, error) {
req, err := os.client.NewRequest("DELETE", "/maintenance.occurrences/"+strconv.FormatInt(id, 10), nil)
if err != nil {
return nil, err
}
m := &PingdomResponse{}
_, err = os.client.Do(req, m)
if err != nil {
return nil, err
}
return m, err
}