Skip to content

Commit

Permalink
notifications: use go-querystring for filtering URI params
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobbednarz committed May 18, 2022
1 parent 56ad0f3 commit 19c9713
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 25 deletions.
4 changes: 2 additions & 2 deletions cloudflare.go
Expand Up @@ -456,8 +456,8 @@ func (api *API) Raw(method, endpoint string, data interface{}) (json.RawMessage,
// PaginationOptions can be passed to a list request to configure paging
// These values will be defaulted if omitted, and PerPage has min/max limits set by resource.
type PaginationOptions struct {
Page int `json:"page,omitempty"`
PerPage int `json:"per_page,omitempty"`
Page int `json:"page,omitempty" url:"page,omitempty"`
PerPage int `json:"per_page,omitempty" url:"per_page,omitempty"`
}

// RetryPolicy specifies number of retries and min/max retry delays
Expand Down
35 changes: 12 additions & 23 deletions notifications.go
Expand Up @@ -5,9 +5,9 @@ import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strconv"
"time"

"github.com/google/go-querystring/query"
)

// NotificationMechanismData holds a single public facing mechanism data
Expand Down Expand Up @@ -407,14 +407,14 @@ func (api *API) GetAvailableNotificationTypes(ctx context.Context, accountID str

// TimeRange is an object for filtering the alert history based on timestamp.
type TimeRange struct {
Since string `json:"since,omitempty"`
Before string `json:"before,omitempty"`
Since string `json:"since,omitempty" url:"since,omitempty"`
Before string `json:"before,omitempty" url:"before,omitempty"`
}

// AlertHistoryFilter is an object for filtering the alert history response from the api.
type AlertHistoryFilter struct {
TimeRange TimeRange
Pagination PaginationOptions
TimeRange
PaginationOptions
}

// ListNotificationHistory will return the history of alerts sent for
Expand All @@ -424,26 +424,15 @@ type AlertHistoryFilter struct {
//
// API Reference: https://api.cloudflare.com/#notification-history-list-history
func (api *API) ListNotificationHistory(ctx context.Context, accountID string, alertHistoryFilter AlertHistoryFilter) ([]NotificationHistory, ResultInfo, error) {
v := url.Values{}
if alertHistoryFilter.Pagination.PerPage > 0 {
v.Set("per_page", strconv.Itoa(alertHistoryFilter.Pagination.PerPage))
}
if alertHistoryFilter.Pagination.Page > 0 {
v.Set("page", strconv.Itoa(alertHistoryFilter.Pagination.Page))
}
if alertHistoryFilter.TimeRange.Since != "" {
v.Set("since", alertHistoryFilter.TimeRange.Since)
}
if alertHistoryFilter.TimeRange.Before != "" {
v.Set("before", alertHistoryFilter.TimeRange.Before)
}
v, _ := query.Values(alertHistoryFilter)

baseURL := fmt.Sprintf("/accounts/%s/alerting/v3/history", accountID)
if len(v) > 0 {
baseURL = fmt.Sprintf("%s?%s", baseURL, v.Encode())
queryParams := v.Encode()
if queryParams != "" {
queryParams = "?" + queryParams
}

res, err := api.makeRequestContext(ctx, http.MethodGet, baseURL, nil)
baseURL := fmt.Sprintf("/accounts/%s/alerting/v3/history", accountID)
res, err := api.makeRequestContext(ctx, http.MethodGet, baseURL+queryParams, nil)
if err != nil {
return []NotificationHistory{}, ResultInfo{}, err
}
Expand Down

0 comments on commit 19c9713

Please sign in to comment.