From 1b92672d3eeb8094ddbb557a8ceb860b9d5d11e1 Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:08:59 -0700 Subject: [PATCH 1/2] Update http status codes to retry request on --- polygon/rest/base.py | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/polygon/rest/base.py b/polygon/rest/base.py index a166e5f2..abdf2e0d 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -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 @@ -47,6 +48,16 @@ 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( @@ -54,10 +65,11 @@ def __init__( 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 From 870b91841238f9c96281eaf3fb44329ae3c8bf1c Mon Sep 17 00:00:00 2001 From: justinpolygon <123573436+justinpolygon@users.noreply.github.com> Date: Wed, 27 Sep 2023 10:30:16 -0700 Subject: [PATCH 2/2] Fix lint check --- polygon/rest/base.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/polygon/rest/base.py b/polygon/rest/base.py index abdf2e0d..34fd8121 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -54,8 +54,8 @@ def __init__( # 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, ...] + 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 @@ -65,7 +65,7 @@ def __init__( 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 + retries=retry_strategy, # use the customized Retry instance ) self.timeout = urllib3.Timeout(connect=connect_timeout, read=read_timeout)