From 8ee51b25f7ac6f21803cb77ff7bebf6f49e6b439 Mon Sep 17 00:00:00 2001 From: zack <43246297+clickingbuttons@users.noreply.github.com> Date: Mon, 23 May 2022 11:21:23 -0400 Subject: [PATCH 1/2] make params optional --- polygon/rest/quotes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polygon/rest/quotes.py b/polygon/rest/quotes.py index c39b6665..0169321f 100644 --- a/polygon/rest/quotes.py +++ b/polygon/rest/quotes.py @@ -105,7 +105,7 @@ def get_real_time_currency_conversion( to: str, amount: float, precision: Union[int, Precision] = 2, - params: Dict[str, Any] = None, + params: Optional[Dict[str, Any]] = None, raw: bool = False, ) -> Union[RealTimeCurrencyConversion, HTTPResponse]: """ From 2d655772fc304df255d179acd369d7fc56600482 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 2/2] 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(