Skip to content

Commit bc99f4f

Browse files
fix: update paginated endpoints (#12)
1 parent 8163f09 commit bc99f4f

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

README.md

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

8282
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`.
8383

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

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

0 commit comments

Comments
 (0)