Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ssl_context for HTTPS requests to avoid reading certificate files on every request #632

Merged
merged 2 commits into from
Mar 28, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions polygon/rest/base.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import certifi
import json
import ssl
import urllib3
import inspect
from urllib3.util.retry import Retry
Expand Down Expand Up @@ -66,13 +67,16 @@ 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())

# 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
)

Expand Down
Loading