Skip to content

Commit

Permalink
Update pagination to compute current page based off total_count and f…
Browse files Browse the repository at this point in the history
…ix pagination for a few Access Resources
  • Loading branch information
jroyal committed Aug 24, 2023
1 parent 0220774 commit a1a582b
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 11 deletions.
2 changes: 1 addition & 1 deletion access_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ func (api *API) ListAccessApplications(ctx context.Context, rc *ResourceContaine
}
applications = append(applications, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || autoPaginate {
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion access_ca_certificate.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ func (api *API) ListAccessCACertificates(ctx context.Context, rc *ResourceContai
}
accessCACertificates = append(accessCACertificates, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || autoPaginate {
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion access_group.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func (api *API) ListAccessGroups(ctx context.Context, rc *ResourceContainer, par
}
accessGroups = append(accessGroups, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || autoPaginate {
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion access_mutual_tls_certificates.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ func (api *API) ListAccessMutualTLSCertificates(ctx context.Context, rc *Resourc
}
accessCertificates = append(accessCertificates, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || autoPaginate {
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
Expand Down
2 changes: 1 addition & 1 deletion access_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (api *API) ListAccessPolicies(ctx context.Context, rc *ResourceContainer, p
}
accessPolicies = append(accessPolicies, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || autoPaginate {
if params.ResultInfo.Done() || !autoPaginate {
break
}
}
Expand Down
28 changes: 22 additions & 6 deletions pagination.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
package cloudflare

import (
"math"
)

// Look first for total_pages, but if total_count and per_page are set then use that to get page count
func (p ResultInfo) getTotalPages() int {
totalPages := p.TotalPages
if totalPages == 0 && p.Total > 0 && p.PerPage > 0 {
totalPages = int(math.Ceil(float64(p.Total) / float64(p.PerPage)))
}
return totalPages
}

// Done returns true for the last page and false otherwise.
func (p ResultInfo) Done() bool {
// A little hacky but if the response body is lacking a defined `ResultInfo`
// object the page will be 1 however the counts will be empty so if we have
// that response, we just assume this is the only page.
if p.Page == 1 && p.TotalPages == 0 {
totalPages := p.getTotalPages()
if p.Page == 1 && totalPages == 0 {
return true
}

return p.Page > 1 && p.Page > p.TotalPages
return p.Page > 1 && p.Page > totalPages
}

// Next advances the page of a paginated API response, but does not fetch the
Expand All @@ -18,13 +32,14 @@ func (p ResultInfo) Next() ResultInfo {
// A little hacky but if the response body is lacking a defined `ResultInfo`
// object the page will be 1 however the counts will be empty so if we have
// that response, we just assume this is the only page.
if p.Page == 1 && p.TotalPages == 0 {
totalPages := p.getTotalPages()
if p.Page == 1 && totalPages == 0 {
return p
}

// This shouldn't happen normally however, when it does just return the
// current page.
if p.Page > p.TotalPages {
if p.Page > totalPages {
return p
}

Expand All @@ -35,9 +50,10 @@ func (p ResultInfo) Next() ResultInfo {
// HasMorePages returns whether there is another page of results after the
// current one.
func (p ResultInfo) HasMorePages() bool {
if p.TotalPages == 0 {
totalPages := p.getTotalPages()
if totalPages == 0 {
return false
}

return p.Page >= 1 && p.Page < p.TotalPages
return p.Page >= 1 && p.Page < totalPages
}
24 changes: 24 additions & 0 deletions pagination_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ func TestPagination_Done(t *testing.T) {
r: ResultInfo{Page: 3, TotalPages: 1},
expected: true,
},
"total pages missing but done": {
r: ResultInfo{Page: 4, Total: 70, PerPage: 25},
expected: true,
},
"total pages missing and not done": {
r: ResultInfo{Page: 1, Total: 70, PerPage: 25},
expected: false,
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -59,6 +67,14 @@ func TestPagination_Next(t *testing.T) {
r: ResultInfo{Page: 3, TotalPages: 1},
expected: ResultInfo{Page: 3, TotalPages: 1},
},
"use per page and greater than page": {
r: ResultInfo{Page: 4, Total: 70, PerPage: 25},
expected: ResultInfo{Page: 4, Total: 70, PerPage: 25},
},
"use per page and less than page": {
r: ResultInfo{Page: 1, Total: 70, PerPage: 25},
expected: ResultInfo{Page: 2, Total: 70, PerPage: 25},
},
}

for name, tc := range testCases {
Expand Down Expand Up @@ -93,6 +109,14 @@ func TestPagination_HasMorePages(t *testing.T) {
r: ResultInfo{Page: 3, TotalPages: 1},
expected: false,
},
"use per page and greater than page": {
r: ResultInfo{Page: 4, Total: 70, PerPage: 25},
expected: false,
},
"use per page and less than page": {
r: ResultInfo{Page: 1, Total: 70, PerPage: 25},
expected: true,
},
}

for name, tc := range testCases {
Expand Down

0 comments on commit a1a582b

Please sign in to comment.