Skip to content

Commit 0d38bcf

Browse files
feat(api): update via SDK Studio (#7)
1 parent 62aa3ad commit 0d38bcf

File tree

1 file changed

+71
-0
lines changed

1 file changed

+71
-0
lines changed

README.md

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

7878
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`.
7979

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

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

0 commit comments

Comments
 (0)