From 1e3040fb55a895b940aa528d98acae558b7d311e Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:12:34 -0400 Subject: [PATCH 01/11] raise error --- polygon/websocket/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 6ef169d8..4a3ed2ac 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -5,6 +5,7 @@ import inspect import ssl import certifi +from multiprocessing import AuthenticationError from .models import * from websockets.client import connect, WebSocketClientProtocol from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError @@ -12,7 +13,6 @@ env_key = "POLYGON_API_KEY" - class WebSocketClient: def __init__( self, @@ -97,9 +97,11 @@ async def connect( auth_msg_parsed = json.loads(auth_msg) if self.verbose: print("authed:", auth_msg) - if auth_msg_parsed[0]["status"] == "auth_failed": - print(auth_msg_parsed[0]["message"]) - return + try: + if auth_msg_parsed[0]["status"] == "auth_failed": + raise AuthenticationError(auth_msg_parsed[0]["message"]) + except (AuthenticationError): + exit("Authentication Failed. Exiting...") while True: if self.schedule_resub: if self.verbose: From 8157876d26c42c998710b8ff62a0b217016fdcd1 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:18:33 -0400 Subject: [PATCH 02/11] lint --- polygon/websocket/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 4a3ed2ac..32468217 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -13,6 +13,7 @@ env_key = "POLYGON_API_KEY" + class WebSocketClient: def __init__( self, From 31b4cb49e5cd407ea07989a4b0c673aacf243165 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:32:07 -0400 Subject: [PATCH 03/11] make custom exception --- polygon/websocket/__init__.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 32468217..be1965e7 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -5,7 +5,6 @@ import inspect import ssl import certifi -from multiprocessing import AuthenticationError from .models import * from websockets.client import connect, WebSocketClientProtocol from websockets.exceptions import ConnectionClosedOK, ConnectionClosedError @@ -13,6 +12,8 @@ env_key = "POLYGON_API_KEY" +class AuthError(Exception): + pass class WebSocketClient: def __init__( @@ -98,11 +99,8 @@ async def connect( auth_msg_parsed = json.loads(auth_msg) if self.verbose: print("authed:", auth_msg) - try: - if auth_msg_parsed[0]["status"] == "auth_failed": - raise AuthenticationError(auth_msg_parsed[0]["message"]) - except (AuthenticationError): - exit("Authentication Failed. Exiting...") + if auth_msg_parsed[0]["status"] == "auth_failed": + raise AuthError(auth_msg_parsed[0]["message"]) while True: if self.schedule_resub: if self.verbose: From 5595cb375f0f9d6c8b7b36a89e5b4905100375f4 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:35:33 -0400 Subject: [PATCH 04/11] add note about invalid api keys --- docs/source/Getting-Started.rst | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/docs/source/Getting-Started.rst b/docs/source/Getting-Started.rst index 1bd896da..1485e4b1 100644 --- a/docs/source/Getting-Started.rst +++ b/docs/source/Getting-Started.rst @@ -186,6 +186,10 @@ Websocket client usage import asyncio client = WebSocketClient(market=Market.Stocks, feed=Feed.RealTime) # Uses POLYGON_API_KEY env var. Can optionally supply your key. + +.. note:: + Raises :code:`AuthError` if invalid API key is provided. + client.subscribe('T.AAPL') async def handle_msg(msg: List[WebSocketMessage]): From 69037ed8a873c32ec232382556e260691ea18202 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:36:42 -0400 Subject: [PATCH 05/11] lint --- polygon/websocket/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index be1965e7..1228c0a5 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -12,9 +12,11 @@ env_key = "POLYGON_API_KEY" + class AuthError(Exception): pass + class WebSocketClient: def __init__( self, From 61adb2f97931214992c912b299be309adcf5f779 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:39:31 -0400 Subject: [PATCH 06/11] move note out of code block --- docs/source/Getting-Started.rst | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/docs/source/Getting-Started.rst b/docs/source/Getting-Started.rst index 1485e4b1..f8756e04 100644 --- a/docs/source/Getting-Started.rst +++ b/docs/source/Getting-Started.rst @@ -186,10 +186,6 @@ Websocket client usage import asyncio client = WebSocketClient(market=Market.Stocks, feed=Feed.RealTime) # Uses POLYGON_API_KEY env var. Can optionally supply your key. - -.. note:: - Raises :code:`AuthError` if invalid API key is provided. - client.subscribe('T.AAPL') async def handle_msg(msg: List[WebSocketMessage]): @@ -197,3 +193,5 @@ Websocket client usage asyncio.run(client.connect(handle_msg)) +.. note:: + Raises :code:`AuthError` if invalid API key is provided. \ No newline at end of file From 67111e642fd89bcb53111d7a47c6a38b7a5f1c73 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 11:53:23 -0400 Subject: [PATCH 07/11] add WebsocketEnums --- docs/source/WebsocketEnums.rst | 25 +++++++++++++++++++++++++ docs/source/index.rst | 2 +- 2 files changed, 26 insertions(+), 1 deletion(-) create mode 100644 docs/source/WebsocketEnums.rst diff --git a/docs/source/WebsocketEnums.rst b/docs/source/WebsocketEnums.rst new file mode 100644 index 00000000..277b49ed --- /dev/null +++ b/docs/source/WebsocketEnums.rst @@ -0,0 +1,25 @@ +.. _websocketEnums_header: + +WebSocketEnums +============================== + +============================================================== +Feed +============================================================== +.. autoclass:: polygon.websocket.models.Feed + :members: + :undoc-members: + +============================================================== +Market +============================================================== +.. autoclass:: polygon.websocket.models.Market + :members: + :undoc-members: + +============================================================== +EventType +============================================================== +.. autoclass:: polygon.websocket.models.EventType + :members: + :undoc-members: \ No newline at end of file diff --git a/docs/source/index.rst b/docs/source/index.rst index dab96872..24330e76 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -17,7 +17,7 @@ This documentation is for the Python client only. For details about the response vX Models Enums - + WebsocketEnums Indices and tables ================== From 8f05297409e058a352fba6a25f2d4e1370cf5ed5 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 12:03:08 -0400 Subject: [PATCH 08/11] expose AuthError --- polygon/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/polygon/__init__.py b/polygon/__init__.py index 8a479e33..e4e0ced3 100644 --- a/polygon/__init__.py +++ b/polygon/__init__.py @@ -1,2 +1,2 @@ from .rest import RESTClient -from .websocket import WebSocketClient +from .websocket import WebSocketClient, AuthError From ecab15bbb2172de0d1417645b101a3568f2abbe0 Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 12:04:17 -0400 Subject: [PATCH 09/11] rename websockets headers --- docs/source/WebSocket-Enums.rst | 25 +++++++++++++++++++++++++ docs/source/WebSocket.rst | 30 ++++++++++++++++++++++++++++++ docs/source/index.rst | 4 ++-- 3 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 docs/source/WebSocket-Enums.rst create mode 100644 docs/source/WebSocket.rst diff --git a/docs/source/WebSocket-Enums.rst b/docs/source/WebSocket-Enums.rst new file mode 100644 index 00000000..dc2fd199 --- /dev/null +++ b/docs/source/WebSocket-Enums.rst @@ -0,0 +1,25 @@ +.. _websocket_enums_header: + +WebSocket Enums +============================== + +============================================================== +Feed +============================================================== +.. autoclass:: polygon.websocket.models.Feed + :members: + :undoc-members: + +============================================================== +Market +============================================================== +.. autoclass:: polygon.websocket.models.Market + :members: + :undoc-members: + +============================================================== +EventType +============================================================== +.. autoclass:: polygon.websocket.models.EventType + :members: + :undoc-members: \ No newline at end of file diff --git a/docs/source/WebSocket.rst b/docs/source/WebSocket.rst new file mode 100644 index 00000000..f66d9fae --- /dev/null +++ b/docs/source/WebSocket.rst @@ -0,0 +1,30 @@ +.. _websocket_header: + +WebSocket +========== + +=========== +Init client +=========== +.. automethod:: polygon.WebSocketClient.__init__ + +============================ +Connect +============================ +.. automethod:: polygon.WebSocketClient.connect + +============================ +Subscribe +============================ +.. automethod:: polygon.WebSocketClient.subscribe + +============================ +Unsubscribe +============================ +.. automethod:: polygon.WebSocketClient.unsubscribe + +============================ +Close +============================ +.. automethod:: polygon.WebSocketClient.close + diff --git a/docs/source/index.rst b/docs/source/index.rst index 24330e76..efe82e2f 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -9,7 +9,7 @@ This documentation is for the Python client only. For details about the response Getting-Started Aggs - WebSockets + WebSocket Snapshot Quotes Reference @@ -17,7 +17,7 @@ This documentation is for the Python client only. For details about the response vX Models Enums - WebsocketEnums + WebSocket-Enums Indices and tables ================== From 0a56db5d3adc0470e0b071df9d4688a2107a18cc Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 12:05:11 -0400 Subject: [PATCH 10/11] deleted old files --- docs/source/WebSockets.rst | 30 ------------------------------ docs/source/WebsocketEnums.rst | 25 ------------------------- 2 files changed, 55 deletions(-) delete mode 100644 docs/source/WebSockets.rst delete mode 100644 docs/source/WebsocketEnums.rst diff --git a/docs/source/WebSockets.rst b/docs/source/WebSockets.rst deleted file mode 100644 index 59281a4c..00000000 --- a/docs/source/WebSockets.rst +++ /dev/null @@ -1,30 +0,0 @@ -.. _websockets_header: - -WebSockets -========== - -=========== -Init client -=========== -.. automethod:: polygon.WebSocketClient.__init__ - -============================ -Connect -============================ -.. automethod:: polygon.WebSocketClient.connect - -============================ -Subscribe -============================ -.. automethod:: polygon.WebSocketClient.subscribe - -============================ -Unsubscribe -============================ -.. automethod:: polygon.WebSocketClient.unsubscribe - -============================ -Close -============================ -.. automethod:: polygon.WebSocketClient.close - diff --git a/docs/source/WebsocketEnums.rst b/docs/source/WebsocketEnums.rst deleted file mode 100644 index 277b49ed..00000000 --- a/docs/source/WebsocketEnums.rst +++ /dev/null @@ -1,25 +0,0 @@ -.. _websocketEnums_header: - -WebSocketEnums -============================== - -============================================================== -Feed -============================================================== -.. autoclass:: polygon.websocket.models.Feed - :members: - :undoc-members: - -============================================================== -Market -============================================================== -.. autoclass:: polygon.websocket.models.Market - :members: - :undoc-members: - -============================================================== -EventType -============================================================== -.. autoclass:: polygon.websocket.models.EventType - :members: - :undoc-members: \ No newline at end of file From a2783483e1f3d8c532df520e160f58a687f98eda Mon Sep 17 00:00:00 2001 From: Darcy Linde <{47221647+Darcy-Linde@users.noreply.github.com}> Date: Fri, 6 May 2022 12:05:33 -0400 Subject: [PATCH 11/11] add docs comment --- polygon/websocket/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/polygon/websocket/__init__.py b/polygon/websocket/__init__.py index 1228c0a5..2538d91b 100644 --- a/polygon/websocket/__init__.py +++ b/polygon/websocket/__init__.py @@ -75,6 +75,7 @@ async def connect( :param processor: The callback to process messages. :param close_timeout: How long to wait for handshake when calling .close. + :raises AuthError: If invalid API key is supplied. """ reconnects = 0 isasync = inspect.iscoroutinefunction(processor)