From b2b0502df6bfc42a5d34c2e8136f240bf601bbd8 Mon Sep 17 00:00:00 2001 From: phillc <15082+phillc@users.noreply.github.com> Date: Thu, 27 Aug 2020 09:15:44 -0400 Subject: [PATCH] Implements PageSize to requests --- README.md | 2 +- pagination.go | 53 +++++++++++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/README.md b/README.md index 18555a879..9a072d4b7 100644 --- a/README.md +++ b/README.md @@ -94,7 +94,7 @@ kernels, err := linodego.ListKernels(context.Background(), opts) ```go opts := linodego.NewListOptions(2,"") -// or opts := linodego.ListOptions{PageOptions: &PageOptions: {Page: 2 }} +// or opts := linodego.ListOptions{PageOptions: &linodego.PageOptions{Page: 2}, PageSize: 500} kernels, err := linodego.ListKernels(context.Background(), opts) // len(kernels) == 100 ``` diff --git a/pagination.go b/pagination.go index f11175302..8bba92670 100644 --- a/pagination.go +++ b/pagination.go @@ -23,7 +23,8 @@ type PageOptions struct { // ListOptions are the pagination and filtering (TODO) parameters for endpoints type ListOptions struct { *PageOptions - Filter string + PageSize int + Filter string } // NewListOptions simplified construction of ListOptions using only @@ -32,6 +33,22 @@ func NewListOptions(page int, filter string) *ListOptions { return &ListOptions{PageOptions: &PageOptions{Page: page}, Filter: filter} } +func applyListOptionsToRequest(opts *ListOptions, req *resty.Request) { + if opts != nil { + if opts.PageOptions != nil && opts.Page > 0 { + req.SetQueryParam("page", strconv.Itoa(opts.Page)) + } + + if opts.PageSize > 0 { + req.SetQueryParam("page_size", strconv.Itoa(opts.PageSize)) + } + + if len(opts.Filter) > 0 { + req.SetHeader("X-Filter", opts.Filter) + } + } +} + // listHelper abstracts fetching and pagination for GET endpoints that // do not require any Ids (top level endpoints). // When opts (or opts.Page) is nil, all pages will be fetched and @@ -39,11 +56,6 @@ func NewListOptions(page int, filter string) *ListOptions { // opts.results and opts.pages will be updated from the API response // nolint func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOptions) error { - req := c.R(ctx) - if opts != nil && opts.PageOptions != nil && opts.Page > 0 { - req.SetQueryParam("page", strconv.Itoa(opts.Page)) - } - var ( err error pages int @@ -51,9 +63,8 @@ func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOption r *resty.Response ) - if opts != nil && len(opts.Filter) > 0 { - req.SetHeader("X-Filter", opts.Filter) - } + req := c.R(ctx) + applyListOptionsToRequest(opts, req) switch v := i.(type) { case *LinodeKernelsPagedResponse: @@ -295,11 +306,6 @@ func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOption // opts.results and opts.pages will be updated from the API response // nolint func (c *Client) listHelperWithID(ctx context.Context, i interface{}, idRaw interface{}, opts *ListOptions) error { - req := c.R(ctx) - if opts != nil && opts.Page > 0 { - req.SetQueryParam("page", strconv.Itoa(opts.Page)) - } - var ( err error pages int @@ -307,11 +313,10 @@ func (c *Client) listHelperWithID(ctx context.Context, i interface{}, idRaw inte r *resty.Response ) - id, _ := idRaw.(int) + req := c.R(ctx) + applyListOptionsToRequest(opts, req) - if opts != nil && len(opts.Filter) > 0 { - req.SetHeader("X-Filter", opts.Filter) - } + id, _ := idRaw.(int) switch v := i.(type) { case *DomainRecordsPagedResponse: @@ -436,13 +441,8 @@ func (c *Client) listHelperWithID(ctx context.Context, i interface{}, idRaw inte // When opts (or opts.Page) is nil, all pages will be fetched and // returned in a single (endpoint-specific)PagedResponse // opts.results and opts.pages will be updated from the API response +// nolint func (c *Client) listHelperWithTwoIDs(ctx context.Context, i interface{}, firstID, secondID int, opts *ListOptions) error { - req := c.R(ctx) - - if opts != nil && opts.Page > 0 { - req.SetQueryParam("page", strconv.Itoa(opts.Page)) - } - var ( err error pages int @@ -450,9 +450,8 @@ func (c *Client) listHelperWithTwoIDs(ctx context.Context, i interface{}, firstI r *resty.Response ) - if opts != nil && len(opts.Filter) > 0 { - req.SetHeader("X-Filter", opts.Filter) - } + req := c.R(ctx) + applyListOptionsToRequest(opts, req) switch v := i.(type) { case *NodeBalancerNodesPagedResponse: