Skip to content

Commit

Permalink
Reduce code for building requests and decoding response bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
itchyny committed Nov 8, 2023
1 parent 264b7b7 commit 4c7e25a
Show file tree
Hide file tree
Showing 24 changed files with 401 additions and 1,277 deletions.
102 changes: 16 additions & 86 deletions alert_group_settings.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
package mackerel

import (
"encoding/json"
"fmt"
"net/http"
)
import "fmt"

// AlertGroupSetting represents a Mackerel alert group setting.
// ref. https://mackerel.io/api-docs/entry/alert-group-settings
Expand All @@ -18,102 +14,36 @@ type AlertGroupSetting struct {
NotificationInterval uint64 `json:"notificationInterval,omitempty"`
}

// FindAlertGroupSettings finds alert group settings
// FindAlertGroupSettings finds alert group settings.
func (c *Client) FindAlertGroupSettings() ([]*AlertGroupSetting, error) {
req, err := http.NewRequest("GET", c.urlFor("/api/v0/alert-group-settings").String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data struct {
data, err := requestGet[struct {
AlertGroupSettings []*AlertGroupSetting `json:"alertGroupSettings"`
}
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
}](c, "/api/v0/alert-group-settings")
if err != nil {
return nil, err
}

return data.AlertGroupSettings, nil
}

// CreateAlertGroupSetting creates a alert group setting
// CreateAlertGroupSetting creates an alert group setting.
func (c *Client) CreateAlertGroupSetting(param *AlertGroupSetting) (*AlertGroupSetting, error) {
resp, err := c.PostJSON("/api/v0/alert-group-settings", param)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var alertGroupSetting AlertGroupSetting
if err := json.NewDecoder(resp.Body).Decode(&alertGroupSetting); err != nil {
return nil, err
}

return &alertGroupSetting, nil
return requestPost[AlertGroupSetting](c, "/api/v0/alert-group-settings", param)
}

// GetAlertGroupSetting gets alert group setting specified by ID
// GetAlertGroupSetting gets an alert group setting.
func (c *Client) GetAlertGroupSetting(id string) (*AlertGroupSetting, error) {
req, err := http.NewRequest("GET", c.urlFor(fmt.Sprintf("/api/v0/alert-group-settings/%s", id)).String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var alertGroupSetting AlertGroupSetting
if err := json.NewDecoder(resp.Body).Decode(&alertGroupSetting); err != nil {
return nil, err
}

return &alertGroupSetting, nil
path := fmt.Sprintf("/api/v0/alert-group-settings/%s", id)
return requestGet[AlertGroupSetting](c, path)
}

// UpdateAlertGroupSetting updates a alert group setting
// UpdateAlertGroupSetting updates an alert group setting.
func (c *Client) UpdateAlertGroupSetting(id string, param *AlertGroupSetting) (*AlertGroupSetting, error) {
resp, err := c.PutJSON(fmt.Sprintf("/api/v0/alert-group-settings/%s", id), param)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var alertGroupSetting AlertGroupSetting
if err := json.NewDecoder(resp.Body).Decode(&alertGroupSetting); err != nil {
return nil, err
}

return &alertGroupSetting, nil
path := fmt.Sprintf("/api/v0/alert-group-settings/%s", id)
return requestPut[AlertGroupSetting](c, path, param)
}

// DeleteAlertGroupSetting deletes a alert group setting specified by ID.
// DeleteAlertGroupSetting deletes an alert group setting.
func (c *Client) DeleteAlertGroupSetting(id string) (*AlertGroupSetting, error) {
req, err := http.NewRequest(
"DELETE",
c.urlFor(fmt.Sprintf("/api/v0/alert-group-settings/%s", id)).String(),
nil,
)
if err != nil {
return nil, err
}
req.Header.Add("Content-Type", "application/json")

resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var alertGroupSetting AlertGroupSetting
if err := json.NewDecoder(resp.Body).Decode(&alertGroupSetting); err != nil {
return nil, err
}

return &alertGroupSetting, nil
path := fmt.Sprintf("/api/v0/alert-group-settings/%s", id)
return requestDelete[AlertGroupSetting](c, path)
}
111 changes: 26 additions & 85 deletions alerts.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package mackerel

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
)

Expand Down Expand Up @@ -61,108 +59,51 @@ type UpdateAlertResponse struct {
Memo string `json:"memo,omitempty"`
}

func (c *Client) findAlertsWithParam(v url.Values) (*AlertsResp, error) {
var d AlertsResp
u := c.urlFor("/api/v0/alerts")
u.RawQuery = v.Encode()
req, err := http.NewRequest("GET", u.String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

err = json.NewDecoder(resp.Body).Decode(&d)
if err != nil {
return nil, err
}
return &d, err
func (c *Client) findAlertsWithParams(params url.Values) (*AlertsResp, error) {
return requestGetWithParams[AlertsResp](c, "/api/v0/alerts", params)
}

// FindAlerts find open alerts
// FindAlerts finds open alerts.
func (c *Client) FindAlerts() (*AlertsResp, error) {
return c.findAlertsWithParam(url.Values{})
return c.findAlertsWithParams(nil)
}

// FindAlertsByNextID find next open alerts by next id
// FindAlertsByNextID finds next open alerts by next id.
func (c *Client) FindAlertsByNextID(nextID string) (*AlertsResp, error) {
v := url.Values{}
v.Set("nextId", nextID)
return c.findAlertsWithParam(v)
params := url.Values{}
params.Set("nextId", nextID)
return c.findAlertsWithParams(params)
}

// FindWithClosedAlerts find open and close alerts
// FindWithClosedAlerts finds open and close alerts.
func (c *Client) FindWithClosedAlerts() (*AlertsResp, error) {
v := url.Values{}
v.Set("withClosed", "true")
return c.findAlertsWithParam(v)
params := url.Values{}
params.Set("withClosed", "true")
return c.findAlertsWithParams(params)
}

// FindWithClosedAlertsByNextID find open and close alerts by next id
// FindWithClosedAlertsByNextID finds open and close alerts by next id.
func (c *Client) FindWithClosedAlertsByNextID(nextID string) (*AlertsResp, error) {
v := url.Values{}
v.Set("nextId", nextID)
v.Set("withClosed", "true")
return c.findAlertsWithParam(v)
params := url.Values{}
params.Set("nextId", nextID)
params.Set("withClosed", "true")
return c.findAlertsWithParams(params)
}

// GetAlert gets Alert
// GetAlert gets an alert.
func (c *Client) GetAlert(alertID string) (*Alert, error) {
req, err := http.NewRequest("GET", c.urlFor(fmt.Sprintf("/api/v0/alerts/%s", alertID)).String(), nil)
if err != nil {
return nil, err
}
resp, err := c.Request(req)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var alert *Alert
err = json.NewDecoder(resp.Body).Decode(&alert)
if err != nil {
return nil, err
}
return alert, err
path := fmt.Sprintf("/api/v0/alerts/%s", alertID)
return requestGet[Alert](c, path)
}

// CloseAlert close alert
// CloseAlert closes an alert.
func (c *Client) CloseAlert(alertID string, reason string) (*Alert, error) {
var reqBody struct {
Reason string `json:"reason"`
}
reqBody.Reason = reason
resp, err := c.PostJSON(fmt.Sprintf("/api/v0/alerts/%s/close", alertID), &reqBody)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data *Alert
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}

return data, nil
path := fmt.Sprintf("/api/v0/alerts/%s/close", alertID)
return requestPost[Alert](c, path, map[string]string{"reason": reason})
}

// UpdateAlert updates an Alert
// UpdateAlert updates an alert.
func (c *Client) UpdateAlert(alertID string, param UpdateAlertParam) (*UpdateAlertResponse, error) {
resp, err := c.PutJSON(fmt.Sprintf("/api/v0/alerts/%s", alertID), param)
defer closeResponse(resp)
if err != nil {
return nil, err
}

var data UpdateAlertResponse
err = json.NewDecoder(resp.Body).Decode(&data)
if err != nil {
return nil, err
}

return &data, nil
path := fmt.Sprintf("/api/v0/alerts/%s", alertID)
return requestPut[UpdateAlertResponse](c, path, param)
}
Loading

0 comments on commit 4c7e25a

Please sign in to comment.