3535
3636 _REQUESTS_AVAILABLE = True
3737 _REQUESTS_META_VERSION = client_meta_version (requests .__version__ )
38+
39+ # Use our custom HTTPSConnectionPool for chain cert fingerprint support.
40+ try :
41+ from ._urllib3_chain_certs import HTTPSConnectionPool
42+ except (ImportError , AttributeError ):
43+ HTTPSConnectionPool = urllib3 .HTTPSConnectionPool
44+
45+ class _ElasticHTTPAdapter (HTTPAdapter ):
46+ def __init__ (self , node_config : NodeConfig , ** kwargs ):
47+ self ._node_config = node_config
48+ super ().__init__ (** kwargs )
49+
50+ def init_poolmanager (
51+ self , connections , maxsize , block = False , ** pool_kwargs
52+ ) -> None :
53+ if self ._node_config .ssl_context :
54+ pool_kwargs .setdefault ("ssl_context" , self ._node_config .ssl_context )
55+ if self ._node_config .ssl_assert_fingerprint :
56+ pool_kwargs .setdefault (
57+ "assert_fingerprint" , self ._node_config .ssl_assert_fingerprint
58+ )
59+
60+ super ().init_poolmanager (connections , maxsize , block = block , ** pool_kwargs )
61+ self .poolmanager .pool_classes_by_scheme ["https" ] = HTTPSConnectionPool
62+
63+
3864except ImportError : # pragma: nocover
3965 _REQUESTS_AVAILABLE = False
4066 _REQUESTS_META_VERSION = ""
4167
4268
4369class RequestsHttpNode (BaseNode ):
44- """
45- Connection using the `requests` library communicating via HTTP.
46-
47- :arg use_ssl: use ssl for the connection if `True`
48- :arg verify_certs: whether to verify SSL certificates
49- :arg ssl_show_warn: show warning when verify certs is disabled
50- :arg ca_certs: optional path to CA bundle. By default standard requests'
51- bundle will be used.
52- :arg client_cert: path to the file containing the private key and the
53- certificate, or cert only if using client_key
54- :arg client_key: path to the file containing the private key if using
55- separate cert and key files (client_cert will contain only the cert)
56- :arg headers: any custom http headers to be add to requests
57- :arg http_compress: Use gzip compression
58- :arg opaque_id: Send this value in the 'X-Opaque-Id' HTTP header
59- For tracing all requests made by this transport.
60- """
70+ """Synchronous node using the ``requests`` library communicating via HTTP"""
6171
6272 _ELASTIC_CLIENT_META = ("rq" , _REQUESTS_META_VERSION )
6373
@@ -109,8 +119,10 @@ def __init__(self, config: NodeConfig):
109119 pool_maxsize = config .connections_per_node ,
110120 pool_block = True ,
111121 )
112- for prefix in ("http://" , "https://" ):
113- self .session .mount (prefix = prefix , adapter = adapter )
122+ # Preload the HTTPConnectionPool so initialization issues
123+ # are raised here instead of in perform_request()
124+ adapter .get_connection (self .base_url )
125+ self .session .mount (prefix = f"{ self .scheme } ://" , adapter = adapter )
114126
115127 def perform_request (
116128 self ,
@@ -161,10 +173,10 @@ def perform_request(
161173 if isinstance (e , requests .Timeout ):
162174 raise ConnectionTimeout (
163175 "Connection timed out during request" , errors = (e ,)
164- )
176+ ) from None
165177 elif isinstance (e , (ssl .SSLError , requests .exceptions .SSLError )):
166- raise TlsError (str (e ), errors = (e ,))
167- raise ConnectionError (str (e ), errors = (e ,))
178+ raise TlsError (str (e ), errors = (e ,)) from None
179+ raise ConnectionError (str (e ), errors = (e ,)) from None
168180
169181 response = ApiResponseMeta (
170182 node = self .config ,
@@ -180,22 +192,3 @@ def close(self) -> None:
180192 Explicitly closes connections
181193 """
182194 self .session .close ()
183-
184-
185- class _ElasticHTTPAdapter (HTTPAdapter ):
186- def __init__ (self , node_config : NodeConfig , ** kwargs ):
187- self ._node_config = node_config
188- super ().__init__ (** kwargs )
189-
190- def init_poolmanager (
191- self , connections , maxsize , block = False , ** pool_kwargs
192- ) -> urllib3 .PoolManager :
193- if self ._node_config .ssl_context :
194- pool_kwargs .setdefault ("ssl_context" , self ._node_config .ssl_context )
195- if self ._node_config .ssl_assert_fingerprint :
196- pool_kwargs .setdefault (
197- "ssl_assert_fingerprint" , self ._node_config .ssl_assert_fingerprint
198- )
199- return super ().init_poolmanager (
200- connections , maxsize , block = block , ** pool_kwargs
201- )
0 commit comments