Skip to content

Commit c952056

Browse files
committed
feat(client): add support for streaming raw responses (#132)
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 e15010d commit c952056

64 files changed

Lines changed: 5905 additions & 547 deletions

Some content is hidden

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

README.md

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ if response.my_field is None:
288288

289289
### Accessing raw response data (e.g. headers)
290290

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

293293
```py
294294
from orb import Orb
@@ -304,7 +304,35 @@ customer = response.parse() # get the object that `customers.create()` would ha
304304
print(customer.id)
305305
```
306306

307-
These methods return an [`APIResponse`](https://github.com/orbcorp/orb-python/tree/main/src/orb/_response.py) object.
307+
These methods return an [`LegacyAPIResponse`](https://github.com/orbcorp/orb-python/tree/main/src/orb/_legacy_response.py) object. This is a legacy class as we're changing it slightly in the next major version.
308+
309+
For the sync client this will mostly be the same with the exception
310+
of `content` & `text` will be methods instead of properties. In the
311+
async client, all methods will be async.
312+
313+
A migration script will be provided & the migration in general should
314+
be smooth.
315+
316+
#### `.with_streaming_response`
317+
318+
The above interface eagerly reads the full response body when you make the request, which may not always be what you want.
319+
320+
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.
321+
322+
As such, `.with_streaming_response` methods return a different [`APIResponse`](https://github.com/orbcorp/orb-python/tree/main/src/orb/_response.py) object, and the async client returns an [`AsyncAPIResponse`](https://github.com/orbcorp/orb-python/tree/main/src/orb/_response.py) object.
323+
324+
```python
325+
with client.customers.with_streaming_response.create(
326+
email="example-customer@withorb.com",
327+
name="My Customer",
328+
) as response:
329+
print(response.headers.get("X-My-Header"))
330+
331+
for line in response.iter_lines():
332+
print(line)
333+
```
334+
335+
The context manager is required so that the response will reliably be closed.
308336

309337
### Configuring the HTTP client
310338

src/orb/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from ._utils import file_from_path
66
from ._client import Orb, Client, Stream, Timeout, AsyncOrb, Transport, 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
OrbError,

0 commit comments

Comments
 (0)