From 2fec1d3428963bc80720cf3a2e52de931697ce22 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:16:37 -0400 Subject: [PATCH 1/7] add more exceptions --- polygon/__init__.py | 5 +++-- polygon/exceptions.py | 18 ++++++++++++++++++ polygon/rest/base.py | 12 ++++-------- polygon/websocket/__init__.py | 9 +++------ 4 files changed, 28 insertions(+), 16 deletions(-) create mode 100644 polygon/exceptions.py diff --git a/polygon/__init__.py b/polygon/__init__.py index bd1c56d4..da6c5136 100644 --- a/polygon/__init__.py +++ b/polygon/__init__.py @@ -1,5 +1,6 @@ from .rest import RESTClient -from .rest.base import NoResultsError, version -from .websocket import WebSocketClient, AuthError +from .rest.base import version +from .websocket import WebSocketClient +from .exceptions import * __version__ = version diff --git a/polygon/exceptions.py b/polygon/exceptions.py new file mode 100644 index 00000000..a2d1bd59 --- /dev/null +++ b/polygon/exceptions.py @@ -0,0 +1,18 @@ + +class PolyBadResponse(Exception): + """ + Non-200 response from API + """ + pass + +class PolyAuthError(Exception): + """" + Empty or invalid API key + """ + pass + +class PolyMissingResults(Exception): + """ + Missing results key + """ + pass diff --git a/polygon/rest/base.py b/polygon/rest/base.py index fc300f19..645422d0 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -8,6 +8,7 @@ import pkg_resources # part of setuptools from ..logging import get_logger import logging +from ..exceptions import PolyAuthError, PolyBadResponse, PolyMissingResults logger = get_logger("RESTClient") version = "unknown" @@ -16,11 +17,6 @@ except: pass - -class NoResultsError(Exception): - pass - - class BaseClient: def __init__( self, @@ -33,7 +29,7 @@ def __init__( verbose: bool, ): if api_key is None: - raise Exception( + raise PolyAuthError( f"Must specify env var POLYGON_API_KEY or pass api_key in constructor" ) self.API_KEY = api_key @@ -78,7 +74,7 @@ def _get( ) if resp.status != 200: - raise Exception(resp.data.decode("utf-8")) + raise PolyBadResponse(resp.data.decode("utf-8")) if raw: return resp @@ -87,7 +83,7 @@ def _get( if result_key: if result_key not in obj: - raise NoResultsError( + raise PolyMissingResults( f'Expected key "{result_key}" in response {obj}.' + "Make sure you have sufficient permissions and your request parameters are valid." + f"This is the url that returned no results: {resp.geturl()}" diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 017a9773..a851f0a6 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -11,15 +11,12 @@ from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError from ..logging import get_logger import logging +from ..exceptions import PolyAuthError env_key = "POLYGON_API_KEY" logger = get_logger("WebSocketClient") -class AuthError(Exception): - pass - - class WebSocketClient: def __init__( self, @@ -45,7 +42,7 @@ def __init__( :return: A client. """ if api_key is None: - raise Exception( + raise PolyAuthError( f"Must specify env var {env_key} or pass api_key in constructor" ) self.api_key = api_key @@ -107,7 +104,7 @@ async def connect( auth_msg_parsed = json.loads(auth_msg) logger.debug("authed: %s", auth_msg) if auth_msg_parsed[0]["status"] == "auth_failed": - raise AuthError(auth_msg_parsed[0]["message"]) + raise PolyAuthError(auth_msg_parsed[0]["message"]) while True: if self.schedule_resub: logger.debug( From 3cb61ae13a5d7f5eaa5457dd5c284222c9433e64 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:19:04 -0400 Subject: [PATCH 2/7] no breaking changes --- polygon/exceptions.py | 6 +++--- polygon/rest/base.py | 8 ++++---- polygon/websocket/__init__.py | 6 +++--- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/polygon/exceptions.py b/polygon/exceptions.py index a2d1bd59..26bdd438 100644 --- a/polygon/exceptions.py +++ b/polygon/exceptions.py @@ -1,17 +1,17 @@ -class PolyBadResponse(Exception): +class BadResponse(Exception): """ Non-200 response from API """ pass -class PolyAuthError(Exception): +class AuthError(Exception): """" Empty or invalid API key """ pass -class PolyMissingResults(Exception): +class MissingResults(Exception): """ Missing results key """ diff --git a/polygon/rest/base.py b/polygon/rest/base.py index 645422d0..124ee81f 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -8,7 +8,7 @@ import pkg_resources # part of setuptools from ..logging import get_logger import logging -from ..exceptions import PolyAuthError, PolyBadResponse, PolyMissingResults +from ..exceptions import AuthError, BadResponse, MissingResults logger = get_logger("RESTClient") version = "unknown" @@ -29,7 +29,7 @@ def __init__( verbose: bool, ): if api_key is None: - raise PolyAuthError( + raise AuthError( f"Must specify env var POLYGON_API_KEY or pass api_key in constructor" ) self.API_KEY = api_key @@ -74,7 +74,7 @@ def _get( ) if resp.status != 200: - raise PolyBadResponse(resp.data.decode("utf-8")) + raise BadResponse(resp.data.decode("utf-8")) if raw: return resp @@ -83,7 +83,7 @@ def _get( if result_key: if result_key not in obj: - raise PolyMissingResults( + raise MissingResults( f'Expected key "{result_key}" in response {obj}.' + "Make sure you have sufficient permissions and your request parameters are valid." + f"This is the url that returned no results: {resp.geturl()}" diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index a851f0a6..b2eab462 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -11,7 +11,7 @@ from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError from ..logging import get_logger import logging -from ..exceptions import PolyAuthError +from ..exceptions import AuthError env_key = "POLYGON_API_KEY" logger = get_logger("WebSocketClient") @@ -42,7 +42,7 @@ def __init__( :return: A client. """ if api_key is None: - raise PolyAuthError( + raise AuthError( f"Must specify env var {env_key} or pass api_key in constructor" ) self.api_key = api_key @@ -104,7 +104,7 @@ async def connect( auth_msg_parsed = json.loads(auth_msg) logger.debug("authed: %s", auth_msg) if auth_msg_parsed[0]["status"] == "auth_failed": - raise PolyAuthError(auth_msg_parsed[0]["message"]) + raise AuthError(auth_msg_parsed[0]["message"]) while True: if self.schedule_resub: logger.debug( From f09b08481e1ddabb7c51b8368a02f88a52763afd Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:21:03 -0400 Subject: [PATCH 3/7] update docs --- docs/source/Exceptions.rst | 13 ++++++++++--- polygon/exceptions.py | 2 +- polygon/rest/base.py | 4 ++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/docs/source/Exceptions.rst b/docs/source/Exceptions.rst index c33a122b..97f1dfdf 100644 --- a/docs/source/Exceptions.rst +++ b/docs/source/Exceptions.rst @@ -6,13 +6,20 @@ Exceptions ============================================================== AuthError ============================================================== -.. autoclass:: polygon.websocket.__init__.AuthError +.. autoclass:: polygon.exceptions.AuthError :members: :undoc-members: ============================================================== NoResultsError ============================================================== -.. autoclass:: polygon.rest.base.NoResultsError +.. autoclass:: polygon.exceptions.NoResultsError :members: - :undoc-members: \ No newline at end of file + :undoc-members: + +============================================================== +BadResponse +============================================================== +.. autoclass:: polygon.exceptions.BadResponse + :members: + :undoc-members: diff --git a/polygon/exceptions.py b/polygon/exceptions.py index 26bdd438..302ab476 100644 --- a/polygon/exceptions.py +++ b/polygon/exceptions.py @@ -11,7 +11,7 @@ class AuthError(Exception): """ pass -class MissingResults(Exception): +class NoResultsError(Exception): """ Missing results key """ diff --git a/polygon/rest/base.py b/polygon/rest/base.py index 124ee81f..cde000be 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -8,7 +8,7 @@ import pkg_resources # part of setuptools from ..logging import get_logger import logging -from ..exceptions import AuthError, BadResponse, MissingResults +from ..exceptions import AuthError, BadResponse, NoResultsError logger = get_logger("RESTClient") version = "unknown" @@ -83,7 +83,7 @@ def _get( if result_key: if result_key not in obj: - raise MissingResults( + raise NoResultsError( f'Expected key "{result_key}" in response {obj}.' + "Make sure you have sufficient permissions and your request parameters are valid." + f"This is the url that returned no results: {resp.geturl()}" From 86eb11589f0878408e44bb5a119a7498feb12265 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:22:03 -0400 Subject: [PATCH 4/7] alphabetize --- docs/source/Exceptions.rst | 9 +++++---- polygon/exceptions.py | 12 ++++++------ 2 files changed, 11 insertions(+), 10 deletions(-) diff --git a/docs/source/Exceptions.rst b/docs/source/Exceptions.rst index 97f1dfdf..a57f633b 100644 --- a/docs/source/Exceptions.rst +++ b/docs/source/Exceptions.rst @@ -11,15 +11,16 @@ AuthError :undoc-members: ============================================================== -NoResultsError +BadResponse ============================================================== -.. autoclass:: polygon.exceptions.NoResultsError +.. autoclass:: polygon.exceptions.BadResponse :members: :undoc-members: ============================================================== -BadResponse +NoResultsError ============================================================== -.. autoclass:: polygon.exceptions.BadResponse +.. autoclass:: polygon.exceptions.NoResultsError :members: :undoc-members: + diff --git a/polygon/exceptions.py b/polygon/exceptions.py index 302ab476..4da5fac5 100644 --- a/polygon/exceptions.py +++ b/polygon/exceptions.py @@ -1,16 +1,16 @@ -class BadResponse(Exception): - """ - Non-200 response from API - """ - pass - class AuthError(Exception): """" Empty or invalid API key """ pass +class BadResponse(Exception): + """ + Non-200 response from API + """ + pass + class NoResultsError(Exception): """ Missing results key From bd2978ef1a7000b612c463d269f5410c13f6cf56 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:34:16 -0400 Subject: [PATCH 5/7] fix comment --- polygon/exceptions.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polygon/exceptions.py b/polygon/exceptions.py index 4da5fac5..4653faa0 100644 --- a/polygon/exceptions.py +++ b/polygon/exceptions.py @@ -1,6 +1,6 @@ class AuthError(Exception): - """" + """ Empty or invalid API key """ pass From c2856a779811dd0686563691cb40f1a6407e408f Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Tue, 24 May 2022 15:34:37 -0400 Subject: [PATCH 6/7] lint --- polygon/exceptions.py | 6 +++++- polygon/rest/base.py | 1 + 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/polygon/exceptions.py b/polygon/exceptions.py index 4653faa0..7246108c 100644 --- a/polygon/exceptions.py +++ b/polygon/exceptions.py @@ -1,18 +1,22 @@ - class AuthError(Exception): """ Empty or invalid API key """ + pass + class BadResponse(Exception): """ Non-200 response from API """ + pass + class NoResultsError(Exception): """ Missing results key """ + pass diff --git a/polygon/rest/base.py b/polygon/rest/base.py index cde000be..fe194469 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -17,6 +17,7 @@ except: pass + class BaseClient: def __init__( self, From 881c4ee9cdfe809692994fadb6e3231719b02713 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Wed, 25 May 2022 14:04:30 -0400 Subject: [PATCH 7/7] update version string --- polygon/rest/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polygon/rest/base.py b/polygon/rest/base.py index fe194469..8afbd330 100644 --- a/polygon/rest/base.py +++ b/polygon/rest/base.py @@ -42,7 +42,7 @@ def __init__( num_pools=num_pools, headers={ "Authorization": "Bearer " + self.API_KEY, - "User-Agent": "Python client " + version, + "User-Agent": f"Polygon.io PythonClient/{version}", }, ca_certs=certifi.where(), cert_reqs="CERT_REQUIRED",