From 103dabe0c76dbd28f4abd760af1e63c1f6bf50b3 Mon Sep 17 00:00:00 2001 From: Jess Porter Date: Mon, 25 Sep 2023 13:43:17 +0100 Subject: [PATCH 1/3] add list item search & list item pagination page size --- list.go | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/list.go b/list.go index e7893c35207..e90c8112801 100644 --- a/list.go +++ b/list.go @@ -5,6 +5,8 @@ import ( "errors" "fmt" "net/http" + "net/url" + "strconv" "time" "github.com/goccy/go-json" @@ -177,7 +179,9 @@ type ListDeleteParams struct { } type ListListItemsParams struct { - ID string + ID string + Search string + PerPage *uint32 } type ListCreateItemsParams struct { @@ -336,14 +340,21 @@ func (api *API) DeleteList(ctx context.Context, rc *ResourceContainer, listID st // API reference: https://api.cloudflare.com/#rules-lists-list-list-items func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params ListListItemsParams) ([]ListItem, error) { var list []ListItem - var cursor string - var cursorQuery string + + var query = url.Values{} + if params.Search != "" { + query.Set("search", params.Search) + } + if params.PerPage != nil { + query.Set("per_page", strconv.FormatUint(uint64(*params.PerPage), 10)) + } for { - if len(cursor) > 0 { - cursorQuery = fmt.Sprintf("?cursor=%s", cursor) + uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", rc.Identifier, params.ID) + if queryEncode := query.Encode(); queryEncode != "" { + uri += "?" + queryEncode } - uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items%s", rc.Identifier, params.ID, cursorQuery) + res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { return []ListItem{}, err @@ -355,8 +366,10 @@ func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params } list = append(list, result.Result...) - if cursor = result.ResultInfo.Cursors.After; cursor == "" { + if cursor := result.ResultInfo.Cursors.After; cursor == "" { break + } else { + query.Set("cursor", cursor) } } From 1cad03010f4b6f05936864a64c4853eb69a8247d Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 26 Sep 2023 12:06:28 +1000 Subject: [PATCH 2/3] swap to inbuilt library conventions for query parameter handling --- list.go | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/list.go b/list.go index e90c8112801..f7cef4aca6f 100644 --- a/list.go +++ b/list.go @@ -5,8 +5,6 @@ import ( "errors" "fmt" "net/http" - "net/url" - "strconv" "time" "github.com/goccy/go-json" @@ -179,9 +177,10 @@ type ListDeleteParams struct { } type ListListItemsParams struct { - ID string - Search string - PerPage *uint32 + ID string `url:"-"` + Search string `url:"search,omitempty"` + PerPage int `url:"per_page,omitempty"` + Cursor string `url:"cursor,omitempty"` } type ListCreateItemsParams struct { @@ -341,19 +340,8 @@ func (api *API) DeleteList(ctx context.Context, rc *ResourceContainer, listID st func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params ListListItemsParams) ([]ListItem, error) { var list []ListItem - var query = url.Values{} - if params.Search != "" { - query.Set("search", params.Search) - } - if params.PerPage != nil { - query.Set("per_page", strconv.FormatUint(uint64(*params.PerPage), 10)) - } - for { - uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", rc.Identifier, params.ID) - if queryEncode := query.Encode(); queryEncode != "" { - uri += "?" + queryEncode - } + uri := buildURI(fmt.Sprintf("/accounts/%s/rules/lists/%s/items", rc.Identifier, params.ID), params) res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil) if err != nil { @@ -369,7 +357,7 @@ func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params if cursor := result.ResultInfo.Cursors.After; cursor == "" { break } else { - query.Set("cursor", cursor) + params.Cursor = cursor } } From 9c451c7b729f3509577ceefd3b3741b9d660d04d Mon Sep 17 00:00:00 2001 From: Jacob Bednarz Date: Tue, 26 Sep 2023 12:07:56 +1000 Subject: [PATCH 3/3] add changelog entry --- .changelog/1409.txt | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .changelog/1409.txt diff --git a/.changelog/1409.txt b/.changelog/1409.txt new file mode 100644 index 00000000000..3132d3b0d4c --- /dev/null +++ b/.changelog/1409.txt @@ -0,0 +1,3 @@ +```release-note:enhancement +list_item: allow filtering by search term, cursor and per page attributes +```