28
28
29
29
import contextlib
30
30
import typing
31
+ from functools import cache
31
32
from types import TracebackType
32
33
33
34
if typing .TYPE_CHECKING :
70
71
71
72
HTTPCORE_EXC_MAP : dict [type [Exception ], type [httpx .HTTPError ]] = {}
72
73
74
+ _DEFAULT_VERIFY = True
75
+ _DEFAULT_CERT = None
76
+ _DEFAULT_TRUST_ENV = True
77
+
78
+
79
+ @cache
80
+ def _create_default_ssl_context () -> ssl .SSLContext :
81
+ return create_ssl_context (
82
+ verify = _DEFAULT_VERIFY ,
83
+ cert = _DEFAULT_CERT ,
84
+ trust_env = _DEFAULT_TRUST_ENV ,
85
+ )
86
+
87
+
88
+ def _create_or_reuse_ssl_context (
89
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
90
+ cert : CertTypes | None = _DEFAULT_CERT ,
91
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
92
+ ) -> ssl .SSLContext :
93
+ if (verify , cert , trust_env ) == (
94
+ _DEFAULT_VERIFY ,
95
+ _DEFAULT_CERT ,
96
+ _DEFAULT_TRUST_ENV ,
97
+ ):
98
+ return _create_default_ssl_context ()
99
+ else :
100
+ return create_ssl_context (verify = verify , cert = cert , trust_env = trust_env )
101
+
73
102
74
103
def _load_httpcore_exceptions () -> dict [type [Exception ], type [httpx .HTTPError ]]:
75
104
import httpcore
@@ -135,9 +164,9 @@ def close(self) -> None:
135
164
class HTTPTransport (BaseTransport ):
136
165
def __init__ (
137
166
self ,
138
- verify : ssl .SSLContext | str | bool = True ,
139
- cert : CertTypes | None = None ,
140
- trust_env : bool = True ,
167
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
168
+ cert : CertTypes | None = _DEFAULT_CERT ,
169
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
141
170
http1 : bool = True ,
142
171
http2 : bool = False ,
143
172
limits : Limits = DEFAULT_LIMITS ,
@@ -150,7 +179,11 @@ def __init__(
150
179
import httpcore
151
180
152
181
proxy = Proxy (url = proxy ) if isinstance (proxy , (str , URL )) else proxy
153
- ssl_context = create_ssl_context (verify = verify , cert = cert , trust_env = trust_env )
182
+ ssl_context = _create_or_reuse_ssl_context (
183
+ verify = verify ,
184
+ cert = cert ,
185
+ trust_env = trust_env ,
186
+ )
154
187
155
188
if proxy is None :
156
189
self ._pool = httpcore .ConnectionPool (
@@ -279,9 +312,9 @@ async def aclose(self) -> None:
279
312
class AsyncHTTPTransport (AsyncBaseTransport ):
280
313
def __init__ (
281
314
self ,
282
- verify : ssl .SSLContext | str | bool = True ,
283
- cert : CertTypes | None = None ,
284
- trust_env : bool = True ,
315
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
316
+ cert : CertTypes | None = _DEFAULT_CERT ,
317
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
285
318
http1 : bool = True ,
286
319
http2 : bool = False ,
287
320
limits : Limits = DEFAULT_LIMITS ,
@@ -294,7 +327,11 @@ def __init__(
294
327
import httpcore
295
328
296
329
proxy = Proxy (url = proxy ) if isinstance (proxy , (str , URL )) else proxy
297
- ssl_context = create_ssl_context (verify = verify , cert = cert , trust_env = trust_env )
330
+ ssl_context = _create_or_reuse_ssl_context (
331
+ verify = verify ,
332
+ cert = cert ,
333
+ trust_env = trust_env ,
334
+ )
298
335
299
336
if proxy is None :
300
337
self ._pool = httpcore .AsyncConnectionPool (
0 commit comments