Skip to content

Commit c62fbb7

Browse files
1yamphilogicae
andauthored
Feature: get_message_status and overload of get_message to return status (#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]>
1 parent 1bc6a4f commit c62fbb7

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

src/aleph/sdk/client/http.py

+42-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,24 @@
44
import ssl
55
from io import BytesIO
66
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+
)
819

920
import aiohttp
21+
from aiohttp.web import HTTPNotFound
1022
from aleph_message import parse_message
1123
from aleph_message.models import AlephMessage, ItemHash, ItemType
24+
from aleph_message.status import MessageStatus
1225
from pydantic import ValidationError
1326

1427
from ..conf import settings
@@ -343,11 +356,27 @@ async def get_messages(
343356
pagination_item=response_json["pagination_item"],
344357
)
345358

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
346367
async def get_message(
347368
self,
348369
item_hash: str,
349370
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]]:
351380
async with self.http_session.get(f"/api/v0/messages/{item_hash}") as resp:
352381
try:
353382
resp.raise_for_status()
@@ -368,7 +397,10 @@ async def get_message(
368397
f"The message type '{message.type}' "
369398
f"does not match the expected type '{expected_type}'"
370399
)
371-
return message
400+
if with_status:
401+
return message, message_raw["status"]
402+
else:
403+
return message
372404

373405
async def get_message_error(
374406
self,
@@ -428,3 +460,10 @@ async def get_program_price(self, item_hash: str) -> PriceResponse:
428460
if e.status == 400:
429461
raise InvalidHashError(f"Bad request or no such hash {item_hash}")
430462
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

Comments
 (0)