From e1020c8ba099e4be4d8e332a0faa210f3598ba4c Mon Sep 17 00:00:00 2001 From: Daan Kolthof Date: Sat, 11 Apr 2026 01:40:24 +0100 Subject: [PATCH 1/4] Initial implementation of get_async_http_client --- foundry_sdk/v2/language_models/utils.py | 33 ++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/foundry_sdk/v2/language_models/utils.py b/foundry_sdk/v2/language_models/utils.py index ee0b6cc5..8f0ba9d0 100644 --- a/foundry_sdk/v2/language_models/utils.py +++ b/foundry_sdk/v2/language_models/utils.py @@ -19,7 +19,7 @@ from foundry_sdk._core.config import Config from foundry_sdk._core.context_and_environment_vars import HOSTNAME_VAR from foundry_sdk._core.context_and_environment_vars import TOKEN_VAR -from foundry_sdk._core.http_client import HttpClient +from foundry_sdk._core.http_client import HttpClient, AsyncHttpClient def _get_api_gateway_base_url(*, preview: bool = False) -> str: @@ -142,3 +142,34 @@ def get_http_client(*, preview: bool = False, config: Optional[Config] = None) - config = replace(config, default_headers=merged_headers) return HttpClient(config=config) + + +def get_async_http_client(*, preview: bool = False, config: Optional[Config] = None) -> AsyncHttpClient: + """Get an async HTTP client configured for the current Foundry environment. + + Args: + preview: Must be set to True to use this beta feature. + config: Optional configuration for the async HTTP client. + + Returns: + An AsyncHttpClient instance configured with the Foundry hostname and authentication. + + Raises: + ValueError: If preview is not set to True. + RuntimeError: If the Foundry API gateway base URL or token is not available in the current context. + """ + if not preview: + raise ValueError( + "get_async_http_client() is in beta. " "Please set the preview parameter to True to use it." + ) + token = get_foundry_token(preview=True) + + # Merge auth header with any user-provided headers + auth_header = {"Authorization": f"Bearer {token}"} + if config is None: + config = Config(default_headers=auth_header) + else: + merged_headers = {**auth_header, **(config.default_headers or {})} + config = replace(config, default_headers=merged_headers) + + return AsyncHttpClient(config=config) From 3e1ee08f158ee2d1985092bfde55d64b947ae290 Mon Sep 17 00:00:00 2001 From: Daan Kolthof Date: Sat, 11 Apr 2026 01:55:20 +0100 Subject: [PATCH 2/4] Format edited file(s) --- foundry_sdk/v2/language_models/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/foundry_sdk/v2/language_models/utils.py b/foundry_sdk/v2/language_models/utils.py index 8f0ba9d0..41413f2c 100644 --- a/foundry_sdk/v2/language_models/utils.py +++ b/foundry_sdk/v2/language_models/utils.py @@ -144,7 +144,9 @@ def get_http_client(*, preview: bool = False, config: Optional[Config] = None) - return HttpClient(config=config) -def get_async_http_client(*, preview: bool = False, config: Optional[Config] = None) -> AsyncHttpClient: +def get_async_http_client( + *, preview: bool = False, config: Optional[Config] = None +) -> AsyncHttpClient: """Get an async HTTP client configured for the current Foundry environment. Args: @@ -160,7 +162,8 @@ def get_async_http_client(*, preview: bool = False, config: Optional[Config] = N """ if not preview: raise ValueError( - "get_async_http_client() is in beta. " "Please set the preview parameter to True to use it." + "get_async_http_client() is in beta. " + "Please set the preview parameter to True to use it." ) token = get_foundry_token(preview=True) From 932205257567789a9e4a53f6b4f463b316df7bea Mon Sep 17 00:00:00 2001 From: Daan Kolthof Date: Sat, 11 Apr 2026 02:07:55 +0100 Subject: [PATCH 3/4] Refactor utils.py, move common logic to separate util function --- foundry_sdk/v2/language_models/utils.py | 33 +++++++++++++------------ 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/foundry_sdk/v2/language_models/utils.py b/foundry_sdk/v2/language_models/utils.py index 41413f2c..2af8a1af 100644 --- a/foundry_sdk/v2/language_models/utils.py +++ b/foundry_sdk/v2/language_models/utils.py @@ -131,17 +131,8 @@ def get_http_client(*, preview: bool = False, config: Optional[Config] = None) - raise ValueError( "get_http_client() is in beta. " "Please set the preview parameter to True to use it." ) - token = get_foundry_token(preview=True) - # Merge auth header with any user-provided headers - auth_header = {"Authorization": f"Bearer {token}"} - if config is None: - config = Config(default_headers=auth_header) - else: - merged_headers = {**auth_header, **(config.default_headers or {})} - config = replace(config, default_headers=merged_headers) - - return HttpClient(config=config) + return HttpClient(config=_create_http_client_config(config)) def get_async_http_client( @@ -165,14 +156,24 @@ def get_async_http_client( "get_async_http_client() is in beta. " "Please set the preview parameter to True to use it." ) - token = get_foundry_token(preview=True) - # Merge auth header with any user-provided headers + return AsyncHttpClient(config=_create_http_client_config(config)) + + +def _create_http_client_config(config: Optional[Config]) -> Config: + """Create a Config object for the HTTP client, ensuring that the Foundry token is included in the headers. + + Args: + config: An optional Config object provided by the user. + + Returns: + A Config object with the Foundry token included in the default headers. + """ + token = get_foundry_token(preview=True) auth_header = {"Authorization": f"Bearer {token}"} + if config is None: - config = Config(default_headers=auth_header) + return Config(default_headers=auth_header) else: merged_headers = {**auth_header, **(config.default_headers or {})} - config = replace(config, default_headers=merged_headers) - - return AsyncHttpClient(config=config) + return replace(config, default_headers=merged_headers) From fede5f4c7244c3fff91a5634218c53ad146ce6c1 Mon Sep 17 00:00:00 2001 From: Daan Kolthof Date: Sat, 11 Apr 2026 02:38:58 +0100 Subject: [PATCH 4/4] Update docstrings to reflect code behaviour --- foundry_sdk/v2/language_models/utils.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/foundry_sdk/v2/language_models/utils.py b/foundry_sdk/v2/language_models/utils.py index 2af8a1af..eed4c7b4 100644 --- a/foundry_sdk/v2/language_models/utils.py +++ b/foundry_sdk/v2/language_models/utils.py @@ -125,7 +125,7 @@ def get_http_client(*, preview: bool = False, config: Optional[Config] = None) - Raises: ValueError: If preview is not set to True. - RuntimeError: If the Foundry API gateway base URL or token is not available in the current context. + RuntimeError: If the Foundry token is not available in the current context. """ if not preview: raise ValueError( @@ -149,7 +149,7 @@ def get_async_http_client( Raises: ValueError: If preview is not set to True. - RuntimeError: If the Foundry API gateway base URL or token is not available in the current context. + RuntimeError: If the Foundry token is not available in the current context. """ if not preview: raise ValueError( @@ -168,6 +168,9 @@ def _create_http_client_config(config: Optional[Config]) -> Config: Returns: A Config object with the Foundry token included in the default headers. + + Raises: + RuntimeError: If the Foundry token is not available in the current context. """ token = get_foundry_token(preview=True) auth_header = {"Authorization": f"Bearer {token}"}