Subclass of APIResponse providing helpers for dealing with binary data. Note: If you want to stream the response data instead of eagerly reading it all at once then you should use `.with_streaming_response` when making the API request, e.g. `.with_streaming_response.get_binary_response(
| 497 | |
| 498 | |
| 499 | class AsyncBinaryAPIResponse(AsyncAPIResponse[bytes]): |
| 500 | """Subclass of APIResponse providing helpers for dealing with binary data. |
| 501 | |
| 502 | Note: If you want to stream the response data instead of eagerly reading it |
| 503 | all at once then you should use `.with_streaming_response` when making |
| 504 | the API request, e.g. `.with_streaming_response.get_binary_response()` |
| 505 | """ |
| 506 | |
| 507 | async def write_to_file( |
| 508 | self, |
| 509 | file: str | os.PathLike[str], |
| 510 | ) -> None: |
| 511 | """Write the output to the given file. |
| 512 | |
| 513 | Accepts a filename or any path-like object, e.g. pathlib.Path |
| 514 | |
| 515 | Note: if you want to stream the data to the file instead of writing |
| 516 | all at once then you should use `.with_streaming_response` when making |
| 517 | the API request, e.g. `.with_streaming_response.get_binary_response()` |
| 518 | """ |
| 519 | path = anyio.Path(file) |
| 520 | async with await path.open(mode="wb") as f: |
| 521 | async for data in self.iter_bytes(): |
| 522 | await f.write(data) |
| 523 | |
| 524 | |
| 525 | class StreamedBinaryAPIResponse(APIResponse[bytes]): |
nothing calls this directly
no outgoing calls
no test coverage detected