Skip to content
This repository was archived by the owner on Mar 17, 2021. It is now read-only.

Commit 1ed4b99

Browse files
committed
Implemented alert notification management
1 parent d49f95c commit 1ed4b99

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed

alertnotification.go

+112
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package gapi
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io/ioutil"
9+
)
10+
11+
type AlertNotification struct {
12+
Id int64 `json:"id,omitempty"`
13+
Name string `json:"name"`
14+
Type string `json:"type"`
15+
IsDefault bool `json:"isDefault"`
16+
Settings interface{} `json:"settings"`
17+
}
18+
19+
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
20+
path := fmt.Sprintf("/api/alert-notifications/%d", id)
21+
req, err := c.newRequest("GET", path, nil)
22+
if err != nil {
23+
return nil, err
24+
}
25+
26+
resp, err := c.Do(req)
27+
if err != nil {
28+
return nil, err
29+
}
30+
if resp.StatusCode != 200 {
31+
return nil, errors.New(resp.Status)
32+
}
33+
34+
data, err := ioutil.ReadAll(resp.Body)
35+
if err != nil {
36+
return nil, err
37+
}
38+
39+
result := &AlertNotification{}
40+
err = json.Unmarshal(data, &result)
41+
return result, err
42+
}
43+
44+
func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
45+
data, err := json.Marshal(a)
46+
if err != nil {
47+
return 0, err
48+
}
49+
req, err := c.newRequest("POST", "/api/alert-notifications", bytes.NewBuffer(data))
50+
if err != nil {
51+
return 0, err
52+
}
53+
54+
resp, err := c.Do(req)
55+
if err != nil {
56+
return 0, err
57+
}
58+
if resp.StatusCode != 200 {
59+
return 0, errors.New(resp.Status)
60+
}
61+
62+
data, err = ioutil.ReadAll(resp.Body)
63+
if err != nil {
64+
return 0, err
65+
}
66+
67+
result := struct {
68+
Id int64 `json:"id"`
69+
}{}
70+
err = json.Unmarshal(data, &result)
71+
return result.Id, err
72+
}
73+
74+
func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
75+
path := fmt.Sprintf("/api/alert-notifications/%d", a.Id)
76+
data, err := json.Marshal(a)
77+
if err != nil {
78+
return err
79+
}
80+
req, err := c.newRequest("PUT", path, bytes.NewBuffer(data))
81+
if err != nil {
82+
return err
83+
}
84+
85+
resp, err := c.Do(req)
86+
if err != nil {
87+
return err
88+
}
89+
if resp.StatusCode != 200 {
90+
return errors.New(resp.Status)
91+
}
92+
93+
return nil
94+
}
95+
96+
func (c *Client) DeleteAlertNotification(id int64) error {
97+
path := fmt.Sprintf("/api/alert-notifications/%d", id)
98+
req, err := c.newRequest("DELETE", path, nil)
99+
if err != nil {
100+
return err
101+
}
102+
103+
resp, err := c.Do(req)
104+
if err != nil {
105+
return err
106+
}
107+
if resp.StatusCode != 200 {
108+
return errors.New(resp.Status)
109+
}
110+
111+
return nil
112+
}

0 commit comments

Comments
 (0)