Skip to content

Commit 128a275

Browse files
stainless-app[bot]stainless-bot
authored andcommitted
feat(client): add DefaultHttpxClient and DefaultAsyncHttpxClient (#224)
1 parent b19a196 commit 128a275

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

README.md

+2-3
Original file line numberDiff line numberDiff line change
@@ -402,13 +402,12 @@ You can directly override the [httpx client](https://www.python-httpx.org/api/#c
402402
- Additional [advanced](https://www.python-httpx.org/advanced/#client-instances) functionality
403403

404404
```python
405-
import httpx
406-
from orb import Orb
405+
from orb import Orb, DefaultHttpxClient
407406

408407
client = Orb(
409408
# Or use the `ORB_BASE_URL` env var
410409
base_url="http://my.test.server.example.com:8083",
411-
http_client=httpx.Client(
410+
http_client=DefaultHttpxClient(
412411
proxies="http://my.test.proxy.example.com",
413412
transport=httpx.HTTPTransport(local_address="0.0.0.0"),
414413
),

src/orb/__init__.py

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
DuplicateResourceCreation,
3737
APIResponseValidationError,
3838
)
39+
from ._base_client import DefaultHttpxClient, DefaultAsyncHttpxClient
3940
from ._utils._logs import setup_logging as _setup_logging
4041

4142
__all__ = [
@@ -86,6 +87,8 @@
8687
"DEFAULT_TIMEOUT",
8788
"DEFAULT_MAX_RETRIES",
8889
"DEFAULT_CONNECTION_LIMITS",
90+
"DefaultHttpxClient",
91+
"DefaultAsyncHttpxClient",
8992
]
9093

9194
_setup_logging()

src/orb/_base_client.py

+42-2
Original file line numberDiff line numberDiff line change
@@ -716,7 +716,27 @@ def _idempotency_key(self) -> str:
716716
return f"stainless-python-retry-{uuid.uuid4()}"
717717

718718

719-
class SyncHttpxClientWrapper(httpx.Client):
719+
class _DefaultHttpxClient(httpx.Client):
720+
def __init__(self, **kwargs: Any) -> None:
721+
kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
722+
kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
723+
kwargs.setdefault("follow_redirects", True)
724+
super().__init__(**kwargs)
725+
726+
727+
if TYPE_CHECKING:
728+
DefaultHttpxClient = httpx.Client
729+
"""An alias to `httpx.Client` that provides the same defaults that this SDK
730+
uses internally.
731+
732+
This is useful because overriding the `http_client` with your own instance of
733+
`httpx.Client` will result in httpx's defaults being used, not ours.
734+
"""
735+
else:
736+
DefaultHttpxClient = _DefaultHttpxClient
737+
738+
739+
class SyncHttpxClientWrapper(DefaultHttpxClient):
720740
def __del__(self) -> None:
721741
try:
722742
self.close()
@@ -1262,7 +1282,27 @@ def get_api_list(
12621282
return self._request_api_list(model, page, opts)
12631283

12641284

1265-
class AsyncHttpxClientWrapper(httpx.AsyncClient):
1285+
class _DefaultAsyncHttpxClient(httpx.AsyncClient):
1286+
def __init__(self, **kwargs: Any) -> None:
1287+
kwargs.setdefault("timeout", DEFAULT_TIMEOUT)
1288+
kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS)
1289+
kwargs.setdefault("follow_redirects", True)
1290+
super().__init__(**kwargs)
1291+
1292+
1293+
if TYPE_CHECKING:
1294+
DefaultAsyncHttpxClient = httpx.AsyncClient
1295+
"""An alias to `httpx.AsyncClient` that provides the same defaults that this SDK
1296+
uses internally.
1297+
1298+
This is useful because overriding the `http_client` with your own instance of
1299+
`httpx.AsyncClient` will result in httpx's defaults being used, not ours.
1300+
"""
1301+
else:
1302+
DefaultAsyncHttpxClient = _DefaultAsyncHttpxClient
1303+
1304+
1305+
class AsyncHttpxClientWrapper(DefaultAsyncHttpxClient):
12661306
def __del__(self) -> None:
12671307
try:
12681308
# TODO(someday): support non asyncio runtimes here

src/orb/_client.py

+6-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,9 @@ def __init__(
7878
max_retries: int = DEFAULT_MAX_RETRIES,
7979
default_headers: Mapping[str, str] | None = None,
8080
default_query: Mapping[str, object] | None = None,
81-
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
81+
# Configure a custom httpx client.
82+
# We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
83+
# See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details.
8284
http_client: httpx.Client | None = None,
8385
# Enable or disable schema validation for data returned by the API.
8486
# When enabled an error APIResponseValidationError is raised
@@ -330,7 +332,9 @@ def __init__(
330332
max_retries: int = DEFAULT_MAX_RETRIES,
331333
default_headers: Mapping[str, str] | None = None,
332334
default_query: Mapping[str, object] | None = None,
333-
# Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details.
335+
# Configure a custom httpx client.
336+
# We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`.
337+
# See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details.
334338
http_client: httpx.AsyncClient | None = None,
335339
# Enable or disable schema validation for data returned by the API.
336340
# When enabled an error APIResponseValidationError is raised

0 commit comments

Comments
 (0)