Skip to content

Commit

Permalink
Implement ResultInfo.DoneCount() for result_info that doesn't impleme…
Browse files Browse the repository at this point in the history
…nt total_pages
  • Loading branch information
Cyb3r-Jak3 committed Apr 17, 2023
1 parent bced544 commit 1e74748
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pages_deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ func (api *API) ListPagesDeployments(ctx context.Context, rc *ResourceContainer,
}
deployments = append(deployments, r.Result...)
params.ResultInfo = r.ResultInfo.Next()
if params.ResultInfo.Done() || !autoPaginate {
if params.DoneCount() || !autoPaginate {
break
}
}
Expand Down
68 changes: 62 additions & 6 deletions pages_deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

const (
testPagesDeplyomentResponse = `
testPagesDeploymentResponse = `
{
"id": "0012e50b-fa5d-44db-8cb5-1f372785dcbe",
"short_id": "0012e50b",
Expand Down Expand Up @@ -268,7 +268,7 @@ func TestListPagesDeployments(t *testing.T) {
"count": 1,
"total_count": 1
}
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler)
Expand All @@ -292,6 +292,62 @@ func TestListPagesDeployments(t *testing.T) {
}
}

func TestListPagesDeploymentsPagination(t *testing.T) {
setup()
defer teardown()
var page1Called, page2Called bool
handler := func(w http.ResponseWriter, r *http.Request) {
page := r.URL.Query().Get("page")
w.Header().Set("content-type", "application/json")
switch page {
case "1":
page1Called = true
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
%s
],
"result_info": {
"page": 1,
"per_page": 25,
"total_count": 26
}
}`, testPagesDeploymentResponse)
case "2":
page2Called = true
fmt.Fprintf(w, `{
"success": true,
"errors": [],
"messages": [],
"result": [
%s
],
"result_info": {
"page": 2,
"per_page": 25,
"total_count": 26
}
}`, testPagesDeploymentResponse)
default:
assert.Failf(t, "Unexpected page number", "Expected page 1 or 2, got %s", page)
return
}
}
mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler)
actual, resultInfo, err := client.ListPagesDeployments(context.Background(), AccountIdentifier(testAccountID), ListPagesDeploymentsParams{
ProjectName: "test",
ResultInfo: ResultInfo{},
})
if assert.NoError(t, err) {
assert.True(t, page1Called)
assert.True(t, page2Called)
assert.Equal(t, 2, len(actual))
assert.Equal(t, 26, resultInfo.Total)
}
}

func TestGetPagesDeploymentInfo(t *testing.T) {
setup()
defer teardown()
Expand All @@ -305,7 +361,7 @@ func TestGetPagesDeploymentInfo(t *testing.T) {
"errors": [],
"messages": [],
"result": %s
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe", handler)
Expand Down Expand Up @@ -379,7 +435,7 @@ func TestCreatePagesDeployment(t *testing.T) {
"errors": [],
"messages": [],
"result": %s
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments", handler)
Expand All @@ -406,7 +462,7 @@ func TestRetryPagesDeployment(t *testing.T) {
"errors": [],
"messages": [],
"result": %s
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe/retry", handler)
Expand All @@ -430,7 +486,7 @@ func TestRollbackPagesDeployment(t *testing.T) {
"errors": [],
"messages": [],
"result": %s
}`, testPagesDeplyomentResponse)
}`, testPagesDeploymentResponse)
}

mux.HandleFunc("/accounts/"+testAccountID+"/pages/projects/test/deployments/0012e50b-fa5d-44db-8cb5-1f372785dcbe/rollback", handler)
Expand Down
12 changes: 12 additions & 0 deletions pagination.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package cloudflare

import (
"math"
)

// Done returns true for the last page and false otherwise.
func (p ResultInfo) Done() bool {
return p.Page > 1 && p.Page > p.TotalPages
Expand All @@ -17,3 +21,11 @@ func (p ResultInfo) Next() ResultInfo {
func (p ResultInfo) HasMorePages() bool {
return p.Page > 1 && p.Page < p.TotalPages
}

// DoneCount returns true for the last page and false otherwise.
// This function uses the Total and PerPage fields to generate TotalPages
// then calls Done()
func (p ResultInfo) DoneCount() bool {
p.TotalPages = int(math.Ceil(float64(p.Total) / float64(p.PerPage)))
return p.Done()
}

0 comments on commit 1e74748

Please sign in to comment.