mirror of
https://github.com/aljazceru/bitfinex-api-py.git
synced 2025-12-19 14:54:21 +01:00
Add bfxapi/websocket/enums.py file. Rewrite bfxapi.websocket's __init__.py. Rename HEARTBEAT constant to _HEARTBEAT to make it internal.
This commit is contained in:
@@ -10,7 +10,7 @@ from .exceptions import ConnectionNotOpen, TooManySubscriptions, WebsocketAuthen
|
|||||||
|
|
||||||
from ..utils.logger import Formatter, CustomLogger
|
from ..utils.logger import Formatter, CustomLogger
|
||||||
|
|
||||||
HEARTBEAT = "hb"
|
_HEARTBEAT = "hb"
|
||||||
|
|
||||||
def _require_websocket_connection(function):
|
def _require_websocket_connection(function):
|
||||||
async def wrapper(self, *args, **kwargs):
|
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.")
|
else: raise InvalidAuthenticationCredentials("Cannot authenticate with given API-KEY and API-SECRET.")
|
||||||
elif isinstance(message, dict) and message["event"] == "error":
|
elif isinstance(message, dict) and message["event"] == "error":
|
||||||
self.event_emitter.emit("wss-error", message["code"], message["msg"])
|
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])
|
self.handler.handle(message[1], message[2])
|
||||||
except websockets.ConnectionClosedError: continue
|
except websockets.ConnectionClosedError: continue
|
||||||
finally: await self.websocket.wait_closed(); break
|
finally: await self.websocket.wait_closed(); break
|
||||||
@@ -171,7 +171,7 @@ class _BfxWebsocketBucket(object):
|
|||||||
del self.chanIds[chanId]
|
del self.chanIds[chanId]
|
||||||
elif isinstance(message, dict) and message["event"] == "error":
|
elif isinstance(message, dict) and message["event"] == "error":
|
||||||
self.event_emitter.emit("wss-error", message["code"], message["msg"])
|
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:])
|
self.handler.handle(self.chanIds[chanId], *message[1:])
|
||||||
except websockets.ConnectionClosedError: continue
|
except websockets.ConnectionClosedError: continue
|
||||||
finally: await self.websocket.wait_closed(); break
|
finally: await self.websocket.wait_closed(); break
|
||||||
@@ -202,21 +202,4 @@ class _BfxWebsocketBucket(object):
|
|||||||
|
|
||||||
@_require_websocket_connection
|
@_require_websocket_connection
|
||||||
async def close(self, code=1000, reason=str()):
|
async def close(self, code=1000, reason=str()):
|
||||||
await self.websocket.close(code=code, reason=reason)
|
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
|
|
||||||
@@ -1,3 +1 @@
|
|||||||
from .BfxWebsocketClient import BfxWebsocketClient, Errors
|
from .BfxWebsocketClient import BfxWebsocketClient
|
||||||
from .handlers import Channels
|
|
||||||
from .exceptions import BfxWebsocketException, ConnectionNotOpen, TooManySubscriptions, WebsocketAuthenticationRequired, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion
|
|
||||||
25
bfxapi/websocket/enums.py
Normal file
25
bfxapi/websocket/enums.py
Normal file
@@ -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
|
||||||
@@ -1,25 +1,10 @@
|
|||||||
from enum import Enum
|
|
||||||
|
|
||||||
from . import serializers
|
from . import serializers
|
||||||
|
from .enums import Channels
|
||||||
from .exceptions import BfxWebsocketException
|
from .exceptions import BfxWebsocketException
|
||||||
|
|
||||||
def _get_sub_dictionary(dictionary, keys):
|
def _get_sub_dictionary(dictionary, keys):
|
||||||
return { key: dictionary[key] for key in dictionary if key in 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("<labels> 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):
|
class PublicChannelsHandler(object):
|
||||||
EVENTS = [
|
EVENTS = [
|
||||||
"t_ticker_update", "f_ticker_update",
|
"t_ticker_update", "f_ticker_update",
|
||||||
|
|||||||
Reference in New Issue
Block a user