4
4
import ssl
5
5
from io import BytesIO
6
6
from pathlib import Path
7
- from typing import Any , AsyncIterable , Dict , Iterable , List , Optional , Type , Union
7
+ from typing import (
8
+ Any ,
9
+ AsyncIterable ,
10
+ Dict ,
11
+ Iterable ,
12
+ List ,
13
+ Optional ,
14
+ Tuple ,
15
+ Type ,
16
+ Union ,
17
+ overload ,
18
+ )
8
19
9
20
import aiohttp
21
+ from aiohttp .web import HTTPNotFound
10
22
from aleph_message import parse_message
11
23
from aleph_message .models import AlephMessage , ItemHash , ItemType
24
+ from aleph_message .status import MessageStatus
12
25
from pydantic import ValidationError
13
26
14
27
from ..conf import settings
@@ -343,11 +356,27 @@ async def get_messages(
343
356
pagination_item = response_json ["pagination_item" ],
344
357
)
345
358
359
+ @overload
360
+ async def get_message (
361
+ self ,
362
+ item_hash : str ,
363
+ message_type : Optional [Type [GenericMessage ]] = None ,
364
+ ) -> GenericMessage : ...
365
+
366
+ @overload
346
367
async def get_message (
347
368
self ,
348
369
item_hash : str ,
349
370
message_type : Optional [Type [GenericMessage ]] = None ,
350
- ) -> GenericMessage :
371
+ with_status : bool = False ,
372
+ ) -> Tuple [GenericMessage , MessageStatus ]: ...
373
+
374
+ async def get_message (
375
+ self ,
376
+ item_hash : str ,
377
+ message_type : Optional [Type [GenericMessage ]] = None ,
378
+ with_status : bool = False ,
379
+ ) -> Union [GenericMessage , Tuple [GenericMessage , MessageStatus ]]:
351
380
async with self .http_session .get (f"/api/v0/messages/{ item_hash } " ) as resp :
352
381
try :
353
382
resp .raise_for_status ()
@@ -368,7 +397,10 @@ async def get_message(
368
397
f"The message type '{ message .type } ' "
369
398
f"does not match the expected type '{ expected_type } '"
370
399
)
371
- return message
400
+ if with_status :
401
+ return message , message_raw ["status" ]
402
+ else :
403
+ return message
372
404
373
405
async def get_message_error (
374
406
self ,
@@ -428,3 +460,10 @@ async def get_program_price(self, item_hash: str) -> PriceResponse:
428
460
if e .status == 400 :
429
461
raise InvalidHashError (f"Bad request or no such hash { item_hash } " )
430
462
raise e
463
+
464
+ async def get_message_status (self , item_hash : str ) -> MessageStatus :
465
+ """return Status of a message"""
466
+ async with self .http_session .get (f"/api/v0/messages/{ item_hash } " ) as resp :
467
+ if resp .status == HTTPNotFound .status_code :
468
+ raise MessageNotFoundError (f"No such hash { item_hash } " )
469
+ resp .raise_for_status ()
0 commit comments