Skip to content

Commit 6840636

Browse files
authored
Implement get_async_http_client function for async Foundry HTTP clients (#341)
1 parent 00e6286 commit 6840636

3 files changed

Lines changed: 64 additions & 10 deletions

File tree

assets/language_model_utils.py

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from foundry_sdk._core.config import Config
2020
from foundry_sdk._core.context_and_environment_vars import HOSTNAME_VAR
2121
from foundry_sdk._core.context_and_environment_vars import TOKEN_VAR
22-
from foundry_sdk._core.http_client import HttpClient
22+
from foundry_sdk._core.http_client import HttpClient, AsyncHttpClient
2323

2424

2525
def _get_api_gateway_base_url(*, preview: bool = False) -> str:
@@ -125,20 +125,58 @@ def get_http_client(*, preview: bool = False, config: Optional[Config] = None) -
125125
126126
Raises:
127127
ValueError: If preview is not set to True.
128-
RuntimeError: If the Foundry API gateway base URL or token is not available in the current context.
128+
RuntimeError: If the Foundry token is not available in the current context.
129129
"""
130130
if not preview:
131131
raise ValueError(
132132
"get_http_client() is in beta. " "Please set the preview parameter to True to use it."
133133
)
134-
token = get_foundry_token(preview=True)
135134

136-
# Merge auth header with any user-provided headers
135+
return HttpClient(config=_create_http_client_config(config))
136+
137+
138+
def get_async_http_client(
139+
*, preview: bool = False, config: Optional[Config] = None
140+
) -> AsyncHttpClient:
141+
"""Get an async HTTP client configured for the current Foundry environment.
142+
143+
Args:
144+
preview: Must be set to True to use this beta feature.
145+
config: Optional configuration for the async HTTP client.
146+
147+
Returns:
148+
An AsyncHttpClient instance configured with the Foundry hostname and authentication.
149+
150+
Raises:
151+
ValueError: If preview is not set to True.
152+
RuntimeError: If the Foundry token is not available in the current context.
153+
"""
154+
if not preview:
155+
raise ValueError(
156+
"get_async_http_client() is in beta. "
157+
"Please set the preview parameter to True to use it."
158+
)
159+
160+
return AsyncHttpClient(config=_create_http_client_config(config))
161+
162+
163+
def _create_http_client_config(config: Optional[Config]) -> Config:
164+
"""Create a Config object for the HTTP client, ensuring that the Foundry token is included in the headers.
165+
166+
Args:
167+
config: An optional Config object provided by the user.
168+
169+
Returns:
170+
A Config object with the Foundry token included in the default headers.
171+
172+
Raises:
173+
RuntimeError: If the Foundry token is not available in the current context.
174+
"""
175+
token = get_foundry_token(preview=True)
137176
auth_header = {"Authorization": f"Bearer {token}"}
177+
138178
if config is None:
139-
config = Config(default_headers=auth_header)
179+
return Config(default_headers=auth_header)
140180
else:
141181
merged_headers = {**auth_header, **(config.default_headers or {})}
142-
config = replace(config, default_headers=merged_headers)
143-
144-
return HttpClient(config=config)
182+
return replace(config, default_headers=merged_headers)

assets/test_language_model_utils.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@
1717

1818
from foundry_sdk._core.context_and_environment_vars import HOSTNAME_VAR
1919
from foundry_sdk._core.context_and_environment_vars import TOKEN_VAR
20-
from foundry_sdk._core.http_client import HttpClient
20+
from foundry_sdk._core.http_client import HttpClient, AsyncHttpClient
2121
from foundry_sdk.v2.language_models import (
2222
get_anthropic_base_url,
2323
get_foundry_token,
2424
get_http_client,
25+
get_async_http_client,
2526
get_openai_base_url,
2627
)
2728
from foundry_sdk.v2.language_models.utils import _get_api_gateway_base_url
@@ -177,3 +178,17 @@ def test_raises_runtime_error_when_token_not_in_context(self):
177178
get_http_client(preview=True)
178179
finally:
179180
HOSTNAME_VAR.reset(hostname_token)
181+
182+
183+
class TestGetAsyncHttpClient:
184+
"""Test get_async_http_client function."""
185+
186+
def test_returns_async_http_client(self):
187+
hostname_token = HOSTNAME_VAR.set("test.palantirfoundry.com")
188+
auth_token = TOKEN_VAR.set("test-token-12345")
189+
try:
190+
result = get_async_http_client(preview=True)
191+
assert isinstance(result, AsyncHttpClient)
192+
finally:
193+
HOSTNAME_VAR.reset(hostname_token)
194+
TOKEN_VAR.reset(auth_token)

config.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@
186186
"from foundry_sdk.v2.language_models.utils import get_foundry_token",
187187
"from foundry_sdk.v2.language_models.utils import get_openai_base_url",
188188
"from foundry_sdk.v2.language_models.utils import get_anthropic_base_url",
189-
"from foundry_sdk.v2.language_models.utils import get_http_client"
189+
"from foundry_sdk.v2.language_models.utils import get_http_client",
190+
"from foundry_sdk.v2.language_models.utils import get_async_http_client"
190191
]
191192
},
192193
"MapRendering": false,

0 commit comments

Comments
 (0)