Skip to content

Commit

Permalink
DRY up JSON unmarshaling to central location
Browse files Browse the repository at this point in the history
Addresses issue nytm#52
  • Loading branch information
mdb committed Jun 6, 2020
1 parent 8073810 commit ee181ad
Show file tree
Hide file tree
Showing 14 changed files with 128 additions and 366 deletions.
18 changes: 4 additions & 14 deletions admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
)

// CreateUser creates a Grafana user.
Expand All @@ -15,28 +14,19 @@ func (c *Client) CreateUser(user User) (int64, error) {
return id, err
}

resp, err := c.request("POST", "/api/admin/users", nil, bytes.NewBuffer(data))
if err != nil {
return id, err
}

data, err = ioutil.ReadAll(resp.Body)
if err != nil {
return id, err
}
created := struct {
Id int64 `json:"id"`
}{}
err = json.Unmarshal(data, &created)

err = c.request("POST", "/api/admin/users", nil, bytes.NewBuffer(data), &created)
if err != nil {
return id, err
}

return created.Id, err
}

// DeleteUser deletes a Grafana user.
func (c *Client) DeleteUser(id int64) error {
_, err := c.request("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil)

return err
return c.request("DELETE", fmt.Sprintf("/api/admin/users/%d", id), nil, nil, nil)
}
39 changes: 9 additions & 30 deletions alertnotification.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
)

// AlertNotification represents a Grafana alert notification.
Expand All @@ -24,35 +23,23 @@ type AlertNotification struct {
func (c *Client) AlertNotifications() ([]AlertNotification, error) {
alertnotifications := make([]AlertNotification, 0)

resp, err := c.request("GET", "/api/alert-notifications/", nil, nil)
err := c.request("GET", "/api/alert-notifications/", nil, nil, &alertnotifications)
if err != nil {
return nil, err
}

data, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}

err = json.Unmarshal(data, &alertnotifications)
return alertnotifications, err
}

// AlertNotification fetches and returns a Grafana alert notification.
func (c *Client) AlertNotification(id int64) (*AlertNotification, error) {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
resp, err := c.request("GET", path, nil, nil)
if err != nil {
return nil, err
}

data, err := ioutil.ReadAll(resp.Body)
result := &AlertNotification{}
err := c.request("GET", path, nil, nil, result)
if err != nil {
return nil, err
}

result := &AlertNotification{}
err = json.Unmarshal(data, &result)
return result, err
}

Expand All @@ -62,20 +49,15 @@ func (c *Client) NewAlertNotification(a *AlertNotification) (int64, error) {
if err != nil {
return 0, err
}
resp, err := c.request("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}
result := struct {
Id int64 `json:"id"`
}{}

data, err = ioutil.ReadAll(resp.Body)
err = c.request("POST", "/api/alert-notifications", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}

result := struct {
Id int64 `json:"id"`
}{}
err = json.Unmarshal(data, &result)
return result.Id, err
}

Expand All @@ -86,17 +68,14 @@ func (c *Client) UpdateAlertNotification(a *AlertNotification) error {
if err != nil {
return err
}

_, err = c.request("PUT", path, nil, bytes.NewBuffer(data))
err = c.request("PUT", path, nil, bytes.NewBuffer(data), nil)

return err

}

// DeleteAlertNotification deletes a Grafana alert notification.
func (c *Client) DeleteAlertNotification(id int64) error {
path := fmt.Sprintf("/api/alert-notifications/%d", id)
_, err := c.request("DELETE", path, nil, nil)

return err
return c.request("DELETE", path, nil, nil, nil)
}
93 changes: 30 additions & 63 deletions annotation.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
)

