Documents and folders with the Python SDK
Add text, list, read and download documents, change them, and follow the work a write starts.
from byterover import ByteRover
client = ByteRover()
drive_id = "drv_4h7k2m9q8r1t5v6w3x0y"Add a small text document
Text up to 256 KiB goes in one call. A file of any other kind goes through an upload. A document already at the path gets a new revision.
written = client.documents.put_text(
drive_id,
path="/policies/customer-refund-policy.md",
text="# Refunds\n\n## European Union\n\nCustomers in the European Union may cancel an order within 14 days of delivery and receive a full refund.",
tags=["policy"],
)
document, operation = written.document, written.operationWait for the work a write started
A write returns an operation, which tracks the work of making the file searchable. client.operations.wait polls it, at the pace the API asks for, until it is done.
from byterover import OperationFailedError, WaitTimeoutError
try:
done = client.operations.wait(operation.id, timeout=300)
print(done.status)
except OperationFailedError as error:
print(error.code, error.operation.error)
except WaitTimeoutError as error:
print("Still running; wait again for", error.operation_id)It waits 10 minutes by default. When the time runs out, the operation goes on; wait for it again by its ID. A poll that fails on the way, such as a 504 while a big file is processed or a 429, is asked again at the next poll, no sooner than the API asks with Retry-After. The wait does this retrying itself, so a poll isn't retried by max_retries unless you pass it in request; either way, nothing runs past timeout.
List the documents of a drive
client.documents.list goes through every page for you when you loop over it. first_page() gives you one page.
for listed in client.documents.list(drive_id, folder="/policies", sort="updated", order="desc"):
print(listed.path, listed.status)
first_page = client.documents.list(drive_id, limit=200).first_page()
print(len(first_page.documents), first_page.next_cursor)folder lists only the documents directly in one folder, name matches part of a file name, and status (in_ingest or failed) finds the documents that aren't searchable yet. See Pagination and timeouts.
One document and its revisions:
policy = client.documents.retrieve(document.id)
revisions = client.documents.list_revisions(document.id).revisions
print(policy.revision, len(revisions))Read the text
client.documents.read returns the text of one part of a document: a page, slide, sheet, heading_path or byte_range, exactly one of them. It's the next call after a search, with the result's document_id and one selector from its location, such as its page or heading_path. See Read a document.
page = client.documents.read("doc_7n4p1s8v2x5z9b3d6f0h", page=6)
section = client.documents.read(
"doc_7n4p1s8v2x5z9b3d6f0h",
heading_path=["Refunds", "European Union"],
max_chars=8_000,
)
print(page.text, section.truncated)Download the original file
client.documents.content streams the file as it was uploaded. Read it whole with read() or text(), chunk by chunk with iter_bytes(), or save it with write_to_file(), which leaves no partial file when the download fails.
original = client.documents.content(document.id, revision=1)
original.write_to_file("customer-refund-policy.md")If the connection drops, or nothing arrives for the call's timeout, the SDK asks for the rest of the file with a Range request tied to the file's ETag. If the file changed in the meantime, the download fails instead of joining two versions. Pass range={"start": 0, "end": 1023} for part of a large file.
Change, copy and delete
moved = client.documents.update(
document.id,
path="/policies/eu/customer-refund-policy.md",
tags=["policy", "eu"],
expected_version=policy.version,
)
copy = client.documents.copy(document.id, drive_id=drive_id, path="/archive/customer-refund-policy.md")
removal = client.documents.delete(copy.document.id)
client.operations.wait(removal.id)
print(moved.document.path)expected_version makes a change fail with 409 VERSION_MISMATCH if the document changed since you read it. move=True on a copy moves the document into another drive instead. To rename or move a document within its drive, change its path with documents.update(document_id, path=...); the API refuses a same-drive move with 400.
Folders
from is a Python keyword, so folders.move takes from_.
client.folders.create(drive_id, path="/policies/eu")
folders = client.folders.list(drive_id).folders
client.folders.move(drive_id, from_="/policies/eu", to="/policies/europe")
client.folders.delete(drive_id, path="/policies/europe")
print([folder.path for folder in folders])folders.create also makes any parent folder that is missing, so /policies/eu makes /policies too, and creating a folder that exists already succeeds. folders.delete deletes a folder only when no document is in it or in a folder below it, and removes its empty subfolders with it; otherwise it raises ConflictError with code FOLDER_NOT_EMPTY (409). Delete or move the documents first.