Skip to content

Commit c98e4d1

Browse files
authoredMay 21, 2024
Merge pull request #6710 from nateprewitt/api_rename
Move _get_connection to get_connection_with_tls_context
2 parents 970e8ce + 92075b3 commit c98e4d1

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed
 

‎src/requests/adapters.py

+31-6
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import os.path
1010
import socket # noqa: F401
1111
import typing
12+
import warnings
1213

1314
from urllib3.exceptions import ClosedPoolError, ConnectTimeoutError
1415
from urllib3.exceptions import HTTPError as _HTTPError
@@ -374,10 +375,20 @@ def build_response(self, req, resp):
374375

375376
return response
376377

377-
def _get_connection(self, request, verify, proxies=None, cert=None):
378-
# Replace the existing get_connection without breaking things and
379-
# ensure that TLS settings are considered when we interact with
380-
# urllib3 HTTP Pools
378+
def get_connection_with_tls_context(self, request, verify, proxies=None, cert=None):
379+
"""Returns a urllib3 connection for the given request and TLS settings.
380+
This should not be called from user code, and is only exposed for use
381+
when subclassing the :class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
382+
383+
:param request: The :class:`PreparedRequest <PreparedRequest>` object
384+
to be sent over the connection.
385+
:param verify: Either a boolean, in which case it controls whether
386+
we verify the server's TLS certificate, or a string, in which case it
387+
must be a path to a CA bundle to use.
388+
:param proxies: (optional) The proxies dictionary to apply to the request.
389+
:param cert: (optional) Any user-provided SSL certificate to be trusted.
390+
:rtype: urllib3.ConnectionPool
391+
"""
381392
proxy = select_proxy(request.url, proxies)
382393
try:
383394
host_params, pool_kwargs = _urllib3_request_context(request, verify, cert)
@@ -404,14 +415,26 @@ def _get_connection(self, request, verify, proxies=None, cert=None):
404415
return conn
405416

406417
def get_connection(self, url, proxies=None):
407-
"""Returns a urllib3 connection for the given URL. This should not be
418+
"""DEPRECATED: Users should move to `get_connection_with_tls_context`
419+
for all subclasses of HTTPAdapter using Requests>=2.32.2.
420+
421+
Returns a urllib3 connection for the given URL. This should not be
408422
called from user code, and is only exposed for use when subclassing the
409423
:class:`HTTPAdapter <requests.adapters.HTTPAdapter>`.
410424
411425
:param url: The URL to connect to.
412426
:param proxies: (optional) A Requests-style dictionary of proxies used on this request.
413427
:rtype: urllib3.ConnectionPool
414428
"""
429+
warnings.warn(
430+
(
431+
"`get_connection` has been deprecated in favor of "
432+
"`get_connection_with_tls_context`. Custom HTTPAdapter subclasses "
433+
"will need to migrate for Requests>=2.32.2. Please see "
434+
"https://github.com/psf/requests/pull/6710 for more details."
435+
),
436+
DeprecationWarning,
437+
)
415438
proxy = select_proxy(url, proxies)
416439

417440
if proxy:
@@ -529,7 +552,9 @@ def send(
529552
"""
530553

531554
try:
532-
conn = self._get_connection(request, verify, proxies=proxies, cert=cert)
555+
conn = self.get_connection_with_tls_context(
556+
request, verify, proxies=proxies, cert=cert
557+
)
533558
except LocationValueError as e:
534559
raise InvalidURL(e, request=request)
535560

0 commit comments

Comments
 (0)