@@ -3,11 +3,10 @@ package gapi
33import (
44 "bytes"
55 "encoding/json"
6- "errors"
76 "fmt"
8- "io/ioutil"
97)
108
9+ // AlertNotification represents a Grafana alert notification.
1110type AlertNotification struct {
1211 Id int64 `json:"id,omitempty"`
1312 Uid string `json:"uid"`
@@ -20,122 +19,63 @@ type AlertNotification struct {
2019 Settings interface {} `json:"settings"`
2120}
2221
22+ // AlertNotifications fetches and returns Grafana alert notifications.
2323func (c * Client ) AlertNotifications () ([]AlertNotification , error ) {
2424 alertnotifications := make ([]AlertNotification , 0 )
2525
26- req , err := c .newRequest ("GET" , "/api/alert-notifications/" , nil , nil )
26+ err := c .request ("GET" , "/api/alert-notifications/" , nil , nil , & alertnotifications )
2727 if err != nil {
2828 return nil , err
2929 }
3030
31- resp , err := c .Do (req )
32- if err != nil {
33- return nil , err
34- }
35- if resp .StatusCode != 200 {
36- return nil , errors .New (resp .Status )
37- }
38-
39- data , err := ioutil .ReadAll (resp .Body )
40- if err != nil {
41- return nil , err
42- }
43-
44- err = json .Unmarshal (data , & alertnotifications )
4531 return alertnotifications , err
4632}
4733
34+ // AlertNotification fetches and returns a Grafana alert notification.
4835func (c * Client ) AlertNotification (id int64 ) (* AlertNotification , error ) {
4936 path := fmt .Sprintf ("/api/alert-notifications/%d" , id )
50- req , err := c .newRequest ("GET" , path , nil , nil )
51- if err != nil {
52- return nil , err
53- }
54-
55- resp , err := c .Do (req )
56- if err != nil {
57- return nil , err
58- }
59- if resp .StatusCode != 200 {
60- return nil , errors .New (resp .Status )
61- }
62-
63- data , err := ioutil .ReadAll (resp .Body )
37+ result := & AlertNotification {}
38+ err := c .request ("GET" , path , nil , nil , result )
6439 if err != nil {
6540 return nil , err
6641 }
6742
68- result := & AlertNotification {}
69- err = json .Unmarshal (data , & result )
7043 return result , err
7144}
7245
46+ // NewAlertNotification creates a new Grafana alert notification.
7347func (c * Client ) NewAlertNotification (a * AlertNotification ) (int64 , error ) {
7448 data , err := json .Marshal (a )
7549 if err != nil {
7650 return 0 , err
7751 }
78- req , err := c .newRequest ("POST" , "/api/alert-notifications" , nil , bytes .NewBuffer (data ))
79- if err != nil {
80- return 0 , err
81- }
82-
83- resp , err := c .Do (req )
84- if err != nil {
85- return 0 , err
86- }
87- if resp .StatusCode != 200 {
88- return 0 , errors .New (resp .Status )
89- }
52+ result := struct {
53+ Id int64 `json:"id"`
54+ }{}
9055
91- data , err = ioutil . ReadAll ( resp . Body )
56+ err = c . request ( "POST" , "/api/alert-notifications" , nil , bytes . NewBuffer ( data ), & result )
9257 if err != nil {
9358 return 0 , err
9459 }
9560
96- result := struct {
97- Id int64 `json:"id"`
98- }{}
99- err = json .Unmarshal (data , & result )
10061 return result .Id , err
10162}
10263
64+ // UpdateAlertNotification updates a Grafana alert notification.
10365func (c * Client ) UpdateAlertNotification (a * AlertNotification ) error {
10466 path := fmt .Sprintf ("/api/alert-notifications/%d" , a .Id )
10567 data , err := json .Marshal (a )
10668 if err != nil {
10769 return err
10870 }
109- req , err := c .newRequest ("PUT" , path , nil , bytes .NewBuffer (data ))
110- if err != nil {
111- return err
112- }
71+ err = c .request ("PUT" , path , nil , bytes .NewBuffer (data ), nil )
11372
114- resp , err := c .Do (req )
115- if err != nil {
116- return err
117- }
118- if resp .StatusCode != 200 {
119- return errors .New (resp .Status )
120- }
121-
122- return nil
73+ return err
12374}
12475
76+ // DeleteAlertNotification deletes a Grafana alert notification.
12577func (c * Client ) DeleteAlertNotification (id int64 ) error {
12678 path := fmt .Sprintf ("/api/alert-notifications/%d" , id )
127- req , err := c .newRequest ("DELETE" , path , nil , nil )
128- if err != nil {
129- return err
130- }
131-
132- resp , err := c .Do (req )
133- if err != nil {
134- return err
135- }
136- if resp .StatusCode != 200 {
137- return errors .New (resp .Status )
138- }
13979
140- return nil
80+ return c . request ( "DELETE" , path , nil , nil , nil )
14181}
0 commit comments