Skip to content

Commit 103dabe

Browse files
committed
add list item search & list item pagination page size
1 parent 3f28780 commit 103dabe

File tree

1 file changed

+20
-7
lines changed

1 file changed

+20
-7
lines changed

list.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"net/url"
9+
"strconv"
810
"time"
911

1012
"github.com/goccy/go-json"
@@ -177,7 +179,9 @@ type ListDeleteParams struct {
177179
}
178180

179181
type ListListItemsParams struct {
180-
ID string
182+
ID string
183+
Search string
184+
PerPage *uint32
181185
}
182186

183187
type ListCreateItemsParams struct {
@@ -336,14 +340,21 @@ func (api *API) DeleteList(ctx context.Context, rc *ResourceContainer, listID st
336340
// API reference: https://api.cloudflare.com/#rules-lists-list-list-items
337341
func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params ListListItemsParams) ([]ListItem, error) {
338342
var list []ListItem
339-
var cursor string
340-
var cursorQuery string
343+
344+
var query = url.Values{}
345+
if params.Search != "" {
346+
query.Set("search", params.Search)
347+
}
348+
if params.PerPage != nil {
349+
query.Set("per_page", strconv.FormatUint(uint64(*params.PerPage), 10))
350+
}
341351

342352
for {
343-
if len(cursor) > 0 {
344-
cursorQuery = fmt.Sprintf("?cursor=%s", cursor)
353+
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items", rc.Identifier, params.ID)
354+
if queryEncode := query.Encode(); queryEncode != "" {
355+
uri += "?" + queryEncode
345356
}
346-
uri := fmt.Sprintf("/accounts/%s/rules/lists/%s/items%s", rc.Identifier, params.ID, cursorQuery)
357+
347358
res, err := api.makeRequestContext(ctx, http.MethodGet, uri, nil)
348359
if err != nil {
349360
return []ListItem{}, err
@@ -355,8 +366,10 @@ func (api *API) ListListItems(ctx context.Context, rc *ResourceContainer, params
355366
}
356367

357368
list = append(list, result.Result...)
358-
if cursor = result.ResultInfo.Cursors.After; cursor == "" {
369+
if cursor := result.ResultInfo.Cursors.After; cursor == "" {
359370
break
371+
} else {
372+
query.Set("cursor", cursor)
360373
}
361374
}
362375

0 commit comments

Comments
 (0)