Skip to content

Commit 60bc727

Browse files
feat(client): add support for streaming raw responses (#261)
As an alternative to `with_raw_response` we now provide `with_streaming_response` as well. When using these methods you will have to use a context manager to ensure that the response is always cleaned up.
1 parent 859a703 commit 60bc727

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

66 files changed

+3446
-478
lines changed

README.md

+27-2
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ if response.my_field is None:
295295

296296
### Accessing raw response data (e.g. headers)
297297

298-
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call.
298+
The "raw" Response object can be accessed by prefixing `.with_raw_response.` to any HTTP method call, e.g.,
299299

300300
```py
301301
from finch import Finch
@@ -308,7 +308,32 @@ directory = response.parse() # get the object that `hris.directory.list()` woul
308308
print(directory.id)
309309
```
310310

311-
These methods return an [`APIResponse`](https://github.com/Finch-API/finch-api-python/tree/main/src/finch/_response.py) object.
311+
These methods return an [`LegacyAPIResponse`](https://github.com/Finch-API/finch-api-python/tree/main/src/finch/_legacy_response.py) object. This is a legacy class as we're changing it slightly in the next major version.
312+
313+
For the sync client this will mostly be the same with the exception
314+
of `content` & `text` will be methods instead of properties. In the
315+
async client, all methods will be async.
316+
317+
A migration script will be provided & the migration in general should
318+
be smooth.
319+
320+
#### `.with_streaming_response`
321+
322+
The above interface eagerly reads the full response body when you make the request, which may not always be what you want.
323+
324+
To stream the response body, use `.with_streaming_response` instead, which requires a context manager and only reads the response body once you call `.read()`, `.text()`, `.json()`, `.iter_bytes()`, `.iter_text()`, `.iter_lines()` or `.parse()`. In the async client, these are async methods.
325+
326+
As such, `.with_streaming_response` methods return a different [`APIResponse`](https://github.com/Finch-API/finch-api-python/tree/main/src/finch/_response.py) object, and the async client returns an [`AsyncAPIResponse`](https://github.com/Finch-API/finch-api-python/tree/main/src/finch/_response.py) object.
327+
328+
```python
329+
with client.hris.directory.with_streaming_response.list() as response:
330+
print(response.headers.get("X-My-Header"))
331+
332+
for line in response.iter_lines():
333+
print(line)
334+
```
335+
336+
The context manager is required so that the response will reliably be closed.
312337

313338
### Configuring the HTTP client
314339

src/finch/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ._utils import file_from_path
66
from ._client import Finch, Client, Stream, Timeout, Transport, AsyncFinch, AsyncClient, AsyncStream, RequestOptions
77
from ._version import __title__, __version__
8+
from ._response import APIResponse as APIResponse, AsyncAPIResponse as AsyncAPIResponse
89
from ._exceptions import (
910
APIError,
1011
FinchError,

0 commit comments

Comments
 (0)