Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()
Loading