@@ -3,11 +3,10 @@ package gapi
3
3
import (
4
4
"bytes"
5
5
"encoding/json"
6
- "errors"
7
6
"fmt"
8
- "io/ioutil"
9
7
)
10
8
9
+ // AlertNotification represents a Grafana alert notification.
11
10
type AlertNotification struct {
12
11
Id int64 `json:"id,omitempty"`
13
12
Uid string `json:"uid"`
@@ -20,122 +19,63 @@ type AlertNotification struct {
20
19
Settings interface {} `json:"settings"`
21
20
}
22
21
22
+ // AlertNotifications fetches and returns Grafana alert notifications.
23
23
func (c * Client ) AlertNotifications () ([]AlertNotification , error ) {
24
24
alertnotifications := make ([]AlertNotification , 0 )
25
25
26
- req , err := c .newRequest ("GET" , "/api/alert-notifications/" , nil , nil )
26
+ err := c .request ("GET" , "/api/alert-notifications/" , nil , nil , & alertnotifications )
27
27
if err != nil {
28
28
return nil , err
29
29
}
30
30
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 )
45
31
return alertnotifications , err
46
32
}
47
33
34
+ // AlertNotification fetches and returns a Grafana alert notification.
48
35
func (c * Client ) AlertNotification (id int64 ) (* AlertNotification , error ) {
49
36
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 )
64
39
if err != nil {
65
40
return nil , err
66
41
}
67
42
68
- result := & AlertNotification {}
69
- err = json .Unmarshal (data , & result )
70
43
return result , err
71
44
}
72
45
46
+ // NewAlertNotification creates a new Grafana alert notification.
73
47
func (c * Client ) NewAlertNotification (a * AlertNotification ) (int64 , error ) {
74
48
data , err := json .Marshal (a )
75
49
if err != nil {
76
50
return 0 , err
77
51
}
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
+ }{}
90
55
91
- data , err = ioutil . ReadAll ( resp . Body )
56
+ err = c . request ( "POST" , "/api/alert-notifications" , nil , bytes . NewBuffer ( data ), & result )
92
57
if err != nil {
93
58
return 0 , err
94
59
}
95
60
96
- result := struct {
97
- Id int64 `json:"id"`
98
- }{}
99
- err = json .Unmarshal (data , & result )
100
61
return result .Id , err
101
62
}
102
63
64
+ // UpdateAlertNotification updates a Grafana alert notification.
103
65
func (c * Client ) UpdateAlertNotification (a * AlertNotification ) error {
104
66
path := fmt .Sprintf ("/api/alert-notifications/%d" , a .Id )
105
67
data , err := json .Marshal (a )
106
68
if err != nil {
107
69
return err
108
70
}
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 )
113
72
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
123
74
}
124
75
76
+ // DeleteAlertNotification deletes a Grafana alert notification.
125
77
func (c * Client ) DeleteAlertNotification (id int64 ) error {
126
78
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
- }
139
79
140
- return nil
80
+ return c . request ( "DELETE" , path , nil , nil , nil )
141
81
}
0 commit comments