Skip to content

Commit

Permalink
Add extended JSON encoder for datetime objects in message encoding.
Browse files Browse the repository at this point in the history
  • Loading branch information
MHHukiewitz committed Nov 25, 2023
1 parent 59e22b5 commit e173835
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/aleph/sdk/client/authenticated_http.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import hashlib
import json
import logging
Expand Down Expand Up @@ -50,6 +51,16 @@
magic = None # type:ignore


def extended_json_encoder(obj: Any) -> str:
if (
isinstance(obj, datetime.datetime)
or isinstance(obj, datetime.date)
or isinstance(obj, datetime.time)
):
return obj.isoformat() # or any other format you prefer
return pydantic_encoder(obj)


class AuthenticatedAlephHttpClient(AlephHttpClient, AuthenticatedAlephClient):
account: Account

Expand Down Expand Up @@ -604,7 +615,7 @@ async def _prepare_aleph_message(

# Use the Pydantic encoder to serialize types like UUID, datetimes, etc.
item_content: str = json.dumps(
content, separators=(",", ":"), default=pydantic_encoder
content, separators=(",", ":"), default=extended_json_encoder
)

if allow_inlining and (len(item_content) < settings.MAX_INLINE_SIZE):
Expand Down

0 comments on commit e173835

Please sign in to comment.