Skip to content

Commit

Permalink
Add get_stored_content
Browse files Browse the repository at this point in the history
  • Loading branch information
philogicae committed Dec 13, 2024
1 parent d09c506 commit 97803a9
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
39 changes: 37 additions & 2 deletions src/aleph/sdk/client/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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)
7 changes: 7 additions & 0 deletions src/aleph/sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]

0 comments on commit 97803a9

Please sign in to comment.