Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AS-8430: Update history API to accept timestamp query params #888

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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: 21 additions & 14 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 @@ -405,27 +405,34 @@ func (api *API) GetAvailableNotificationTypes(ctx context.Context, accountID str
return r, nil
}

// TimeRange is an object for filtering the alert history based on timestamp.
type TimeRange struct {
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
PaginationOptions
}

// ListNotificationHistory will return the history of alerts sent for
// a given account. The time period varies based on zone plan.
// Free, Biz, Pro = 30 days
// Ent = 90 days
//
// API Reference: https://api.cloudflare.com/#notification-history-list-history
func (api *API) ListNotificationHistory(ctx context.Context, accountID string, pageOpts PaginationOptions) ([]NotificationHistory, ResultInfo, error) {
v := url.Values{}
if pageOpts.PerPage > 0 {
v.Set("per_page", strconv.Itoa(pageOpts.PerPage))
}
if pageOpts.Page > 0 {
v.Set("page", strconv.Itoa(pageOpts.Page))
}
func (api *API) ListNotificationHistory(ctx context.Context, accountID string, alertHistoryFilter AlertHistoryFilter) ([]NotificationHistory, ResultInfo, error) {
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
9 changes: 8 additions & 1 deletion notifications_test.go
Expand Up @@ -531,6 +531,13 @@ func TestListNotificationHistory(t *testing.T) {
Page: 1,
}

timeRange := TimeRange{
Since: time.Now().Add(-15 * time.Minute).Format(time.RFC3339),
Before: time.Now().Format(time.RFC3339),
}

historyFilters := AlertHistoryFilter{TimeRange: timeRange, PaginationOptions: pageOptions}

alertHistory, err := json.Marshal(expected)
require.NoError(t, err)
require.NotNil(t, alertHistory)
Expand Down Expand Up @@ -558,7 +565,7 @@ func TestListNotificationHistory(t *testing.T) {

mux.HandleFunc("/accounts/"+testAccountID+"/alerting/v3/history", handler)

actualResult, actualResultInfo, err := client.ListNotificationHistory(context.Background(), testAccountID, pageOptions)
actualResult, actualResultInfo, err := client.ListNotificationHistory(context.Background(), testAccountID, historyFilters)
require.Nil(t, err)
require.NotNil(t, actualResult)
require.Equal(t, expected, actualResult)
Expand Down