From 22451f674ed8611bd54b6ea04d5313df324f4022 Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Sun, 1 Oct 2023 22:36:47 +0200 Subject: [PATCH] Remove inner class Connection.Authenticable (_connection.py). --- .../websocket/_client/bfx_websocket_client.py | 6 +-- bfxapi/websocket/_connection.py | 40 +++++++++---------- 2 files changed, 22 insertions(+), 24 deletions(-) diff --git a/bfxapi/websocket/_client/bfx_websocket_client.py b/bfxapi/websocket/_client/bfx_websocket_client.py index 3b9594d..8b6aada 100644 --- a/bfxapi/websocket/_client/bfx_websocket_client.py +++ b/bfxapi/websocket/_client/bfx_websocket_client.py @@ -69,7 +69,7 @@ class _Delay: def reset(self) -> None: self.__backoff_delay = _Delay.__BACKOFF_MIN -class BfxWebSocketClient(Connection, Connection.Authenticable): +class BfxWebSocketClient(Connection): VERSION = BfxWebSocketBucket.VERSION MAXIMUM_CONNECTIONS_AMOUNT = 20 @@ -307,7 +307,7 @@ class BfxWebSocketClient(Connection, Connection.Authenticable): await self._websocket.close( \ code=code, reason=reason) - @Connection.Authenticable.require_websocket_authentication + @Connection.require_websocket_authentication async def notify(self, info: Any, message_id: Optional[int] = None, @@ -316,7 +316,7 @@ class BfxWebSocketClient(Connection, Connection.Authenticable): json.dumps([ 0, "n", message_id, { "type": "ucm-test", "info": info, **kwargs } ])) - @Connection.Authenticable.require_websocket_authentication + @Connection.require_websocket_authentication async def __handle_websocket_input(self, event: str, data: Any) -> None: await self._websocket.send(json.dumps(\ [ 0, event, None, data], cls=JSONEncoder)) diff --git a/bfxapi/websocket/_connection.py b/bfxapi/websocket/_connection.py index 1562b43..f9dbd3c 100644 --- a/bfxapi/websocket/_connection.py +++ b/bfxapi/websocket/_connection.py @@ -10,30 +10,11 @@ if TYPE_CHECKING: class Connection: HEARTBEAT = "hb" - class Authenticable: - def __init__(self) -> None: - self._authentication: bool = False - - @property - def authentication(self) -> bool: - return self._authentication - - @staticmethod - def require_websocket_authentication(function): - async def wrapper(self, *args, **kwargs): - if not self.authentication: - raise ActionRequiresAuthentication("To perform this action you need to " \ - "authenticate using your API_KEY and API_SECRET.") - - internal = Connection.require_websocket_connection(function) - - return await internal(self, *args, **kwargs) - - return wrapper - def __init__(self, host: str) -> None: self._host = host + self._authentication: bool = False + self.__protocol: Optional["WebSocketClientProtocol"] = None @property @@ -41,6 +22,10 @@ class Connection: return self.__protocol is not None and \ self.__protocol.open + @property + def authentication(self) -> bool: + return self._authentication + @property def _websocket(self) -> "WebSocketClientProtocol": return cast("WebSocketClientProtocol", self.__protocol) @@ -58,3 +43,16 @@ class Connection: raise ConnectionNotOpen("No open connection with the server.") return wrapper + + @staticmethod + def require_websocket_authentication(function): + async def wrapper(self, *args, **kwargs): + if not self.authentication: + raise ActionRequiresAuthentication("To perform this action you need to " \ + "authenticate using your API_KEY and API_SECRET.") + + internal = Connection.require_websocket_connection(function) + + return await internal(self, *args, **kwargs) + + return wrapper