Skip to content
Merged
Show file tree
Hide file tree
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
38 changes: 36 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,36 @@ 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,
) -> StoredContent:
"""return the underlying content for a store message"""

result, resp = None, None
try:
message: AlephMessage
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 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)
1 change: 1 addition & 0 deletions src/aleph/sdk/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ class Settings(BaseSettings):

VM_URL_PATH = "https://aleph.sh/vm/{hash}"
VM_URL_HOST = "https://{hash_base32}.aleph.sh"
IPFS_GATEWAY = "https://ipfs.aleph.cloud/ipfs/"

# Web3Provider settings
TOKEN_DECIMALS = 18
Expand Down
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]
8 changes: 8 additions & 0 deletions src/aleph/sdk/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,11 @@ def make_packet_header(
header[20:52] = h.digest()

return header


def safe_getattr(obj, attr, default=None):
for part in attr.split("."):
obj = getattr(obj, part, default)
if obj is default:
break
return obj
Loading