|
19 | 19 | from foundry_sdk._core.config import Config |
20 | 20 | from foundry_sdk._core.context_and_environment_vars import HOSTNAME_VAR |
21 | 21 | 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 |
23 | 23 |
|
24 | 24 |
|
25 | 25 | 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) - |
125 | 125 |
|
126 | 126 | Raises: |
127 | 127 | 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. |
129 | 129 | """ |
130 | 130 | if not preview: |
131 | 131 | raise ValueError( |
132 | 132 | "get_http_client() is in beta. " "Please set the preview parameter to True to use it." |
133 | 133 | ) |
134 | | - token = get_foundry_token(preview=True) |
135 | 134 |
|
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) |
137 | 176 | auth_header = {"Authorization": f"Bearer {token}"} |
| 177 | + |
138 | 178 | if config is None: |
139 | | - config = Config(default_headers=auth_header) |
| 179 | + return Config(default_headers=auth_header) |
140 | 180 | else: |
141 | 181 | 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) |
0 commit comments