Add EventNotSupported exception in errors.py.

This commit is contained in:
Davide Casale
2022-11-16 19:01:52 +01:00
parent 41aa49d2bb
commit d58c60b02d
5 changed files with 46 additions and 4 deletions

View File

@@ -4,13 +4,19 @@ from pyee.asyncio import AsyncIOEventEmitter
from .handlers import Channels, PublicChannelsHandler, AuthenticatedChannelsHandler
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, OutdatedClientVersion
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion
HEARTBEAT = "hb"
class BfxWebsocketClient(object):
VERSION = 2
EVENTS = [
"open", "subscribed", "authenticated", "error",
*PublicChannelsHandler.EVENTS,
*AuthenticatedChannelsHandler.EVENTS
]
def __init__(self, host, API_KEY=None, API_SECRET=None):
self.host, self.chanIds, self.event_emitter = host, dict(), AsyncIOEventEmitter()
@@ -102,12 +108,18 @@ class BfxWebsocketClient(object):
await self.unsubscribe(chanId)
def on(self, event):
if event not in BfxWebsocketClient.EVENTS:
raise EventNotSupported(f"Event <{event}> is not supported. To get a list of available events use BfxWebsocketClient.EVENTS.")
def handler(function):
self.event_emitter.on(event, function)
return handler
def once(self, event):
if event not in BfxWebsocketClient.EVENTS:
raise EventNotSupported(f"Event <{event}> is not supported. To get a list of available events use BfxWebsocketClient.EVENTS.")
def handler(function):
self.event_emitter.once(event, function)

View File

@@ -1,3 +1,3 @@
from .BfxWebsocketClient import BfxWebsocketClient
from .handlers import Channels
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, OutdatedClientVersion
from .errors import BfxWebsocketException, ConnectionNotOpen, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion

View File

@@ -25,6 +25,13 @@ class InvalidAuthenticationCredentials(BfxWebsocketException):
pass
class EventNotSupported(BfxWebsocketException):
"""
This error indicates a failed attempt to subscribe to an event not supported by the BfxWebsocketClient.
"""
pass
class OutdatedClientVersion(BfxWebsocketException):
"""
This error indicates a mismatch between the client version and the server WSS version.

View File

@@ -21,6 +21,14 @@ class Channels(str, Enum):
STATUS = "status"
class PublicChannelsHandler(object):
EVENTS = [
"tp_ticker_update", "fc_ticker_update",
"tp_trade_executed", "tp_trade_execution_update", "fc_trade_executed", "fc_trade_execution_update", "tp_trades_snapshot", "fc_trades_snapshot",
"book_snapshot", "raw_book_snapshot", "book_update", "raw_book_update",
"candles_snapshot", "candles_update",
"derivatives_status_update"
]
def __init__(self, event_emitter):
self.event_emitter = event_emitter
@@ -126,8 +134,19 @@ class PublicChannelsHandler(object):
subscription,
serializers.DerivativesStatus(*stream[0])
)
class AuthenticatedChannelsHandler(object):
EVENTS = [
"order_snapshot", "new_order", "order_update", "order_cancel",
"position_snapshot", "new_position", "position_update", "position_close",
"trade_executed", "trade_execution_update",
"funding_offer_snapshot", "funding_offer_new", "funding_offer_update", "funding_offer_cancel",
"funding_credit_snapshot", "funding_credit_new", "funding_credit_update", "funding_credit_close",
"funding_loan_snapshot", "funding_loan_new", "funding_loan_update", "funding_loan_close",
"wallet_snapshot", "wallet_update",
"balance_update"
]
def __init__(self, event_emitter, strict = False):
self.event_emitter, self.strict = event_emitter, strict

View File

@@ -15,6 +15,8 @@ class _Serializer(object):
def __call__(self, *values):
return dict(self.__serialize(*values))
#region Serializers definition for Websocket Public Channels
TradingPairTicker = _Serializer("TradingPairTicker", labels=[
"BID",
"BID_SIZE",
@@ -121,4 +123,6 @@ DerivativesStatus = _Serializer("DerivativesStatus", labels=[
"_PLACEHOLDER",
"CLAMP_MIN",
"CLAMP_MAX"
])
])
#endregion