Rename bfxapi/utils/decimal.py to encoder.py. Add support for datetime JSON serialization. Update class reference in BfxWebsocketClient.py.

This commit is contained in:
Davide Casale
2022-12-20 18:40:41 +01:00
parent 87bb6dc5c7
commit 6217f9040c
2 changed files with 5 additions and 5 deletions

View File

@@ -1,9 +1,9 @@
import json import json
from decimal import Decimal from decimal import Decimal
from datetime import datetime
class DecimalEncoder(json.JSONEncoder): class JSONEncoder(json.JSONEncoder):
def default(self, obj): def default(self, obj):
if isinstance(obj, Decimal): if isinstance(obj, Decimal) or isinstance(obj, datetime):
return str(obj) return str(obj)
return json.JSONEncoder.default(self, obj) return json.JSONEncoder.default(self, obj)

View File

@@ -10,7 +10,7 @@ from .typings import Inputs
from .handlers import Channels, PublicChannelsHandler, AuthenticatedChannelsHandler from .handlers import Channels, PublicChannelsHandler, AuthenticatedChannelsHandler
from .exceptions import ConnectionNotOpen, TooManySubscriptions, WebsocketAuthenticationRequired, InvalidAuthenticationCredentials, EventNotSupported, OutdatedClientVersion 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 from ..utils.logger import Formatter, CustomLogger
@@ -139,7 +139,7 @@ class BfxWebsocketClient(object):
@_require_websocket_authentication @_require_websocket_authentication
async def __handle_websocket_input(self, input, data): 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): def __bucket_open_signal(self, index):
if all(bucket.websocket != None and bucket.websocket.open == True for bucket in self.buckets): if all(bucket.websocket != None and bucket.websocket.open == True for bucket in self.buckets):