Skip to content

Commit 8ddfb41

Browse files
fix: pagination example
1 parent ba50863 commit 8ddfb41

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,69 @@ Nested request parameters are [TypedDicts](https://docs.python.org/3/library/typ
7676

7777
Typed requests and responses provide autocomplete and documentation within your editor. If you would like to see type errors in VS Code to help catch bugs earlier, set `python.analysis.typeCheckingMode` to `basic`.
7878

79+
## Pagination
80+
81+
List methods in the Gitpod API are paginated.
82+
83+
This library provides auto-paginating iterators with each list response, so you do not have to request successive pages manually:
84+
85+
```python
86+
from gitpod import Gitpod
87+
88+
client = Gitpod()
89+
90+
all_services = []
91+
# Automatically fetches more pages as needed.
92+
for service in client.environments.automations.services.list():
93+
# Do something with service here
94+
all_services.append(service)
95+
print(all_services)
96+
```
97+
98+
Or, asynchronously:
99+
100+
```python
101+
import asyncio
102+
from gitpod import AsyncGitpod
103+
104+
client = AsyncGitpod()
105+
106+
107+
async def main() -> None:
108+
all_services = []
109+
# Iterate through items across all pages, issuing requests as needed.
110+
async for service in client.environments.automations.services.list():
111+
all_services.append(service)
112+
print(all_services)
113+
114+
115+
asyncio.run(main())
116+
```
117+
118+
Alternatively, you can use the `.has_next_page()`, `.next_page_info()`, or `.get_next_page()` methods for more granular control working with pages:
119+
120+
```python
121+
first_page = await client.environments.automations.services.list()
122+
if first_page.has_next_page():
123+
print(f"will fetch next page using these details: {first_page.next_page_info()}")
124+
next_page = await first_page.get_next_page()
125+
print(f"number of items we just fetched: {len(next_page.pagination.personal_access_tokens)}")
126+
127+
# Remove `await` for non-async usage.
128+
```
129+
130+
Or just work directly with the returned data:
131+
132+
```python
133+
first_page = await client.environments.automations.services.list()
134+
135+
print(f"next page cursor: {first_page.pagination.next_token}") # => "next page cursor: ..."
136+
for service in first_page.pagination.personal_access_tokens:
137+
print(service.pagination)
138+
139+
# Remove `await` for non-async usage.
140+
```
141+
79142
## Handling errors
80143

81144
When the library is unable to connect to the API (for example, due to network connection problems or a timeout), a subclass of `gitpod.APIConnectionError` is raised.

0 commit comments

Comments
 (0)