Expand Down Expand Up @@ -38,18 +37,12 @@ type GraphiteAnnotation struct {

// Annotations fetches the annotations queried with the params it's passed
func (c *Client) Annotations(params url.Values) ([]Annotation, error) {
resp, err := c.request("GET", "/api/annotation", params, nil)
if err != nil {
return nil, err
}

data, err := ioutil.ReadAll(resp.Body)
result := []Annotation{}
err := c.request("GET", "/api/annotation", params, nil, &result)
if err != nil {
return nil, err
}

result := []Annotation{}
err = json.Unmarshal(data, &result)
return result, err
}

Expand All @@ -59,20 +52,16 @@ func (c *Client) NewAnnotation(a *Annotation) (int64, error) {
if err != nil {
return 0, err
}
resp, err := c.request("POST", "/api/annotations", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}

data, err = ioutil.ReadAll(resp.Body)
result := struct {
ID int64 `json:"id"`
}{}

err = c.request("POST", "/api/annotations", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}

result := struct {
ID int64 `json:"id"`
}{}
err = json.Unmarshal(data, &result)
return result.ID, err
}

Expand All @@ -82,20 +71,16 @@ func (c *Client) NewGraphiteAnnotation(gfa *GraphiteAnnotation) (int64, error) {
if err != nil {
return 0, err
}
resp, err := c.request("POST", "/api/annotations/graphite", nil, bytes.NewBuffer(data))
if err != nil {
return 0, err
}

data, err = ioutil.ReadAll(resp.Body)
result := struct {
ID int64 `json:"id"`
}{}

err = c.request("POST", "/api/annotations/graphite", nil, bytes.NewBuffer(data), &result)
if err != nil {
return 0, err
}

result := struct {
ID int64 `json:"id"`
}{}
err = json.Unmarshal(data, &result)
return result.ID, err
}

Expand All @@ -106,20 +91,16 @@ func (c *Client) UpdateAnnotation(id int64, a *Annotation) (string, error) {
if err != nil {
return "", err
}
resp, err := c.request("PUT", path, nil, bytes.NewBuffer(data))
if err != nil {
return "", err
}

data, err = ioutil.ReadAll(resp.Body)
result := struct {
Message string `json:"message"`
}{}

err = c.request("PUT", path, nil, bytes.NewBuffer(data), &result)
if err != nil {
return "", err
}

result := struct {
Message string `json:"message"`
}{}
err = json.Unmarshal(data, &result)
return result.Message, err
}

Expand All @@ -130,59 +111,45 @@ func (c *Client) PatchAnnotation(id int64, a *Annotation) (string, error) {
if err != nil {
return "", err
}
resp, err := c.request("PATCH", path, nil, bytes.NewBuffer(data))
if err != nil {
return "", err
}

data, err = ioutil.ReadAll(resp.Body)
result := struct {
Message string `json:"message"`
}{}

err = c.request("PATCH", path, nil, bytes.NewBuffer(data), &result)
if err != nil {
return "", err
}

result := struct {
Message string `json:"message"`
}{}
err = json.Unmarshal(data, &result)
return result.Message, err
}

// DeleteAnnotation deletes the annotation of the ID it is passed
func (c *Client) DeleteAnnotation(id int64) (string, error) {
path := fmt.Sprintf("/api/annotations/%d", id)
resp, err := c.request("DELETE", path, nil, bytes.NewBuffer(nil))
if err != nil {
return "", err
}
result := struct {
Message string `json:"message"`
}{}

data, err := ioutil.ReadAll(resp.Body)
err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &result)
if err != nil {
return "", err
}

result := struct {
Message string `json:"message"`
}{}
err = json.Unmarshal(data, &result)
return result.Message, err
}

// DeleteAnnotationByRegionID deletes the annotation corresponding to the region ID it is passed
func (c *Client) DeleteAnnotationByRegionID(id int64) (string, error) {
path := fmt.Sprintf("/api/annotations/region/%d", id)
resp, err := c.request("DELETE", path, nil, bytes.NewBuffer(nil))
if err != nil {
return "", err
}
result := struct {
Message string `json:"message"`
}{}

data, err := ioutil.ReadAll(resp.Body)
err := c.request("DELETE", path, nil, bytes.NewBuffer(nil), &result)
if err != nil {
return "", err
}

result := struct {
Message string `json:"message"`
}{}
err = json.Unmarshal(data, &result)
return result.Message, err
}
26 changes: 21 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ package gapi

import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"net/url"
Expand Down Expand Up @@ -41,22 +43,36 @@ func New(auth, baseURL string) (*Client, error) {
}, nil
}

func (c *Client) request(method, requestPath string, query url.Values, body io.Reader) (*http.Response, error) {
func (c *Client) request(method, requestPath string, query url.Values, body io.Reader, responseStruct interface{}) error {
r, err := c.newRequest(method, requestPath, query, body)
if err != nil {
return nil, err
return err
}

resp, err := c.Do(r)
if err != nil {
return nil, err
return err
}

bodyContents, err := ioutil.ReadAll(resp.Body)
if err != nil {
return err
}

if resp.StatusCode >= 400 {
return nil, fmt.Errorf("status: %d", resp.StatusCode)
return fmt.Errorf("status: %d, body: %v", resp.StatusCode, bodyContents)
}

if responseStruct == nil {
return nil
}

err = json.Unmarshal(bodyContents, responseStruct)
if err != nil {
return err
}

return resp, err
return nil
}

func (c *Client) newRequest(method, requestPath string, query url.Values, body io.Reader) (*http.Request, error) {
Expand Down
Loading

0 comments on commit ee181ad

Please sign in to comment.