From 34f9c0a67c201f98ddde69f26794d1b2bf54539c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gabriel=20Ram=C3=ADrez?= Date: Wed, 6 Oct 2021 14:18:44 +0000 Subject: [PATCH] Updating docs and interation exit condition to not check the final page --- internal/codespaces/api/api.go | 6 ++++-- internal/codespaces/api/api_test.go | 2 ++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/internal/codespaces/api/api.go b/internal/codespaces/api/api.go index 2f5cc4d40..4abaac3d6 100644 --- a/internal/codespaces/api/api.go +++ b/internal/codespaces/api/api.go @@ -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 diff --git a/internal/codespaces/api/api_test.go b/internal/codespaces/api/api_test.go index 09c22df35..f1cd98efb 100644 --- a/internal/codespaces/api/api_test.go +++ b/internal/codespaces/api/api_test.go @@ -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)