From fd5384c5e74c27c7516173c55c42ac8bba5afd20 Mon Sep 17 00:00:00 2001 From: TankorSmash Date: Fri, 6 Nov 2020 10:38:34 -0500 Subject: [PATCH] pass RESTClient a timeout in seconds (#41) 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. --- polygon/rest/client.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/polygon/rest/client.py b/polygon/rest/client.py index ecc28f71..71de2bf7 100644 --- a/polygon/rest/client.py +++ b/polygon/rest/client.py @@ -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 @@ -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: