Updating docs and interation exit condition to not check the final page

This commit is contained in:
Gabriel Ramírez 2021-10-06 14:18:44 +00:00 committed by GitHub
parent aa49a3a557
commit 34f9c0a67c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 6 additions and 2 deletions

View file

@ -194,16 +194,18 @@ type getCodespacesListResponse struct {
}
// ListCodespaces returns a list of codespaces for the user.
// It consumes all pages returned by the API until all codespaces have been fetched.
func (a *API) ListCodespaces(ctx context.Context) (codespaces []*Codespace, err error) {
per_page := 50
for page := 1; ; page++ {
response, err := a.fetchCodespaces(ctx, page)
if err != nil {
return nil, fmt.Errorf("%w", err)
}
if len(codespaces) >= response.TotalCount || len(response.Codespaces) == 0 {
codespaces = append(codespaces, response.Codespaces...)
if page*per_page >= response.TotalCount {
break
}
codespaces = append(codespaces, response.Codespaces...)
}
return codespaces, nil

View file

@ -50,6 +50,8 @@ func createFakeListEndpointServer(t *testing.T, initalTotal int, finalTotal int)
} else if page == 2 {
response.Codespaces = generateCodespaceList(per_page, per_page*2)
response.TotalCount = finalTotal
} else {
t.Fatal("Should not check extra page")
}
data, _ := json.Marshal(response)