From 6217f9040cb8cdd30182aaecfcdbb5a3137f088e Mon Sep 17 00:00:00 2001 From: Davide Casale Date: Tue, 20 Dec 2022 18:40:41 +0100 Subject: [PATCH] Rename bfxapi/utils/decimal.py to encoder.py. Add support for datetime JSON serialization. Update class reference in BfxWebsocketClient.py. --- bfxapi/utils/{decimal.py => encoder.py} | 6 +++--- bfxapi/websocket/BfxWebsocketClient.py | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) rename bfxapi/utils/{decimal.py => encoder.py} (52%) diff --git a/bfxapi/utils/decimal.py b/bfxapi/utils/encoder.py similarity index 52% rename from bfxapi/utils/decimal.py rename to bfxapi/utils/encoder.py index 5a7af71..3649823 100644 --- a/bfxapi/utils/decimal.py +++ b/bfxapi/utils/encoder.py @@ -1,9 +1,9 @@ import json - from decimal import Decimal +from datetime import datetime -class DecimalEncoder(json.JSONEncoder): +class JSONEncoder(json.JSONEncoder): def default(self, obj): - if isinstance(obj, Decimal): + if isinstance(obj, Decimal) or isinstance(obj, datetime): return str(obj) return json.JSONEncoder.default(self, obj) \ No newline at end of file diff --git a/bfxapi/websocket/BfxWebsocketClient.py b/bfxapi/websocket/BfxWebsocketClient.py index 775fa10..7cd8728 100644 --- a/bfxapi/websocket/BfxWebsocketClient.py +++ b/bfxapi/websocket/BfxWebsocketClient.py @@ -10,7 +10,7 @@ from .typings import Inputs from .handlers import Channels, PublicChannelsHandler, AuthenticatedChannelsHandler from .exceptions import ConnectionNotOpen, TooManySubscriptions, WebsocketAuthenticationRequired, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion -from ..utils.decimal import DecimalEncoder +from ..utils.encoder import JSONEncoder from ..utils.logger import Formatter, CustomLogger @@ -139,7 +139,7 @@ class BfxWebsocketClient(object): @_require_websocket_authentication async def __handle_websocket_input(self, input, data): - await self.websocket.send(json.dumps([ 0, input, None, data], cls=DecimalEncoder)) + await self.websocket.send(json.dumps([ 0, input, None, data], cls=JSONEncoder)) def __bucket_open_signal(self, index): if all(bucket.websocket != None and bucket.websocket.open == True for bucket in self.buckets):