Skip to content

Commit 34f9c0a

Browse files
authored
Updating docs and interation exit condition to not check the final page
1 parent aa49a3a commit 34f9c0a

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

internal/codespaces/api/api.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -194,16 +194,18 @@ type getCodespacesListResponse struct {
194194
}
195195

196196
// ListCodespaces returns a list of codespaces for the user.
197+
// It consumes all pages returned by the API until all codespaces have been fetched.
197198
func (a *API) ListCodespaces(ctx context.Context) (codespaces []*Codespace, err error) {
199+
per_page := 50
198200
for page := 1; ; page++ {
199201
response, err := a.fetchCodespaces(ctx, page)
200202
if err != nil {
201203
return nil, fmt.Errorf("%w", err)
202204
}
203-
if len(codespaces) >= response.TotalCount || len(response.Codespaces) == 0 {
205+
codespaces = append(codespaces, response.Codespaces...)
206+
if page*per_page >= response.TotalCount {
204207
break
205208
}
206-
codespaces = append(codespaces, response.Codespaces...)
207209
}
208210

209211
return codespaces, nil

internal/codespaces/api/api_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func createFakeListEndpointServer(t *testing.T, initalTotal int, finalTotal int)
5050
} else if page == 2 {
5151
response.Codespaces = generateCodespaceList(per_page, per_page*2)
5252
response.TotalCount = finalTotal
53+
} else {
54+
t.Fatal("Should not check extra page")
5355
}
5456

5557
data, _ := json.Marshal(response)

0 commit comments

Comments
 (0)