Skip to content

Commit

Permalink
Feature: get_message_status and overload of get_message to return sta…
Browse files Browse the repository at this point in the history
…tus (#163)

* Feature: get_message_status and overload of get_message to return status

* fixup! Feature: get_message_status and overload of get_message to return status

* Fix after review

---------

Co-authored-by: philogicae <[email protected]>
  • Loading branch information
1yam and philogicae authored Sep 3, 2024
1 parent 1bc6a4f commit c62fbb7
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions src/aleph/sdk/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand All @@ -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,
Expand Down Expand Up @@ -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()

0 comments on commit c62fbb7

Please sign in to comment.