From 611fcbaebffda66582d0c807569541fb9e23418d Mon Sep 17 00:00:00 2001 From: Sergio Gonzalez Date: Tue, 19 Mar 2024 21:53:57 -0700 Subject: [PATCH 1/2] Use ssl_context for HTTPS requests to avoid reading certificate files on every request --- polygon/rest/base.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/polygon/rest/base.py b/polygon/rest/base.py index dcddb8a8..8b52add3 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -1,5 +1,6 @@ import certifi import json +import ssl import urllib3 import inspect from urllib3.util.retry import Retry @@ -66,13 +67,15 @@ def __init__( backoff_factor=0.1, # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...] ) + ssl_context = ssl.create_default_context() + ssl_context.load_verify_locations(certifi.where()) + # https://urllib3.readthedocs.io/en/stable/reference/urllib3.poolmanager.html # https://urllib3.readthedocs.io/en/stable/reference/urllib3.connectionpool.html#urllib3.HTTPConnectionPool self.client = urllib3.PoolManager( num_pools=num_pools, headers=self.headers, # default headers sent with each request. - ca_certs=certifi.where(), - cert_reqs="CERT_REQUIRED", + ssl_context=ssl_context, retries=retry_strategy, # use the customized Retry instance ) From 4df2a607e96bcb61f8e6a358c824873ada2b1cab Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Thu, 28 Mar 2024 11:21:41 -0700 Subject: [PATCH 2/2] Update base.py Added comment to explain logic --- polygon/rest/base.py | 1 + 1 file changed, 1 insertion(+) diff --git a/polygon/rest/base.py b/polygon/rest/base.py index 8b52add3..3e65c06c 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -67,6 +67,7 @@ def __init__( backoff_factor=0.1, # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...] ) + # global cache ssl context and use (vs on each init of pool manager) ssl_context = ssl.create_default_context() ssl_context.load_verify_locations(certifi.where())