Skip to content

Commit

Permalink
pass RESTClient a timeout in seconds (#41)
Browse files Browse the repository at this point in the history
This will throw a `requests.ConnectTimeout` if the timeout is passed before the server responds. I believe the default amount of retries is 3, but I haven't checked.
  • Loading branch information
tankorsmash authored Nov 6, 2020
1 parent caab917 commit fd5384c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions polygon/rest/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ class RESTClient:
""" This is a custom generated class """
DEFAULT_HOST = "api.polygon.io"

def __init__(self, auth_key: str):
def __init__(self, auth_key: str, timeout: int=None):
self.auth_key = auth_key
self.url = "https://" + self.DEFAULT_HOST

self._session = requests.Session()
self._session.params["apiKey"] = self.auth_key
self.timeout = timeout

def __enter__(self):
return self
Expand All @@ -27,7 +28,7 @@ def close(self):
self._session.close()

def _handle_response(self, response_type: str, endpoint: str, params: Dict[str, str]) -> Type[models.AnyDefinition]:
resp: requests.Response = self._session.get(endpoint, params=params)
resp: requests.Response = self._session.get(endpoint, params=params, timeout=self.timeout)
if resp.status_code == 200:
return unmarshal.unmarshal_json(response_type, resp.json())
else:
Expand Down

0 comments on commit fd5384c

Please sign in to comment.