Skip to content

Commit

Permalink
Merge pull request #160 from phillc/page_size
Browse files Browse the repository at this point in the history
Implements PageSize to requests
  • Loading branch information
phillc authored Aug 27, 2020
2 parents 3607eff + 181381e commit e4d9cf2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
53 changes: 26 additions & 27 deletions pagination.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -32,28 +33,38 @@ 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
// returned in a single (endpoint-specific)PagedResponse
// 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
results int
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:
Expand Down Expand Up @@ -295,23 +306,17 @@ 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
results int
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:
Expand Down Expand Up @@ -436,23 +441,17 @@ 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
results int
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:
Expand Down

0 comments on commit e4d9cf2

Please sign in to comment.