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 .Context :
81
+ return create_ssl_context (
82
+ verify = _DEFAULT_VERIFY ,
83
+ cert = _DEFAULT_CERT ,
84
+ trust_env = _DEFAULT_TRUST_ENV ,
85
+ )
86
+
87
+
88
+ @cache
89
+ def _create_or_reuse_ssl_context (
90
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
91
+ cert : CertTypes | None = _DEFAULT_CERT ,
92
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
93
+ ) -> ssl .Context :
94
+ if (verify , cert , trust_env ) == (
95
+ _DEFAULT_VERIFY ,
96
+ _DEFAULT_CERT ,
97
+ _DEFAULT_TRUST_ENV ,
98
+ ):
99
+ return _create_default_ssl_context ()
100
+ else :
101
+ return create_ssl_context (verify = verify , cert = cert , trust_env = trust_env )
102
+
73
103
74
104
def _load_httpcore_exceptions () -> dict [type [Exception ], type [httpx .HTTPError ]]:
75
105
import httpcore
@@ -135,9 +165,9 @@ def close(self) -> None:
135
165
class HTTPTransport (BaseTransport ):
136
166
def __init__ (
137
167
self ,
138
- verify : ssl .SSLContext | str | bool = True ,
139
- cert : CertTypes | None = None ,
140
- trust_env : bool = True ,
168
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
169
+ cert : CertTypes | None = _DEFAULT_CERT ,
170
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
141
171
http1 : bool = True ,
142
172
http2 : bool = False ,
143
173
limits : Limits = DEFAULT_LIMITS ,
@@ -150,7 +180,11 @@ def __init__(
150
180
import httpcore
151
181
152
182
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 )
183
+ ssl_context = _create_or_reuse_ssl_context (
184
+ verify = verify ,
185
+ cert = cert ,
186
+ trust_env = trust_env ,
187
+ )
154
188
155
189
if proxy is None :
156
190
self ._pool = httpcore .ConnectionPool (
@@ -279,9 +313,9 @@ async def aclose(self) -> None:
279
313
class AsyncHTTPTransport (AsyncBaseTransport ):
280
314
def __init__ (
281
315
self ,
282
- verify : ssl .SSLContext | str | bool = True ,
283
- cert : CertTypes | None = None ,
284
- trust_env : bool = True ,
316
+ verify : ssl .SSLContext | str | bool = _DEFAULT_VERIFY ,
317
+ cert : CertTypes | None = _DEFAULT_CERT ,
318
+ trust_env : bool = _DEFAULT_TRUST_ENV ,
285
319
http1 : bool = True ,
286
320
http2 : bool = False ,
287
321
limits : Limits = DEFAULT_LIMITS ,
@@ -294,7 +328,11 @@ def __init__(
294
328
import httpcore
295
329
296
330
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 )
331
+ ssl_context = _create_or_reuse_ssl_context (
332
+ verify = verify ,
333
+ cert = cert ,
334
+ trust_env = trust_env ,
335
+ )
298
336
299
337
if proxy is None :
300
338
self ._pool = httpcore .AsyncConnectionPool (
0 commit comments