You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
Copy file name to clipboardExpand all lines: README.md
+27-2
Original file line number
Diff line number
Diff line change
@@ -295,7 +295,7 @@ if response.my_field is None:
295
295
296
296
### Accessing raw response data (e.g. headers)
297
297
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.,
299
299
300
300
```py
301
301
from finch import Finch
@@ -308,7 +308,32 @@ directory = response.parse() # get the object that `hris.directory.list()` woul
308
308
print(directory.id)
309
309
```
310
310
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.
0 commit comments