Skip to content

Commit f88c7e8

Browse files
authored
Merge pull request grafana#51 from mdb/centralize-request-handling
2 parents 48cec1d + 0f467ed commit f88c7e8

14 files changed

+367
-858
lines changed

admin.go

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -3,54 +3,30 @@ package gapi
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
8-
"io/ioutil"
97
)
108

9+
// CreateUser creates a Grafana user.
1110
func (c *Client) CreateUser(user User) (int64, error) {
1211
id := int64(0)
1312
data, err := json.Marshal(user)
1413
if err != nil {
1514
return id, err
1615
}
1716

18-
req, err := c.newRequest("POST", "/api/admin/users", nil, bytes.NewBuffer(data))
19-
if err != nil {
20-
return id, err
21-
}
22-
resp, err := c.Do(req)
23-
if err != nil {
24-
return id, err
25-
}
26-
if resp.StatusCode != 200 {
27-
return id, errors.New(resp.Status)
28-
}
29-
data, err = ioutil.ReadAll(resp.Body)
30-
if err != nil {
31-
return id, err
32-
}
3317
created := struct {
3418
Id int64 `json:"id"`
3519
}{}
36-
err = json.Unmarshal(data, &created)
20+
21+
err = c.request("POST", "/api/admin/users", nil, bytes.NewBuffer(data), &created)
3722
if err != nil {
3823
return id, err
3924
}
25+
4026
return created.Id, err
4127
}
4228

29+
// DeleteUser deletes a Grafana user.
4330
func (c *Client) DeleteUser(id int64) error {
44-
req, err := c.newRequest("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil)
45-
if err != nil {
46-
return err
47-
}
48-
resp, err := c.Do(req)
49-
if err != nil {
50-
return err
51-
}
52-
if resp.StatusCode != 200 {
53-
return errors.New(resp.Status)
54-
}
55-
return err
31+
return c.request("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil, nil)
5632
}

alertnotification.go

Lines changed: 16 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@ package gapi
33
import (
44
"bytes"
55
"encoding/json"
6-
"errors"
76
"fmt"
8-
"io/ioutil"
97
)
108

9+
// AlertNotification represents a Grafana alert notification.
1110
type 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.
2323
func (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.
4835
func (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.
7347
func (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.
10365
func (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.
12577
func (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

Comments
 (0)