@@ -285,6 +285,30 @@ def __init__(self, email=None, api_key=None, config_file=None,
285
285
self .client_cert = client_cert
286
286
self .client_cert_key = client_cert_key
287
287
288
+ self .session = None # type: Union[None, requests.Session]
289
+
290
+ def _init_session (self ):
291
+ # type: () -> None
292
+
293
+ # Check if the session has been created already, and return
294
+ # immediately if so.
295
+ if self .session :
296
+ return
297
+
298
+ # Build a client cert object for requests
299
+ if self .client_cert_key is not None :
300
+ client_cert = (self .client_cert , self .client_cert_key ) # type: Union[str, Tuple[str, str]]
301
+ else :
302
+ client_cert = self .client_cert
303
+
304
+ # Actually construct the session
305
+ session = requests .Session ()
306
+ session .auth = requests .auth .HTTPBasicAuth (self .email , self .api_key )
307
+ session .verify = self .tls_verification
308
+ session .cert = client_cert
309
+ session .headers = {"User-agent" : self .get_user_agent ()}
310
+ self .session = session
311
+
288
312
def get_user_agent (self ):
289
313
# type: () -> str
290
314
vendor = ''
@@ -327,6 +351,8 @@ def do_api_query(self, orig_request, url, method="POST", longpolling=False, file
327
351
for f in files :
328
352
req_files .append ((f .name , f ))
329
353
354
+ self ._init_session ()
355
+
330
356
query_state = {
331
357
'had_error_retry' : False ,
332
358
'request' : request ,
@@ -370,21 +396,10 @@ def end_error_retry(succeeded):
370
396
if files :
371
397
kwargs ['files' ] = req_files
372
398
373
- # Build a client cert object for requests
374
- if self .client_cert_key is not None :
375
- client_cert = (self .client_cert , self .client_cert_key ) # type: Union[str, Tuple[str, str]]
376
- else :
377
- client_cert = self .client_cert
378
-
379
- res = requests .request (
399
+ res = self .session .request (
380
400
method ,
381
401
urllib .parse .urljoin (self .base_url , url ),
382
- auth = requests .auth .HTTPBasicAuth (self .email ,
383
- self .api_key ),
384
- verify = self .tls_verification ,
385
- cert = client_cert ,
386
402
timeout = 90 ,
387
- headers = {"User-agent" : self .get_user_agent ()},
388
403
** kwargs )
389
404
390
405
# On 50x errors, try again after a short sleep
0 commit comments