2727from __future__ import annotations
2828
2929import contextlib
30- import ssl
3130import typing
3231from types import TracebackType
3332
34- import httpcore
33+ if typing .TYPE_CHECKING :
34+ import ssl # pragma: no cover
35+
36+ import httpx # pragma: no cover
3537
3638from .._config import DEFAULT_LIMITS , Limits , Proxy , SSLContext , create_ssl_context
3739from .._exceptions import (
6668
6769__all__ = ["AsyncHTTPTransport" , "HTTPTransport" ]
6870
71+ HTTPCORE_EXC_MAP : dict [type [Exception ], type [httpx .HTTPError ]] = {}
72+
73+
74+ def _load_httpcore_exceptions () -> dict [type [Exception ], type [httpx .HTTPError ]]:
75+ import httpcore
76+
77+ return {
78+ httpcore .TimeoutException : TimeoutException ,
79+ httpcore .ConnectTimeout : ConnectTimeout ,
80+ httpcore .ReadTimeout : ReadTimeout ,
81+ httpcore .WriteTimeout : WriteTimeout ,
82+ httpcore .PoolTimeout : PoolTimeout ,
83+ httpcore .NetworkError : NetworkError ,
84+ httpcore .ConnectError : ConnectError ,
85+ httpcore .ReadError : ReadError ,
86+ httpcore .WriteError : WriteError ,
87+ httpcore .ProxyError : ProxyError ,
88+ httpcore .UnsupportedProtocol : UnsupportedProtocol ,
89+ httpcore .ProtocolError : ProtocolError ,
90+ httpcore .LocalProtocolError : LocalProtocolError ,
91+ httpcore .RemoteProtocolError : RemoteProtocolError ,
92+ }
93+
6994
7095@contextlib .contextmanager
7196def map_httpcore_exceptions () -> typing .Iterator [None ]:
97+ global HTTPCORE_EXC_MAP
98+ if len (HTTPCORE_EXC_MAP ) == 0 :
99+ HTTPCORE_EXC_MAP = _load_httpcore_exceptions ()
72100 try :
73101 yield
74102 except Exception as exc :
@@ -90,24 +118,6 @@ def map_httpcore_exceptions() -> typing.Iterator[None]:
90118 raise mapped_exc (message ) from exc
91119
92120
93- HTTPCORE_EXC_MAP = {
94- httpcore .TimeoutException : TimeoutException ,
95- httpcore .ConnectTimeout : ConnectTimeout ,
96- httpcore .ReadTimeout : ReadTimeout ,
97- httpcore .WriteTimeout : WriteTimeout ,
98- httpcore .PoolTimeout : PoolTimeout ,
99- httpcore .NetworkError : NetworkError ,
100- httpcore .ConnectError : ConnectError ,
101- httpcore .ReadError : ReadError ,
102- httpcore .WriteError : WriteError ,
103- httpcore .ProxyError : ProxyError ,
104- httpcore .UnsupportedProtocol : UnsupportedProtocol ,
105- httpcore .ProtocolError : ProtocolError ,
106- httpcore .LocalProtocolError : LocalProtocolError ,
107- httpcore .RemoteProtocolError : RemoteProtocolError ,
108- }
109-
110-
111121class ResponseStream (SyncByteStream ):
112122 def __init__ (self , httpcore_stream : typing .Iterable [bytes ]) -> None :
113123 self ._httpcore_stream = httpcore_stream
@@ -138,6 +148,8 @@ def __init__(
138148 verify : typing .Any = None ,
139149 cert : typing .Any = None ,
140150 ) -> None :
151+ import httpcore
152+
141153 proxy = Proxy (url = proxy ) if isinstance (proxy , (str , URL )) else proxy
142154 if verify is not None or cert is not None : # pragma: nocover
143155 # Deprecated...
@@ -225,6 +237,7 @@ def handle_request(
225237 request : Request ,
226238 ) -> Response :
227239 assert isinstance (request .stream , SyncByteStream )
240+ import httpcore
228241
229242 req = httpcore .Request (
230243 method = request .method ,
@@ -284,6 +297,8 @@ def __init__(
284297 verify : typing .Any = None ,
285298 cert : typing .Any = None ,
286299 ) -> None :
300+ import httpcore
301+
287302 proxy = Proxy (url = proxy ) if isinstance (proxy , (str , URL )) else proxy
288303 if verify is not None or cert is not None : # pragma: nocover
289304 # Deprecated...
@@ -371,6 +386,7 @@ async def handle_async_request(
371386 request : Request ,
372387 ) -> Response :
373388 assert isinstance (request .stream , AsyncByteStream )
389+ import httpcore
374390
375391 req = httpcore .Request (
376392 method = request .method ,
0 commit comments