Skip to content
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
14 changes: 13 additions & 1 deletion polygon/rest/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import json
import urllib3
import inspect
from urllib3.util.retry import Retry
from enum import Enum
from typing import Optional, Any, Dict
from datetime import datetime
Expand Down Expand Up @@ -47,17 +48,28 @@ def __init__(
"User-Agent": f"Polygon.io PythonClient/{version}",
}

# initialize self.retries with the parameter value before using it
self.retries = retries

# https://urllib3.readthedocs.io/en/stable/reference/urllib3.util.html#urllib3.util.Retry.RETRY_AFTER_STATUS_CODES
retry_strategy = Retry(
total=self.retries,
status_forcelist=[413, 429, 500, 502, 503, 504], # default 413, 429, 503
backoff_factor=0.1, # [0.0s, 0.2s, 0.4s, 0.8s, 1.6s, ...]
)

# 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",
retries=retry_strategy, # use the customized Retry instance
)

self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)
self.retries = retries

if verbose:
logger.setLevel(logging.DEBUG)
self.trace = trace
Expand Down