1717
1818from .config import Environment , validate_client_settings
1919from .config import config as _config
20- from .http import AsyncHTTPClient , HTTPXTransport
21- from .http_client import _SyncHTTPClient
20+ from .http import HTTPXTransport
21+ from .http_client import _AsyncHTTPClient , _SyncHTTPClient
2222
2323API_KEY_ENV_VAR = "SHADE_API_KEY"
2424ENVIRONMENT_ENV_VAR = "SHADE_ENVIRONMENT"
@@ -105,13 +105,7 @@ def __init__(
105105 max_retries = self ._max_retries ,
106106 timeout = self ._timeout ,
107107 )
108- self ._async_http = AsyncHTTPClient (
109- base_url = self ._api_base ,
110- api_key = self ._api_key ,
111- environment = self ._environment ,
112- max_retries = self ._max_retries ,
113- timeout = self ._timeout ,
114- )
108+ self ._async_http_instance : Optional [_AsyncHTTPClient ] = None
115109 self ._client = HTTPXTransport (
116110 api_key = self ._api_key ,
117111 base_url = self ._api_base ,
@@ -121,6 +115,18 @@ def __init__(
121115 http_client = http_client ,
122116 )
123117
118+ @property
119+ def _async_http (self ) -> _AsyncHTTPClient :
120+ if self ._async_http_instance is None :
121+ self ._async_http_instance = _AsyncHTTPClient (
122+ api_base = self ._api_base ,
123+ api_key = self ._api_key ,
124+ environment = self ._environment ,
125+ max_retries = self ._max_retries ,
126+ timeout = self ._timeout ,
127+ )
128+ return self ._async_http_instance
129+
124130 @classmethod
125131 def from_env (cls , ** overrides : Any ) -> "ShadeClient" :
126132 """Build a client from ``SHADE_API_KEY`` and ``SHADE_ENVIRONMENT``.
@@ -154,7 +160,8 @@ def api_key(self) -> Optional[str]:
154160 def api_key (self , value : Optional [str ]) -> None :
155161 self ._api_key = value
156162 self ._http .api_key = value
157- self ._async_http .api_key = value
163+ if self ._async_http_instance is not None :
164+ self ._async_http_instance .api_key = value
158165 self ._client .api_key = value
159166
160167 @property
@@ -168,7 +175,8 @@ def environment(self, value: str | Environment) -> None:
168175 parsed = _config .parse_environment (value )
169176 self ._environment = parsed
170177 self ._http .environment = parsed
171- self ._async_http .environment = parsed
178+ if self ._async_http_instance is not None :
179+ self ._async_http_instance .environment = parsed
172180 self ._client .environment = parsed
173181
174182 @property
@@ -196,12 +204,24 @@ def close(self) -> None:
196204 self ._http .close ()
197205 self ._client .close ()
198206
207+ async def aclose (self ) -> None :
208+ self .close ()
209+ if self ._async_http_instance is not None :
210+ await self ._async_http_instance .aclose ()
211+ self ._async_http_instance = None
212+
199213 def __enter__ (self ) -> "ShadeClient" :
200214 return self
201215
202216 def __exit__ (self , * args : Any ) -> None :
203217 self .close ()
204218
219+ async def __aenter__ (self ) -> "ShadeClient" :
220+ return self
221+
222+ async def __aexit__ (self , * args : Any ) -> None :
223+ await self .aclose ()
224+
205225 def request (
206226 self ,
207227 method : str ,
0 commit comments