Streaming File Download
Use download_file() (sync) or adownload_file() (async) to download files without loading the entire response into memory. Data is streamed in chunks to a file path or file-like object.
When to Use
| Approach | Use case |
|---|---|
download_file() / adownload_file() | Large files, memory-constrained environments, progress tracking |
get_raw() | Small files or when you need the full response object |
Basic Usage
Sync: download to path
from intapp_rest_client import RestClient
with RestClient(base_url="https://api.example.com", api_key="key") as client:
# Returns number of bytes written
size = client.download_file("/api/files/report.pdf", "/tmp/report.pdf")
print(f"Downloaded {size} bytes")Sync: download to file object
with open("/tmp/report.pdf", "wb") as f:
size = client.download_file("/api/files/report.pdf", f)Async: adownload_file
import asyncio
from intapp_rest_client import RestClient
async def main():
async with RestClient(base_url="https://api.example.com", api_key="key") as client:
size = await client.adownload_file("/api/files/report.pdf", "/tmp/report.pdf")
asyncio.run(main())Parameters
| Parameter | Type | Description |
|---|---|---|
endpoint | str | API endpoint path |
destination | str, Path, or BinaryIO | File path to write to, or open binary file object |
params | dict | Optional query parameters |
headers | dict | Optional headers |
chunk_size | int | Bytes per chunk (default 8192) |
progress_callback | Callable | Optional (bytes_downloaded, total_bytes) callback |
Progress Callback
def on_progress(downloaded: int, total: Optional[int]):
if total:
pct = 100 * downloaded / total
print(f"{downloaded}/{total} ({pct:.1f}%)")
client.download_file(
"/api/files/large.zip",
"/tmp/large.zip",
progress_callback=on_progress
)See Also
- API Reference: download_file – Full signature and async table
- Async Operations – Async usage and
adownload_file()