Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: get_message_status and overload of get_message to return status #163

Merged
merged 3 commits into from
Sep 3, 2024
Merged
Changes from 2 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
50 changes: 47 additions & 3 deletions src/aleph/sdk/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,23 @@
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 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 +355,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,
with_status: bool = False,
) -> Tuple[GenericMessage, MessageStatus]: ...

async def get_message(
self,
item_hash: str,
message_type: Optional[Type[GenericMessage]] = None,
) -> GenericMessage:
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 +396,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 +459,16 @@ 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:
try:
resp.raise_for_status()
except aiohttp.ClientResponseError as e:
if e.status == 404:
raise MessageNotFoundError(f"No such hash {item_hash}")
raise e
philogicae marked this conversation as resolved.
Show resolved Hide resolved

message_raw = await resp.json()
return MessageStatus(message_raw["status"])
Loading