Skip to content

Commit e4d9cf2

Browse files
authored
Merge pull request #160 from phillc/page_size
Implements PageSize to requests
2 parents 3607eff + 181381e commit e4d9cf2

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ kernels, err := linodego.ListKernels(context.Background(), opts)
9494

9595
```go
9696
opts := linodego.NewListOptions(2,"")
97-
// or opts := linodego.ListOptions{PageOptions: &PageOptions: {Page: 2 }}
97+
// or opts := linodego.ListOptions{PageOptions: &linodego.PageOptions{Page: 2}, PageSize: 500}
9898
kernels, err := linodego.ListKernels(context.Background(), opts)
9999
// len(kernels) == 100
100100
```

pagination.go

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ type PageOptions struct {
2323
// ListOptions are the pagination and filtering (TODO) parameters for endpoints
2424
type ListOptions struct {
2525
*PageOptions
26-
Filter string
26+
PageSize int
27+
Filter string
2728
}
2829

2930
// NewListOptions simplified construction of ListOptions using only
@@ -32,28 +33,38 @@ func NewListOptions(page int, filter string) *ListOptions {
3233
return &ListOptions{PageOptions: &PageOptions{Page: page}, Filter: filter}
3334
}
3435

36+
func applyListOptionsToRequest(opts *ListOptions, req *resty.Request) {
37+
if opts != nil {
38+
if opts.PageOptions != nil && opts.Page > 0 {
39+
req.SetQueryParam("page", strconv.Itoa(opts.Page))
40+
}
41+
42+
if opts.PageSize > 0 {
43+
req.SetQueryParam("page_size", strconv.Itoa(opts.PageSize))
44+
}
45+
46+
if len(opts.Filter) > 0 {
47+
req.SetHeader("X-Filter", opts.Filter)
48+
}
49+
}
50+
}
51+
3552
// listHelper abstracts fetching and pagination for GET endpoints that
3653
// do not require any Ids (top level endpoints).
3754
// When opts (or opts.Page) is nil, all pages will be fetched and
3855
// returned in a single (endpoint-specific)PagedResponse
3956
// opts.results and opts.pages will be updated from the API response
4057
// nolint
4158
func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOptions) error {
42-
req := c.R(ctx)
43-
if opts != nil && opts.PageOptions != nil && opts.Page > 0 {
44-
req.SetQueryParam("page", strconv.Itoa(opts.Page))
45-
}
46-
4759
var (
4860
err error
4961
pages int
5062
results int
5163
r *resty.Response
5264
)
5365

54-
if opts != nil && len(opts.Filter) > 0 {
55-
req.SetHeader("X-Filter", opts.Filter)
56-
}
66+
req := c.R(ctx)
67+
applyListOptionsToRequest(opts, req)
5768

5869
switch v := i.(type) {
5970
case *LinodeKernelsPagedResponse:
@@ -295,23 +306,17 @@ func (c *Client) listHelper(ctx context.Context, i interface{}, opts *ListOption
295306
// opts.results and opts.pages will be updated from the API response
296307
// nolint
297308
func (c *Client) listHelperWithID(ctx context.Context, i interface{}, idRaw interface{}, opts *ListOptions) error {
298-
req := c.R(ctx)
299-
if opts != nil && opts.Page > 0 {
300-
req.SetQueryParam("page", strconv.Itoa(opts.Page))
301-
}
302-
303309
var (
304310
err error
305311
pages int
306312
results int
307313
r *resty.Response
308314
)
309315

310-
id, _ := idRaw.(int)
316+
req := c.R(ctx)
317+
applyListOptionsToRequest(opts, req)
311318

312-
if opts != nil && len(opts.Filter) > 0 {
313-
req.SetHeader("X-Filter", opts.Filter)
314-
}
319+
id, _ := idRaw.(int)
315320

316321
switch v := i.(type) {
317322
case *DomainRecordsPagedResponse:
@@ -436,23 +441,17 @@ func (c *Client) listHelperWithID(ctx context.Context, i interface{}, idRaw inte
436441
// When opts (or opts.Page) is nil, all pages will be fetched and
437442
// returned in a single (endpoint-specific)PagedResponse
438443
// opts.results and opts.pages will be updated from the API response
444+
// nolint
439445
func (c *Client) listHelperWithTwoIDs(ctx context.Context, i interface{}, firstID, secondID int, opts *ListOptions) error {
440-
req := c.R(ctx)
441-
442-
if opts != nil && opts.Page > 0 {
443-
req.SetQueryParam("page", strconv.Itoa(opts.Page))
444-
}
445-
446446
var (
447447
err error
448448
pages int
449449
results int
450450
r *resty.Response
451451
)
452452

453-
if opts != nil && len(opts.Filter) > 0 {
454-
req.SetHeader("X-Filter", opts.Filter)
455-
}
453+
req := c.R(ctx)
454+
applyListOptionsToRequest(opts, req)
456455

457456
switch v := i.(type) {
458457
case *NodeBalancerNodesPagedResponse:

0 commit comments

Comments
 (0)