Python v4 SDKs now handle files. An operation that uploads a binary body or a multipart/form-data form, and one that returns a binary body on success or on error, all generate with typed parameters and typed results.
Details
A file is passed as a Path, bytes, any binary reader or an iterator of chunks, and the same FileInput type is accepted for a raw binary body and for a multipart file part. A Path streams from disk, keeps its Content-Length and names the file on the wire. Wrap any source in NamedFile to set the filename or the media type yourself. A multipart form takes each part as its own parameter: a file, a typed JSON document or a text field.
A binary success response is returned as a FileResponse whose body has not been read yet. Save it to disk, read it into memory or iterate its chunks, and each of those closes the connection when it finishes. A binary error response arrives as a RawError on the raised ApiError, with its bytes in content, so the body is never lost.
The asynchronous client takes AsyncFileInput, which also accepts async readers and async iterables, and returns an AsyncFileResponse.
Usage Example
Upload from a path, override the filename where the source has none, and stream a download to disk:
from pathlib import Path
from payments import PaymentsClient
from payments.core import NamedFile
client = PaymentsClient(bearer_auth="YOUR_ACCESS_TOKEN")
# A Path streams from disk and sends its own filename.
client.documents.upload_document(Path("invoice.pdf"))
# In-memory bytes carry no name, so give them one and the media type the server expects.
client.documents.upload_document(NamedFile(pdf_bytes, filename="invoice.pdf", media_type="application/pdf"))
# The download streams to disk, and save_to releases the connection when it is done.
client.documents.download_document("doc_123").save_to("invoice-copy.pdf")
Every file goes out as application/octet-stream unless you set media_type on a NamedFile.
This feature is available in the Python v4 SDK (beta). Generate it from the APIMatic CLI:
apimatic sdk generate --language=python --codegen-version=v4 --stability=beta
For the full set of flags and options, see the APIMatic CLI documentation.