diff --git a/.release-please-manifest.json b/.release-please-manifest.json index 463488b6..4ad3fef3 100644 --- a/.release-please-manifest.json +++ b/.release-please-manifest.json @@ -1,3 +1,3 @@ { - ".": "0.17.1" + ".": "0.18.0" } \ No newline at end of file diff --git a/CHANGELOG.md b/CHANGELOG.md index 53df7203..b913c343 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,13 @@ # Changelog +## 0.18.0 (2024-04-08) + +Full Changelog: [v0.17.1...v0.18.0](https://github.com/Finch-API/finch-api-python/compare/v0.17.1...v0.18.0) + +### Features + +* **client:** add DefaultHttpxClient and DefaultAsyncHttpxClient ([#342](https://github.com/Finch-API/finch-api-python/issues/342)) ([399b634](https://github.com/Finch-API/finch-api-python/commit/399b634a14156f5d6cba68ed3cc74f49edf1920c)) + ## 0.17.1 (2024-04-04) Full Changelog: [v0.17.0...v0.17.1](https://github.com/Finch-API/finch-api-python/compare/v0.17.0...v0.17.1) diff --git a/README.md b/README.md index 8e807ac0..d83b21ef 100644 --- a/README.md +++ b/README.md @@ -380,13 +380,12 @@ You can directly override the [httpx client](https://www.python-httpx.org/api/#c - Additional [advanced](https://www.python-httpx.org/advanced/#client-instances) functionality ```python -import httpx -from finch import Finch +from finch import Finch, DefaultHttpxClient client = Finch( # Or use the `FINCH_BASE_URL` env var base_url="http://my.test.server.example.com:8083", - http_client=httpx.Client( + http_client=DefaultHttpxClient( proxies="http://my.test.proxy.example.com", transport=httpx.HTTPTransport(local_address="0.0.0.0"), ), diff --git a/pyproject.toml b/pyproject.toml index 2d143e28..92ecf7d4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [project] name = "finch-api" -version = "0.17.1" +version = "0.18.0" description = "The official Python library for the Finch API" dynamic = ["readme"] license = "Apache-2.0" diff --git a/src/finch/__init__.py b/src/finch/__init__.py index 61b87181..0ca73426 100644 --- a/src/finch/__init__.py +++ b/src/finch/__init__.py @@ -24,6 +24,7 @@ UnprocessableEntityError, APIResponseValidationError, ) +from ._base_client import DefaultHttpxClient, DefaultAsyncHttpxClient from ._utils._logs import setup_logging as _setup_logging __all__ = [ @@ -62,6 +63,8 @@ "DEFAULT_TIMEOUT", "DEFAULT_MAX_RETRIES", "DEFAULT_CONNECTION_LIMITS", + "DefaultHttpxClient", + "DefaultAsyncHttpxClient", ] _setup_logging() diff --git a/src/finch/_base_client.py b/src/finch/_base_client.py index ebf39f15..b936cf2b 100644 --- a/src/finch/_base_client.py +++ b/src/finch/_base_client.py @@ -716,7 +716,27 @@ def _idempotency_key(self) -> str: return f"stainless-python-retry-{uuid.uuid4()}" -class SyncHttpxClientWrapper(httpx.Client): +class _DefaultHttpxClient(httpx.Client): + def __init__(self, **kwargs: Any) -> None: + kwargs.setdefault("timeout", DEFAULT_TIMEOUT) + kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) + kwargs.setdefault("follow_redirects", True) + super().__init__(**kwargs) + + +if TYPE_CHECKING: + DefaultHttpxClient = httpx.Client + """An alias to `httpx.Client` that provides the same defaults that this SDK + uses internally. + + This is useful because overriding the `http_client` with your own instance of + `httpx.Client` will result in httpx's defaults being used, not ours. + """ +else: + DefaultHttpxClient = _DefaultHttpxClient + + +class SyncHttpxClientWrapper(DefaultHttpxClient): def __del__(self) -> None: try: self.close() @@ -1262,7 +1282,27 @@ def get_api_list( return self._request_api_list(model, page, opts) -class AsyncHttpxClientWrapper(httpx.AsyncClient): +class _DefaultAsyncHttpxClient(httpx.AsyncClient): + def __init__(self, **kwargs: Any) -> None: + kwargs.setdefault("timeout", DEFAULT_TIMEOUT) + kwargs.setdefault("limits", DEFAULT_CONNECTION_LIMITS) + kwargs.setdefault("follow_redirects", True) + super().__init__(**kwargs) + + +if TYPE_CHECKING: + DefaultAsyncHttpxClient = httpx.AsyncClient + """An alias to `httpx.AsyncClient` that provides the same defaults that this SDK + uses internally. + + This is useful because overriding the `http_client` with your own instance of + `httpx.AsyncClient` will result in httpx's defaults being used, not ours. + """ +else: + DefaultAsyncHttpxClient = _DefaultAsyncHttpxClient + + +class AsyncHttpxClientWrapper(DefaultAsyncHttpxClient): def __del__(self) -> None: try: # TODO(someday): support non asyncio runtimes here diff --git a/src/finch/_client.py b/src/finch/_client.py index 38ed6d91..246de6a3 100644 --- a/src/finch/_client.py +++ b/src/finch/_client.py @@ -85,7 +85,9 @@ def __init__( max_retries: int = DEFAULT_MAX_RETRIES, default_headers: Mapping[str, str] | None = None, default_query: Mapping[str, object] | None = None, - # Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details. + # Configure a custom httpx client. + # We provide a `DefaultHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. + # See the [httpx documentation](https://www.python-httpx.org/api/#client) for more details. http_client: httpx.Client | None = None, # See httpx documentation for [custom transports](https://www.python-httpx.org/advanced/#custom-transports) transport: Transport | None = None, @@ -433,7 +435,9 @@ def __init__( max_retries: int = DEFAULT_MAX_RETRIES, default_headers: Mapping[str, str] | None = None, default_query: Mapping[str, object] | None = None, - # Configure a custom httpx client. See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details. + # Configure a custom httpx client. + # We provide a `DefaultAsyncHttpxClient` class that you can pass to retain the default values we use for `limits`, `timeout` & `follow_redirects`. + # See the [httpx documentation](https://www.python-httpx.org/api/#asyncclient) for more details. http_client: httpx.AsyncClient | None = None, # See httpx documentation for [custom transports](https://www.python-httpx.org/advanced/#custom-transports) transport: AsyncTransport | None = None, diff --git a/src/finch/_version.py b/src/finch/_version.py index ddac6293..4c105f7f 100644 --- a/src/finch/_version.py +++ b/src/finch/_version.py @@ -1,4 +1,4 @@ # File generated from our OpenAPI spec by Stainless. See CONTRIBUTING.md for details. __title__ = "finch" -__version__ = "0.17.1" # x-release-please-version +__version__ = "0.18.0" # x-release-please-version