@@ -342,6 +342,30 @@ def __init__(self, email=None, api_key=None, config_file=None,
342
342
self .client_cert = client_cert
343
343
self .client_cert_key = client_cert_key
344
344
345
+ self .session = None # type: Union[None, requests.Session]
346
+
347
+ def ensure_session (self ):
348
+ # type: () -> None
349
+
350
+ # Check if the session has been created already, and return
351
+ # immediately if so.
352
+ if self .session :
353
+ return
354
+
355
+ # Build a client cert object for requests
356
+ if self .client_cert_key is not None :
357
+ client_cert = (self .client_cert , self .client_cert_key ) # type: Union[str, Tuple[str, str]]
358
+ else :
359
+ client_cert = self .client_cert
360
+
361
+ # Actually construct the session
362
+ session = requests .Session ()
363
+ session .auth = requests .auth .HTTPBasicAuth (self .email , self .api_key ) # type: ignore # https://github.com/python/typeshed/pull/1504
364
+ session .verify = self .tls_verification # type: ignore # https://github.com/python/typeshed/pull/1504
365
+ session .cert = client_cert
366
+ session .headers = {"User-agent" : self .get_user_agent ()}
367
+ self .session = session
368
+
345
369
def get_user_agent (self ):
346
370
# type: () -> str
347
371
vendor = ''
@@ -384,6 +408,8 @@ def do_api_query(self, orig_request, url, method="POST", longpolling=False, file
384
408
for f in files :
385
409
req_files .append ((f .name , f ))
386
410
411
+ self .ensure_session ()
412
+
387
413
query_state = {
388
414
'had_error_retry' : False ,
389
415
'request' : request ,
@@ -427,21 +453,10 @@ def end_error_retry(succeeded):
427
453
if files :
428
454
kwargs ['files' ] = req_files
429
455
430
- # Build a client cert object for requests
431
- if self .client_cert_key is not None :
432
- client_cert = (self .client_cert , self .client_cert_key ) # type: Union[str, Tuple[str, str]]
433
- else :
434
- client_cert = self .client_cert
435
-
436
- res = requests .request (
456
+ res = self .session .request (
437
457
method ,
438
458
urllib .parse .urljoin (self .base_url , url ),
439
- auth = requests .auth .HTTPBasicAuth (self .email ,
440
- self .api_key ),
441
- verify = self .tls_verification ,
442
- cert = client_cert ,
443
459
timeout = 90 ,
444
- headers = {"User-agent" : self .get_user_agent ()},
445
460
** kwargs )
446
461
447
462
# On 50x errors, try again after a short sleep
0 commit comments