1616 Union ,
1717)
1818
19- try :
20- import httpx2 as httpx
21- except ImportError :
22- import httpx
19+ import httpx2
2320from typing_extensions import Unpack
2421
2522from replicate .__about__ import __version__
4340class Client :
4441 """A Replicate API client library"""
4542
46- __client : Optional [httpx .Client ] = None
47- __async_client : Optional [httpx .AsyncClient ] = None
43+ __client : Optional [httpx2 .Client ] = None
44+ __async_client : Optional [httpx2 .AsyncClient ] = None
4845
4946 def __init__ (
5047 self ,
5148 api_token : Optional [str ] = None ,
5249 * ,
5350 base_url : Optional [str ] = None ,
54- timeout : Optional [httpx .Timeout ] = None ,
51+ timeout : Optional [httpx2 .Timeout ] = None ,
5552 ** kwargs ,
5653 ) -> None :
5754 super ().__init__ ()
@@ -64,10 +61,10 @@ def __init__(
6461 self .poll_interval = float (os .environ .get ("REPLICATE_POLL_INTERVAL" , "0.5" ))
6562
6663 @property
67- def _client (self ) -> httpx .Client :
64+ def _client (self ) -> httpx2 .Client :
6865 if not self .__client :
6966 self .__client = _build_httpx_client (
70- httpx .Client ,
67+ httpx2 .Client ,
7168 self ._api_token ,
7269 self ._base_url ,
7370 self ._timeout ,
@@ -76,24 +73,24 @@ def _client(self) -> httpx.Client:
7673 return self .__client # type: ignore[return-value]
7774
7875 @property
79- def _async_client (self ) -> httpx .AsyncClient :
76+ def _async_client (self ) -> httpx2 .AsyncClient :
8077 if not self .__async_client :
8178 self .__async_client = _build_httpx_client (
82- httpx .AsyncClient ,
79+ httpx2 .AsyncClient ,
8380 self ._api_token ,
8481 self ._base_url ,
8582 self ._timeout ,
8683 ** self ._client_kwargs ,
8784 ) # type: ignore[assignment]
8885 return self .__async_client # type: ignore[return-value]
8986
90- def _request (self , method : str , path : str , ** kwargs ) -> httpx .Response :
87+ def _request (self , method : str , path : str , ** kwargs ) -> httpx2 .Response :
9188 resp = self ._client .request (method , path , ** kwargs )
9289 _raise_for_status (resp )
9390
9491 return resp
9592
96- async def _async_request (self , method : str , path : str , ** kwargs ) -> httpx .Response :
93+ async def _async_request (self , method : str , path : str , ** kwargs ) -> httpx2 .Response :
9794 resp = await self ._async_client .request (method , path , ** kwargs )
9895 _raise_for_status (resp )
9996
@@ -222,8 +219,8 @@ async def async_stream(
222219 return async_stream (self , ref , input , use_file_output = use_file_output , ** params )
223220
224221
225- # Adapted from https://github.com/encode/httpx /issues/108#issuecomment-1132753155
226- class RetryTransport (httpx .AsyncBaseTransport , httpx .BaseTransport ):
222+ # Adapted from https://github.com/encode/httpx2 /issues/108#issuecomment-1132753155
223+ class RetryTransport (httpx2 .AsyncBaseTransport , httpx2 .BaseTransport ):
227224 """A custom HTTP transport that automatically retries requests using an exponential backoff strategy
228225 for specific HTTP status codes and request methods.
229226 """
@@ -240,7 +237,7 @@ class RetryTransport(httpx.AsyncBaseTransport, httpx.BaseTransport):
240237
241238 def __init__ ( # pylint: disable=too-many-arguments
242239 self ,
243- wrapped_transport : Union [httpx .BaseTransport , httpx .AsyncBaseTransport ],
240+ wrapped_transport : Union [httpx2 .BaseTransport , httpx2 .AsyncBaseTransport ],
244241 * ,
245242 max_attempts : int = 10 ,
246243 max_backoff_wait : float = MAX_BACKOFF_WAIT ,
@@ -272,7 +269,7 @@ def __init__( # pylint: disable=too-many-arguments
272269 self .max_backoff_wait = max_backoff_wait
273270
274271 def _calculate_sleep (
275- self , attempts_made : int , headers : Union [httpx .Headers , Mapping [str , str ]]
272+ self , attempts_made : int , headers : Union [httpx2 .Headers , Mapping [str , str ]]
276273 ) -> float :
277274 retry_after_header = (headers .get ("Retry-After" ) or "" ).strip ()
278275 if retry_after_header :
@@ -292,7 +289,7 @@ def _calculate_sleep(
292289 total_backoff = backoff + jitter
293290 return min (total_backoff , self .max_backoff_wait )
294291
295- def handle_request (self , request : httpx .Request ) -> httpx .Response :
292+ def handle_request (self , request : httpx2 .Request ) -> httpx2 .Response :
296293 response = self ._wrapped_transport .handle_request (request ) # type: ignore
297294
298295 if request .method not in self .retryable_methods :
@@ -318,7 +315,7 @@ def handle_request(self, request: httpx.Request) -> httpx.Response:
318315 attempts_made += 1
319316 remaining_attempts -= 1
320317
321- async def handle_async_request (self , request : httpx .Request ) -> httpx .Response :
318+ async def handle_async_request (self , request : httpx2 .Request ) -> httpx2 .Response :
322319 response = await self ._wrapped_transport .handle_async_request (request ) # type: ignore
323320
324321 if request .method not in self .retryable_methods :
@@ -366,12 +363,12 @@ def _get_api_token_from_environment() -> Optional[str]:
366363
367364
368365def _build_httpx_client (
369- client_type : Type [Union [httpx .Client , httpx .AsyncClient ]],
366+ client_type : Type [Union [httpx2 .Client , httpx2 .AsyncClient ]],
370367 api_token : Optional [str ] = None ,
371368 base_url : Optional [str ] = None ,
372- timeout : Optional [httpx .Timeout ] = None ,
369+ timeout : Optional [httpx2 .Timeout ] = None ,
373370 ** kwargs ,
374- ) -> Union [httpx .Client , httpx .AsyncClient ]:
371+ ) -> Union [httpx2 .Client , httpx2 .AsyncClient ]:
375372 headers = kwargs .pop ("headers" , {})
376373 if "User-Agent" not in headers :
377374 headers ["User-Agent" ] = f"replicate-python/{ __version__ } "
@@ -386,14 +383,14 @@ def _build_httpx_client(
386383 if base_url == "" :
387384 base_url = "https://api.replicate.com"
388385
389- timeout = timeout or httpx .Timeout (
386+ timeout = timeout or httpx2 .Timeout (
390387 5.0 , read = 30.0 , write = 30.0 , connect = 5.0 , pool = 10.0
391388 )
392389
393390 transport = kwargs .pop ("transport" , None ) or (
394- httpx .HTTPTransport ()
395- if client_type is httpx .Client
396- else httpx .AsyncHTTPTransport ()
391+ httpx2 .HTTPTransport ()
392+ if client_type is httpx2 .Client
393+ else httpx2 .AsyncHTTPTransport ()
397394 )
398395
399396 return client_type (
@@ -405,6 +402,6 @@ def _build_httpx_client(
405402 )
406403
407404
408- def _raise_for_status (resp : httpx .Response ) -> None :
405+ def _raise_for_status (resp : httpx2 .Response ) -> None :
409406 if 400 <= resp .status_code < 600 :
410407 raise ReplicateError .from_response (resp )
0 commit comments