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

AlephClientBase and AuthenticatedAlephClientBase #54

Merged
merged 7 commits into from
Sep 27, 2023
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
3 changes: 3 additions & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ exclude_lines =
# Don't complain if non-runnable code isn't run:
if 0:
if __name__ == .__main__.:

# Don't complain about ineffective code:
pass
MHHukiewitz marked this conversation as resolved.
Show resolved Hide resolved
476 changes: 476 additions & 0 deletions src/aleph/sdk/base.py

Large diffs are not rendered by default.

218 changes: 61 additions & 157 deletions src/aleph/sdk/client.py

Large diffs are not rendered by default.

51 changes: 44 additions & 7 deletions src/aleph/sdk/models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,51 @@
from typing import List
from typing import Any, Dict, List, Optional, Union

from aleph_message.models import AlephMessage
from pydantic import BaseModel
from aleph_message.models import AlephMessage, BaseMessage, ChainRef, ItemHash
from pydantic import BaseModel, Field


class MessagesResponse(BaseModel):
"""Response from an Aleph node API on the path /api/v0/messages.json"""

messages: List[AlephMessage]
class PaginationResponse(BaseModel):
pagination_page: int
pagination_total: int
pagination_per_page: int
pagination_item: str


class MessagesResponse(PaginationResponse):
"""Response from an Aleph node API on the path /api/v0/messages.json"""

messages: List[AlephMessage]
pagination_item = "messages"


class Post(BaseMessage):
"""
A post is a type of message that can be updated. Over the get_posts API
we get the latest version of a post.
"""

hash: ItemHash = Field(description="Hash of the content (sha256 by default)")
original_item_hash: ItemHash = Field(
description="Hash of the original content (sha256 by default)"
)
original_signature: Optional[str] = Field(
description="Cryptographic signature of the original message by the sender"
)
original_type: str = Field(
description="The original, user-generated 'content-type' of the POST message"
)
content: Dict[str, Any] = Field(
description="The content.content of the POST message"
)
type: str = Field(description="The content.type of the POST message")
address: str = Field(description="The address of the sender of the POST message")
ref: Optional[Union[str, ChainRef]] = Field(
description="Other message referenced by this one"
)


class PostsResponse(PaginationResponse):
"""Response from an Aleph node API on the path /api/v0/posts.json"""

posts: List[Post]
pagination_item = "posts"
2 changes: 1 addition & 1 deletion src/aleph/sdk/vm/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def keys(self, pattern: str = "*") -> List[str]:
return await resp.json()


class TestVmCache(BaseVmCache):
class LocalVmCache(BaseVmCache):
"""This is a local, dict-based cache that can be used for testing purposes."""

def __init__(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/integration/itest_forget.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ async def test_forget_a_forget_message(fixture_account):
account=fixture_account, api_server=TARGET_NODE
) as session:
get_post_response = await session.get_posts(hashes=[post_hash])
assert len(get_post_response["posts"]) == 1
post = get_post_response["posts"][0]
assert len(get_post_response.posts) == 1
post = get_post_response.posts[0]

forget_message_hash = post["forgotten_by"][0]
forget_message_hash = post.forgotten_by[0]
forget_message, forget_status = await session.forget(
hashes=[forget_message_hash],
reason="I want to remember this post. Maybe I can forget I forgot it?",
Expand Down
13 changes: 5 additions & 8 deletions tests/unit/test_asynchronous_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
from unittest.mock import AsyncMock

import pytest
from aleph_message.models import MessagesResponse, MessageType
from aleph_message.models import MessagesResponse

from aleph.sdk.client import AlephClient
from aleph.sdk.conf import settings
from aleph.sdk.models import PostsResponse


def make_mock_session(get_return_value: Dict[str, Any]) -> AlephClient:
Expand Down Expand Up @@ -66,14 +67,10 @@ async def test_fetch_aggregates():
@pytest.mark.asyncio
async def test_get_posts():
async with AlephClient(api_server=settings.API_HOST) as session:
response: MessagesResponse = await session.get_messages(
message_type=MessageType.post,
)
response: PostsResponse = await session.get_posts()

messages = response.messages
assert len(messages) > 1
for message in messages:
assert message.type == MessageType.post
posts = response.posts
assert len(posts) > 1


@pytest.mark.asyncio
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/test_synchronous_get.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@
from aleph.sdk.conf import settings


def test_get_posts():
def test_get_post_messages():
with AlephClient(api_server=settings.API_HOST) as session:
# TODO: Remove deprecated message_type parameter after message_types changes on pyaleph are deployed
response: MessagesResponse = session.get_messages(
pagination=2,
message_type=MessageType.post,
Expand Down
4 changes: 2 additions & 2 deletions tests/unit/test_vm_cache.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import pytest

from aleph.sdk.vm.cache import TestVmCache, sanitize_cache_key
from aleph.sdk.vm.cache import LocalVmCache, sanitize_cache_key


@pytest.mark.asyncio
async def test_local_vm_cache():
cache = TestVmCache()
cache = LocalVmCache()
assert (await cache.get("doesnotexist")) is None
assert len(await cache.keys()) == 0
key = "thisdoesexist"
Expand Down