mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 14:54:21 +01:00
Rewrite AuthenticatedEventsHandler with _label_array_elements logic. Add support to multiple new events. Fix bug in BfxWebsocketClient.py.
This commit is contained in:
@@ -43,9 +43,9 @@ class BfxWebsocketClient(object):
|
|||||||
if message["status"] == "OK":
|
if message["status"] == "OK":
|
||||||
self.event_emitter.emit("authenticated", message)
|
self.event_emitter.emit("authenticated", message)
|
||||||
else: raise AuthenticationCredentialsError("Cannot authenticate with given API-KEY and API-SECRET.")
|
else: raise AuthenticationCredentialsError("Cannot authenticate with given API-KEY and API-SECRET.")
|
||||||
elif isinstance(message, list) and (chanId := message[0]) and message[1] != HEARTBEAT:
|
elif isinstance(message, list) and ((chanId := message[0]) or True) and message[1] != HEARTBEAT:
|
||||||
if chanId == 0:
|
if chanId == 0:
|
||||||
self.handlers["authenticated"].handle(message[1], *message[2:])
|
self.handlers["authenticated"].handle(message[1], message[2])
|
||||||
else: self.handlers["public"].handle(self.chanIds[chanId], *message[1:])
|
else: self.handlers["public"].handle(self.chanIds[chanId], *message[1:])
|
||||||
except websockets.ConnectionClosed:
|
except websockets.ConnectionClosed:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -50,11 +50,140 @@ class AuthenticatedEventsHandler(object):
|
|||||||
self.event_emitter = event_emitter
|
self.event_emitter = event_emitter
|
||||||
|
|
||||||
self.__handlers = {
|
self.__handlers = {
|
||||||
"bu": self.__bu_event_handler
|
"bu": self.__bu_event_handler,
|
||||||
|
"ws": self.__ws_event_handler,
|
||||||
|
"wu": self.__wu_event_handler,
|
||||||
|
"os": self.__os_event_handler,
|
||||||
|
"on": self.__on_event_handler
|
||||||
}
|
}
|
||||||
|
|
||||||
def handle(self, type, *parameters):
|
def handle(self, type, parameters):
|
||||||
self.__handlers[type](*parameters)
|
if type in self.__handlers:
|
||||||
|
self.__handlers[type](*parameters)
|
||||||
|
|
||||||
def __bu_event_handler(self, AUM, AUM_NET):
|
def __bu_event_handler(self, *parameters):
|
||||||
self.event_emitter.emit("balance_update", AUM, AUM_NET)
|
self.event_emitter.emit("balance_update", _label_array_elements(
|
||||||
|
[
|
||||||
|
"AUM",
|
||||||
|
"AUM_NET"
|
||||||
|
],
|
||||||
|
*parameters
|
||||||
|
))
|
||||||
|
|
||||||
|
def __ws_event_handler(self, *parameters):
|
||||||
|
self.event_emitter.emit("wallet_snapshot", [
|
||||||
|
_label_array_elements(
|
||||||
|
[
|
||||||
|
"WALLET_TYPE",
|
||||||
|
"CURRENCY",
|
||||||
|
"BALANCE",
|
||||||
|
"UNSETTLED_INTEREST",
|
||||||
|
"BALANCE_AVAILABLE",
|
||||||
|
"DESCRIPTION",
|
||||||
|
"META"
|
||||||
|
],
|
||||||
|
*parameter
|
||||||
|
) for parameter in parameters
|
||||||
|
])
|
||||||
|
|
||||||
|
def __wu_event_handler(self, *parameters):
|
||||||
|
self.event_emitter.emit("wallet_update", _label_array_elements(
|
||||||
|
[
|
||||||
|
"WALLET_TYPE",
|
||||||
|
"CURRENCY",
|
||||||
|
"BALANCE",
|
||||||
|
"UNSETTLED_INTEREST",
|
||||||
|
"BALANCE_AVAILABLE",
|
||||||
|
"DESCRIPTION",
|
||||||
|
"META"
|
||||||
|
],
|
||||||
|
*parameters
|
||||||
|
))
|
||||||
|
|
||||||
|
def __os_event_handler(self, *parameters):
|
||||||
|
self.event_emitter.emit("order_snapshot", [
|
||||||
|
_label_array_elements(
|
||||||
|
[
|
||||||
|
"ID",
|
||||||
|
"GID",
|
||||||
|
"CID",
|
||||||
|
"SYMBOL",
|
||||||
|
"MTS_CREATE",
|
||||||
|
"MTS_UPDATE",
|
||||||
|
"AMOUNT",
|
||||||
|
"AMOUNT_ORIG",
|
||||||
|
"ORDER_TYPE",
|
||||||
|
"TYPE_PREV",
|
||||||
|
"MTS_TIF",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"FLAGS",
|
||||||
|
"STATUS",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"PRICE",
|
||||||
|
"PRICE_AVG",
|
||||||
|
"PRICE_TRAILING",
|
||||||
|
"PRICE_AUX_LIMIT",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"NOTIFY",
|
||||||
|
"HIDDEN",
|
||||||
|
"PLACED_ID",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"ROUTING",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"META"
|
||||||
|
],
|
||||||
|
*parameter
|
||||||
|
) for parameter in parameters
|
||||||
|
])
|
||||||
|
|
||||||
|
def __on_event_handler(self, *parameters):
|
||||||
|
self.event_emitter.emit("new_order", _label_array_elements(
|
||||||
|
[
|
||||||
|
"ID",
|
||||||
|
"GID",
|
||||||
|
"CID",
|
||||||
|
"SYMBOL",
|
||||||
|
"MTS_CREATE",
|
||||||
|
"MTS_UPDATE",
|
||||||
|
"AMOUNT",
|
||||||
|
"AMOUNT_ORIG",
|
||||||
|
"ORDER_TYPE",
|
||||||
|
"TYPE_PREV",
|
||||||
|
"MTS_TIF",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"FLAGS",
|
||||||
|
"ORDER_STATUS",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"PRICE",
|
||||||
|
"PRICE_AVG",
|
||||||
|
"PRICE_TRAILING",
|
||||||
|
"PRICE_AUX_LIMIT",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"NOTIFY",
|
||||||
|
"HIDDEN",
|
||||||
|
"PLACED_ID",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"ROUTING",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER",
|
||||||
|
"_PLACEHOLDER"
|
||||||
|
],
|
||||||
|
*parameters
|
||||||
|
))
|
||||||
|
|
||||||
|
def _label_array_elements(labels, *args):
|
||||||
|
if len(labels) != len(args):
|
||||||
|
raise Exception("<labels> and <*args> arguments should contain the same amount of elements.")
|
||||||
|
|
||||||
|
_PLACEHOLDER = "_PLACEHOLDER"
|
||||||
|
|
||||||
|
return { label: args[index] for index, label in enumerate(labels) if label != _PLACEHOLDER }
|
||||||
Reference in New Issue
Block a user