Skip to content

Commit c2514e2

Browse files
fix: pagination example
1 parent 2a82e77 commit c2514e2

File tree

3 files changed

+105
-0
lines changed

3 files changed

+105
-0
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,9 +153,34 @@ This library provides some conveniences for working with paginated list endpoint
153153

154154
You can use `.ListAutoPaging()` methods to iterate through items across all pages:
155155

156+
```go
157+
iter := client.Environments.Automations.Services.ListAutoPaging(context.TODO(), gitpod.EnvironmentAutomationServiceListParams{})
158+
// Automatically fetches more pages as needed.
159+
for iter.Next() {
160+
environmentAutomationServiceListResponse := iter.Current()
161+
fmt.Printf("%+v\n", environmentAutomationServiceListResponse)
162+
}
163+
if err := iter.Err(); err != nil {
164+
panic(err.Error())
165+
}
166+
```
167+
156168
Or you can use simple `.List()` methods to fetch a single page and receive a standard response object
157169
with additional helper methods like `.GetNextPage()`, e.g.:
158170

171+
```go
172+
page, err := client.Environments.Automations.Services.List(context.TODO(), gitpod.EnvironmentAutomationServiceListParams{})
173+
for page != nil {
174+
for _, service := range page.Pagination.PersonalAccessTokens {
175+
fmt.Printf("%+v\n", service)
176+
}
177+
page, err = page.GetNextPage()
178+
}
179+
if err != nil {
180+
panic(err.Error())
181+
}
182+
```
183+
159184
### Errors
160185

161186
When the API returns a non-success status code, we return an error with type

paginationauto_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
package gitpod_test
4+
5+
import (
6+
"context"
7+
"os"
8+
"testing"
9+
10+
"github.com/stainless-sdks/gitpod-go"
11+
"github.com/stainless-sdks/gitpod-go/internal/testutil"
12+
"github.com/stainless-sdks/gitpod-go/option"
13+
)
14+
15+
func TestAutoPagination(t *testing.T) {
16+
baseURL := "http://localhost:4010"
17+
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
18+
baseURL = envURL
19+
}
20+
if !testutil.CheckTestServer(t, baseURL) {
21+
return
22+
}
23+
client := gitpod.NewClient(
24+
option.WithBaseURL(baseURL),
25+
option.WithBearerToken("My Bearer Token"),
26+
)
27+
iter := client.Environments.Automations.Services.ListAutoPaging(context.TODO(), gitpod.EnvironmentAutomationServiceListParams{})
28+
// Prism mock isn't going to give us real pagination
29+
for i := 0; i < 3 && iter.Next(); i++ {
30+
service := iter.Current()
31+
t.Logf("%+v\n", service.Pagination)
32+
}
33+
if err := iter.Err(); err != nil {
34+
t.Fatalf("err should be nil: %s", err.Error())
35+
}
36+
}

paginationmanual_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details.
2+
3+
package gitpod_test
4+
5+
import (
6+
"context"
7+
"os"
8+
"testing"
9+
10+
"github.com/stainless-sdks/gitpod-go"
11+
"github.com/stainless-sdks/gitpod-go/internal/testutil"
12+
"github.com/stainless-sdks/gitpod-go/option"
13+
)
14+
15+
func TestManualPagination(t *testing.T) {
16+
baseURL := "http://localhost:4010"
17+
if envURL, ok := os.LookupEnv("TEST_API_BASE_URL"); ok {
18+
baseURL = envURL
19+
}
20+
if !testutil.CheckTestServer(t, baseURL) {
21+
return
22+
}
23+
client := gitpod.NewClient(
24+
option.WithBaseURL(baseURL),
25+
option.WithBearerToken("My Bearer Token"),
26+
)
27+
page, err := client.Environments.Automations.Services.List(context.TODO(), gitpod.EnvironmentAutomationServiceListParams{})
28+
if err != nil {
29+
t.Fatalf("err should be nil: %s", err.Error())
30+
}
31+
for _, service := range page.Pagination.PersonalAccessTokens {
32+
t.Logf("%+v\n", service.Pagination)
33+
}
34+
// Prism mock isn't going to give us real pagination
35+
page, err = page.GetNextPage()
36+
if err != nil {
37+
t.Fatalf("err should be nil: %s", err.Error())
38+
}
39+
if page != nil {
40+
for _, service := range page.Pagination.PersonalAccessTokens {
41+
t.Logf("%+v\n", service.Pagination)
42+
}
43+
}
44+
}

0 commit comments

Comments
 (0)