From 97803a9e5e7a39f08d3958115240075377a52b0d Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Fri, 13 Dec 2024 14:28:45 +0200 Subject: [PATCH] Add get_stored_content --- src/aleph/sdk/client/http.py | 39 ++++++++++++++++++++++++++++++++++-- src/aleph/sdk/types.py | 7 +++++++ 2 files changed, 44 insertions(+), 2 deletions(-) diff --git a/src/aleph/sdk/client/http.py b/src/aleph/sdk/client/http.py index 7a2c7cca..53543ed8 100644 --- a/src/aleph/sdk/client/http.py +++ b/src/aleph/sdk/client/http.py @@ -20,7 +20,7 @@ import aiohttp from aiohttp.web import HTTPNotFound from aleph_message import parse_message -from aleph_message.models import AlephMessage, ItemHash, ItemType +from aleph_message.models import AlephMessage, ItemHash, ItemType, MessageType from aleph_message.status import MessageStatus from pydantic import ValidationError @@ -33,13 +33,14 @@ ) from ..query.filters import MessageFilter, PostFilter from ..query.responses import MessagesResponse, Post, PostsResponse, PriceResponse -from ..types import GenericMessage +from ..types import GenericMessage, StoredContent from ..utils import ( Writable, check_unix_socket_valid, copy_async_readable_to_buffer, extended_json_encoder, get_message_type_value, + safe_getattr, ) from .abstract import AlephClient @@ -469,3 +470,37 @@ async def get_message_status(self, item_hash: str) -> MessageStatus: resp.raise_for_status() result = await resp.json() return MessageStatus(result["status"]) + + async def get_stored_content( + self, + item_hash: str, + ) -> Optional[StoredContent]: + """return the underlying content for a store message""" + + result, resp = None, None + try: + message, status = await self.get_message( + item_hash=ItemHash(item_hash), with_status=True + ) + if status != MessageStatus.PROCESSED: + resp = f"Invalid message status: {status}" + elif message.type != MessageType.store: + resp = f"Invalid message type: {message.type}" + elif message.content.item_type != ItemType.ipfs: + resp = f"Invalid item type: {message.content.item_type}" + elif not message.content.item_hash: + resp = f"Invalid CID: {message.content.item_hash}" + else: + filename = safe_getattr(message.content, "metadata.name") + hash = message.content.item_hash + url = ( + f"{self.api_server}/api/v0/storage/raw/" + if len(hash) == 64 + else settings.IPFS_GATEWAY + ) + hash + result = StoredContent(filename=filename, hash=hash, url=url) + except MessageNotFoundError: + resp = f"Message not found: {item_hash}" + except ForgottenMessageError: + resp = f"Message forgotten: {item_hash}" + return result if result else StoredContent(error=resp) diff --git a/src/aleph/sdk/types.py b/src/aleph/sdk/types.py index 8f70dbd8..c698da5d 100644 --- a/src/aleph/sdk/types.py +++ b/src/aleph/sdk/types.py @@ -80,3 +80,10 @@ class ChainInfo(BaseModel): token: Optional[str] = None super_token: Optional[str] = None active: bool = True + + +class StoredContent(BaseModel): + filename: Optional[str] + hash: Optional[str] + url: Optional[str] + error: Optional[str]