From 37a97e7031f07c5ab6c2afc0fe1054dbc9c884a9 Mon Sep 17 00:00:00 2001 From: Priyadharshini Subramanian Date: Tue, 17 May 2022 17:36:10 -0700 Subject: [PATCH] AS-8430: Update history API to accept timestamp query params --- notifications.go | 28 +++++++++++++++++++++++----- notifications_test.go | 19 +++++++++++++------ 2 files changed, 36 insertions(+), 11 deletions(-) diff --git a/notifications.go b/notifications.go index fe7630c650..0a1d97356c 100644 --- a/notifications.go +++ b/notifications.go @@ -407,19 +407,37 @@ 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"` + Before string `json:"before,omitempty"` +} + +// AlertHistoryFilter is an object for filtering the alert history response from the api. +type AlertHistoryFilter struct { + TimeRange TimeRange + Pagination 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) { +func (api *API) ListNotificationHistory(ctx context.Context, accountID string, alertHistoryFilter AlertHistoryFilter) ([]NotificationHistory, ResultInfo, error) { v := url.Values{} - if pageOpts.PerPage > 0 { - v.Set("per_page", strconv.Itoa(pageOpts.PerPage)) + 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 pageOpts.Page > 0 { - v.Set("page", strconv.Itoa(pageOpts.Page)) + if alertHistoryFilter.TimeRange.Before != "" { + v.Set("before", alertHistoryFilter.TimeRange.Before) } baseURL := fmt.Sprintf("/accounts/%s/alerting/v3/history", accountID) diff --git a/notifications_test.go b/notifications_test.go index 67d17d3cad..06d6d6099c 100644 --- a/notifications_test.go +++ b/notifications_test.go @@ -523,16 +523,23 @@ func TestListNotificationHistory(t *testing.T) { } expectedResultInfo := ResultInfo{ - Page: 0, - PerPage: 25, - Count: 1, + Page: 0, + PerPage: 25, + Count: 1, } pageOptions := PaginationOptions{ PerPage: 25, - Page: 1, + Page: 1, + } + + timeRange := TimeRange{ + Since: time.Now().Add(-15 * time.Minute).Format(time.RFC3339), + Before: time.Now().Format(time.RFC3339), } + historyFilters := AlertHistoryFilter{TimeRange: timeRange, Pagination: pageOptions} + alertHistory, err := json.Marshal(expected) require.NoError(t, err) require.NotNil(t, alertHistory) @@ -560,9 +567,9 @@ 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) require.Equal(t, expectedResultInfo, actualResultInfo) -} \ No newline at end of file +}