From 19c97136dd43817a76b01c76f9170fd79c160e2a Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Wed, 18 May 2022 13:16:02 +1000 Subject: [PATCH] notifications: use go-querystring for filtering URI params --- cloudflare.go | 4 ++-- notifications.go | 35 ++++++++++++----------------------- 2 files changed, 14 insertions(+), 25 deletions(-) diff --git a/cloudflare.go b/cloudflare.go index 76781776e3..8631143a9f 100644 --- a/cloudflare.go +++ b/cloudflare.go @@ -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 diff --git a/notifications.go b/notifications.go index 12933d8259..80e73d0bcd 100644 --- a/notifications.go +++ b/notifications.go @@ -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 @@ -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 @@ -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 }