Advanced
Streaming File Download

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

ApproachUse 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

ParameterTypeDescription
endpointstrAPI endpoint path
destinationstr, Path, or BinaryIOFile path to write to, or open binary file object
paramsdictOptional query parameters
headersdictOptional headers
chunk_sizeintBytes per chunk (default 8192)
progress_callbackCallableOptional (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