mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 23:04:21 +01:00
Add OutdatedClientVersion exception in bfxapi/websocket/errors.py. Add check for version mismatch between client and server. Add support for error event.
This commit is contained in:
@@ -4,11 +4,13 @@ from pyee.asyncio import AsyncIOEventEmitter
|
||||
|
||||
from .handlers import Channels, PublicChannelsHandler, AuthenticatedChannelsHandler
|
||||
|
||||
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials
|
||||
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, OutdatedClientVersion
|
||||
|
||||
HEARTBEAT = "hb"
|
||||
|
||||
class BfxWebsocketClient(object):
|
||||
VERSION = 1
|
||||
|
||||
def __init__(self, host, API_KEY=None, API_SECRET=None):
|
||||
self.host, self.chanIds, self.event_emitter = host, dict(), AsyncIOEventEmitter()
|
||||
|
||||
@@ -32,7 +34,10 @@ class BfxWebsocketClient(object):
|
||||
async for message in websocket:
|
||||
message = json.loads(message)
|
||||
|
||||
if isinstance(message, dict) and message["event"] == "subscribed":
|
||||
if isinstance(message, dict) and message["event"] == "info" and "version" in message:
|
||||
if BfxWebsocketClient.VERSION != message["version"]:
|
||||
raise OutdatedClientVersion(f"Mismatch between the client version and the server version. Update the library to the latest version to continue (client version: {BfxWebsocketClient.VERSION}, server version: {message['version']}).")
|
||||
elif isinstance(message, dict) and message["event"] == "subscribed":
|
||||
self.chanIds[message["chanId"]] = message
|
||||
|
||||
self.event_emitter.emit("subscribed", message)
|
||||
@@ -43,6 +48,8 @@ class BfxWebsocketClient(object):
|
||||
if message["status"] == "OK":
|
||||
self.event_emitter.emit("authenticated", message)
|
||||
else: raise InvalidAuthenticationCredentials("Cannot authenticate with given API-KEY and API-SECRET.")
|
||||
elif isinstance(message, dict) and message["event"] == "error":
|
||||
self.event_emitter.emit("error", message["code"], message["msg"])
|
||||
elif isinstance(message, list) and ((chanId := message[0]) or True) and message[1] != HEARTBEAT:
|
||||
if chanId == 0:
|
||||
self.handlers["authenticated"].handle(message[1], message[2])
|
||||
@@ -50,7 +57,6 @@ class BfxWebsocketClient(object):
|
||||
except websockets.ConnectionClosed:
|
||||
continue
|
||||
|
||||
@staticmethod
|
||||
def __require_websocket_connection(function):
|
||||
async def wrapper(self, *args, **kwargs):
|
||||
if self.websocket == None or self.websocket.open == False:
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
from .BfxWebsocketClient import BfxWebsocketClient
|
||||
from .handlers import Channels
|
||||
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials
|
||||
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, OutdatedClientVersion
|
||||
@@ -24,3 +24,10 @@ class InvalidAuthenticationCredentials(BfxWebsocketException):
|
||||
"""
|
||||
|
||||
pass
|
||||
|
||||
class OutdatedClientVersion(BfxWebsocketException):
|
||||
"""
|
||||
This error indicates a mismatch between the client version and the server WSS version.
|
||||
"""
|
||||
|
||||
pass
|
||||
Reference in New Issue
Block a user