From c8d85868418a0cede632e77b3f5d1f4d76e6442e Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Wed, 23 Nov 2022 16:56:13 +0100 Subject: [PATCH] Add bfxapi/websocket/enums.py file. Rewrite bfxapi.websocket's __init__.py. Rename HEARTBEAT constant to _HEARTBEAT to make it internal. --- bfxapi/websocket/BfxWebsocketClient.py | 25 ++++--------------------- bfxapi/websocket/__init__.py | 4 +--- bfxapi/websocket/enums.py | 25 +++++++++++++++++++++++++ bfxapi/websocket/handlers.py | 19 ++----------------- 4 files changed, 32 insertions(+), 41 deletions(-) create mode 100644 bfxapi/websocket/enums.py diff --git a/bfxapi/websocket/BfxWebsocketClient.py b/bfxapi/websocket/BfxWebsocketClient.py index e2b8105..000efc3 100644 --- a/bfxapi/websocket/BfxWebsocketClient.py +++ b/bfxapi/websocket/BfxWebsocketClient.py @@ -10,7 +10,7 @@ from .exceptions import ConnectionNotOpen, TooManySubscriptions, WebsocketAuthen from ..utils.logger import Formatter, CustomLogger -HEARTBEAT = "hb" +_HEARTBEAT = "hb" def _require_websocket_connection(function): async def wrapper(self, *args, **kwargs): @@ -65,7 +65,7 @@ class BfxWebsocketClient(object): else: raise InvalidAuthenticationCredentials("Cannot authenticate with given API-KEY and API-SECRET.") elif isinstance(message, dict) and message["event"] == "error": self.event_emitter.emit("wss-error", message["code"], message["msg"]) - elif isinstance(message, list) and (chanId := message[0]) == 0 and message[1] != HEARTBEAT: + elif isinstance(message, list) and (chanId := message[0]) == 0 and message[1] != _HEARTBEAT: self.handler.handle(message[1], message[2]) except websockets.ConnectionClosedError: continue finally: await self.websocket.wait_closed(); break @@ -171,7 +171,7 @@ class _BfxWebsocketBucket(object): del self.chanIds[chanId] elif isinstance(message, dict) and message["event"] == "error": self.event_emitter.emit("wss-error", message["code"], message["msg"]) - elif isinstance(message, list) and (chanId := message[0]) and message[1] != HEARTBEAT: + elif isinstance(message, list) and (chanId := message[0]) and message[1] != _HEARTBEAT: self.handler.handle(self.chanIds[chanId], *message[1:]) except websockets.ConnectionClosedError: continue finally: await self.websocket.wait_closed(); break @@ -202,21 +202,4 @@ class _BfxWebsocketBucket(object): @_require_websocket_connection async def close(self, code=1000, reason=str()): - await self.websocket.close(code=code, reason=reason) - -class Errors(int, Enum): - ERR_UNK = 10000 - ERR_GENERIC = 10001 - ERR_CONCURRENCY = 10008 - ERR_PARAMS = 10020 - ERR_CONF_FAIL = 10050 - ERR_AUTH_FAIL = 10100 - ERR_AUTH_PAYLOAD = 10111 - ERR_AUTH_SIG = 10112 - ERR_AUTH_HMAC = 10113 - ERR_AUTH_NONCE = 10114 - ERR_UNAUTH_FAIL = 10200 - ERR_SUB_FAIL = 10300 - ERR_SUB_MULTI = 10301 - ERR_UNSUB_FAIL = 10400 - ERR_READY = 11000 \ No newline at end of file + await self.websocket.close(code=code, reason=reason) \ No newline at end of file diff --git a/bfxapi/websocket/__init__.py b/bfxapi/websocket/__init__.py index 4704773..e24f778 100644 --- a/bfxapi/websocket/__init__.py +++ b/bfxapi/websocket/__init__.py @@ -1,3 +1 @@ -from .BfxWebsocketClient import BfxWebsocketClient, Errors -from .handlers import Channels -from .exceptions import BfxWebsocketException, ConnectionNotOpen, TooManySubscriptions, WebsocketAuthenticationRequired, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion \ No newline at end of file +from .BfxWebsocketClient import BfxWebsocketClient \ No newline at end of file diff --git a/bfxapi/websocket/enums.py b/bfxapi/websocket/enums.py new file mode 100644 index 0000000..9a89eb0 --- /dev/null +++ b/bfxapi/websocket/enums.py @@ -0,0 +1,25 @@ +from enum import Enum + +class Channels(str, Enum): + TICKER = "ticker" + TRADES = "trades" + BOOK = "book" + CANDLES = "candles" + STATUS = "status" + +class Errors(int, Enum): + ERR_UNK = 10000 + ERR_GENERIC = 10001 + ERR_CONCURRENCY = 10008 + ERR_PARAMS = 10020 + ERR_CONF_FAIL = 10050 + ERR_AUTH_FAIL = 10100 + ERR_AUTH_PAYLOAD = 10111 + ERR_AUTH_SIG = 10112 + ERR_AUTH_HMAC = 10113 + ERR_AUTH_NONCE = 10114 + ERR_UNAUTH_FAIL = 10200 + ERR_SUB_FAIL = 10300 + ERR_SUB_MULTI = 10301 + ERR_UNSUB_FAIL = 10400 + ERR_READY = 11000 \ No newline at end of file diff --git a/bfxapi/websocket/handlers.py b/bfxapi/websocket/handlers.py index dd85b76..0d79072 100644 --- a/bfxapi/websocket/handlers.py +++ b/bfxapi/websocket/handlers.py @@ -1,25 +1,10 @@ -from enum import Enum - from . import serializers - +from .enums import Channels from .exceptions import BfxWebsocketException def _get_sub_dictionary(dictionary, keys): return { key: dictionary[key] for key in dictionary if key in keys } - -def _label_stream_data(labels, *args, IGNORE = [ "_PLACEHOLDER" ]): - if len(labels) != len(args): - raise BfxWebsocketException(" and <*args> arguments should contain the same amount of elements.") - - return { label: args[index] for index, label in enumerate(labels) if label not in IGNORE } - -class Channels(str, Enum): - TICKER = "ticker" - TRADES = "trades" - BOOK = "book" - CANDLES = "candles" - STATUS = "status" - + class PublicChannelsHandler(object): EVENTS = [ "t_ticker_update", "f_ticker_update",