1- import datetime
21import hashlib
32import json
43import logging
3433from aleph_message .models .execution .program import CodeContent , FunctionRuntime
3534from aleph_message .models .execution .volume import MachineVolume , ParentVolume
3635from aleph_message .status import MessageStatus
37- from pydantic .json import pydantic_encoder
3836
3937from ..conf import settings
4038from ..exceptions import BroadcastError , InvalidMessageError
4139from ..types import Account , StorageEnum
40+ from ..utils import extended_json_encoder
4241from .abstract import AuthenticatedAlephClient
4342from .http import AlephHttpClient
4443
5150 magic = None # type:ignore
5251
5352
54- def extended_json_encoder (obj : Any ) -> str :
55- if (
56- isinstance (obj , datetime .datetime )
57- or isinstance (obj , datetime .date )
58- or isinstance (obj , datetime .time )
59- ):
60- return obj .isoformat () # or any other format you prefer
61- return pydantic_encoder (obj )
62-
63-
6453class AuthenticatedAlephHttpClient (AlephHttpClient , AuthenticatedAlephClient ):
6554 account : Account
6655
@@ -181,12 +170,20 @@ async def _handle_broadcast_error(response: aiohttp.ClientResponse) -> NoReturn:
181170 logger .error (error_msg )
182171 raise BroadcastError (error_msg )
183172 elif response .status == 422 :
184- errors = await response .json ()
185- logger .error (
186- "The message could not be processed because of the following errors: %s" ,
187- errors ,
188- )
189- raise InvalidMessageError (errors )
173+ try :
174+ errors = await response .json ()
175+ logger .error (
176+ "The message could not be processed because of the following errors: %s" ,
177+ errors ,
178+ )
179+ raise InvalidMessageError (errors )
180+ except (json .JSONDecodeError , aiohttp .client_exceptions .ContentTypeError ):
181+ error = await response .text ()
182+ logger .error (
183+ "The message could not be processed because of the following errors: %s" ,
184+ error ,
185+ )
186+ raise InvalidMessageError (error )
190187 else :
191188 error_msg = (
192189 f"Unexpected HTTP response ({ response .status } : { await response .text ()} )"
@@ -212,12 +209,11 @@ async def _broadcast_deprecated(self, message_dict: Mapping[str, Any]) -> None:
212209
213210 url = "/api/v0/ipfs/pubsub/pub"
214211 logger .debug (f"Posting message on { url } " )
215-
216212 async with self .http_session .post (
217213 url ,
218214 json = {
219215 "topic" : "ALEPH-TEST" ,
220- "data" : json . dumps ( message_dict , default = extended_json_encoder ) ,
216+ "data" : message_dict ,
221217 },
222218 ) as response :
223219 await self ._handle_broadcast_deprecated_response (response )
@@ -257,10 +253,12 @@ async def _broadcast(
257253 logger .debug (f"Posting message on { url } " )
258254
259255 message_dict = message .dict (include = self .BROADCAST_MESSAGE_FIELDS )
260-
261256 async with self .http_session .post (
262257 url ,
263- json = {"sync" : sync , "message" : message_dict },
258+ json = {
259+ "sync" : sync ,
260+ "message" : message_dict ,
261+ },
264262 ) as response :
265263 # The endpoint may be unavailable on this node, try the deprecated version.
266264 if response .status in (404 , 405 ):
0 commit comments