Pagination and timeouts in the Python SDK
Go through every page of a list, set a call's timeout, and stop a long call.
Pages
A list call, such as client.documents.list, returns a list you can use in three ways:
from byterover import ByteRover
client = ByteRover()
drive_id = "drv_4h7k2m9q8r1t5v6w3x0y"
# Every document, page after page.
for document in client.documents.list(drive_id):
print(document.path)
# One page: the API's response, with next_cursor.
first = client.documents.list(drive_id, limit=50).first_page()
# Every page.
for page in client.documents.list(drive_id).pages():
print(len(page.documents))
# The next page yourself, from a cursor you kept.
if first.next_cursor:
second = client.documents.list(drive_id, cursor=first.next_cursor).first_page()
print(len(second.documents))Nothing is fetched until you loop or ask for a page, and the SDK asks for the next page only when your loop needs it. Leaving the loop early stops there.
Timeouts
timeout is in seconds: 60 by default, 105 for a search at medium and 135 at high. Set it on the client or on one call; a search keeps its own unless you pass timeout on that call.
It bounds each step of an attempt, not the attempt as a whole. Waiting for a free connection, connecting, each write and each read get up to timeout (httpx's pool, connect, write and read timeouts). Once timeout has passed since the attempt started, the SDK also stops reading the response at the next chunk that arrives. So an attempt usually ends within timeout, but a server that answers slowly, bit by bit, can make it take longer. A download fails when nothing arrives for timeout; a download that keeps arriving has no total limit.
quick = client.drives.list(timeout=5)
everything = client.operations.wait(
"op_3g6j9m2p5s8v1y4b7e0h",
timeout=900, # how long to keep waiting
request={"timeout": 10}, # each poll
)
print(len(quick.drives), everything.status)A call that runs out of time raises an APITimeoutError. A wait's timeout covers the whole wait, its polls and their retries included; one that runs out of time raises a WaitTimeoutError. The operation goes on, and you can wait for it again.
Stop a call
The client is sync: a call returns when it is done, or when its timeout runs out. To give up on a slow call sooner, pass a shorter timeout. Stopping your program (Ctrl-C) stops a call too. An upload raises at once and starts no more parts; a part already being sent can't be cut, so it finishes in the background, or times out under part_timeout, and Python waits for it before the process exits.
Stopping stops your side only. The API may still finish what it started, and charge a search that ran. There is no call that cancels work on the server.