Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements PageSize to requests #160

Merged
merged 1 commit into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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