diff --git a/src/aleph/sdk/client/http.py b/src/aleph/sdk/client/http.py index 7e8755b5..2c953d4e 100644 --- a/src/aleph/sdk/client/http.py +++ b/src/aleph/sdk/client/http.py @@ -4,11 +4,24 @@ import ssl from io import BytesIO from pathlib import Path -from typing import Any, AsyncIterable, Dict, Iterable, List, Optional, Type, Union +from typing import ( + Any, + AsyncIterable, + Dict, + Iterable, + List, + Optional, + Tuple, + Type, + Union, + overload, +) import aiohttp +from aiohttp.web import HTTPNotFound from aleph_message import parse_message from aleph_message.models import AlephMessage, ItemHash, ItemType +from aleph_message.status import MessageStatus from pydantic import ValidationError from ..conf import settings @@ -343,11 +356,27 @@ async def get_messages( pagination_item=response_json["pagination_item"], ) + @overload + async def get_message( + self, + item_hash: str, + message_type: Optional[Type[GenericMessage]] = None, + ) -> GenericMessage: ... + + @overload async def get_message( self, item_hash: str, message_type: Optional[Type[GenericMessage]] = None, - ) -> GenericMessage: + with_status: bool = False, + ) -> Tuple[GenericMessage, MessageStatus]: ... + + async def get_message( + self, + item_hash: str, + message_type: Optional[Type[GenericMessage]] = None, + with_status: bool = False, + ) -> Union[GenericMessage, Tuple[GenericMessage, MessageStatus]]: async with self.http_session.get(f"/api/v0/messages/{item_hash}") as resp: try: resp.raise_for_status() @@ -368,7 +397,10 @@ async def get_message( f"The message type '{message.type}' " f"does not match the expected type '{expected_type}'" ) - return message + if with_status: + return message, message_raw["status"] + else: + return message async def get_message_error( self, @@ -428,3 +460,10 @@ async def get_program_price(self, item_hash: str) -> PriceResponse: if e.status == 400: raise InvalidHashError(f"Bad request or no such hash {item_hash}") raise e + + async def get_message_status(self, item_hash: str) -> MessageStatus: + """return Status of a message""" + async with self.http_session.get(f"/api/v0/messages/{item_hash}") as resp: + if resp.status == HTTPNotFound.status_code: + raise MessageNotFoundError(f"No such hash {item_hash}") + resp.raise_for_status()