From b19cf2c04d499ba6ff9ca88d114d3b194a8005ac Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Mon, 19 Feb 2024 05:44:50 +0200 Subject: [PATCH 1/5] Fix msgTypes + Add sort_by/sort_order args to MessageFilter/PostFilter --- src/aleph/sdk/query/filters.py | 37 +++++++++++++++++++++++++++++++--- src/aleph/sdk/utils.py | 4 +++- 2 files changed, 37 insertions(+), 4 deletions(-) diff --git a/src/aleph/sdk/query/filters.py b/src/aleph/sdk/query/filters.py index 17c4a8cd..0c4eaa4e 100644 --- a/src/aleph/sdk/query/filters.py +++ b/src/aleph/sdk/query/filters.py @@ -1,9 +1,22 @@ from datetime import datetime from typing import Dict, Iterable, Optional, Union - from aleph_message.models import MessageType +from ..utils import _date_field_to_timestamp, serialize_list, enum_as_str +from enum import Enum, IntEnum + + +class SortBy(str, Enum): + """Supported SortBy types""" + + TIME = "time" + TX_TIME = "tx-time" + + +class SortOrder(str, Enum): + """Supported SortOrder types""" -from ..utils import _date_field_to_timestamp, serialize_list + ASCENDING = "1" + DESCENDING = "-1" class MessageFilter: @@ -20,6 +33,8 @@ class MessageFilter: :param chains: Filter by sender address chain :param start_date: Earliest date to fetch messages from :param end_date: Latest date to fetch messages from + :param sort_by: Sort by time or tx-time + :param sort_order: Sort by ascending or descending order """ message_types: Optional[Iterable[MessageType]] @@ -33,6 +48,8 @@ class MessageFilter: chains: Optional[Iterable[str]] start_date: Optional[Union[datetime, float]] end_date: Optional[Union[datetime, float]] + sort_by: Optional[SortBy] + sort_order: Optional[SortOrder] def __init__( self, @@ -47,6 +64,8 @@ def __init__( chains: Optional[Iterable[str]] = None, start_date: Optional[Union[datetime, float]] = None, end_date: Optional[Union[datetime, float]] = None, + sort_by: Optional[SortBy] = None, + sort_order: Optional[SortOrder] = None, ): self.message_types = message_types self.content_types = content_types @@ -59,6 +78,8 @@ def __init__( self.chains = chains self.start_date = start_date self.end_date = end_date + self.sort_by = sort_by + self.sort_order = sort_order def as_http_params(self) -> Dict[str, str]: """Convert the filters into a dict that can be used by an `aiohttp` client @@ -66,7 +87,7 @@ def as_http_params(self) -> Dict[str, str]: """ partial_result = { - "msgType": serialize_list( + "msgTypes": serialize_list( [type.value for type in self.message_types] if self.message_types else None @@ -81,6 +102,8 @@ def as_http_params(self) -> Dict[str, str]: "chains": serialize_list(self.chains), "startDate": _date_field_to_timestamp(self.start_date), "endDate": _date_field_to_timestamp(self.end_date), + "sortBy": enum_as_str(self.sort_by), + "sortOrder": enum_as_str(self.sort_order), } # Ensure all values are strings. @@ -110,6 +133,8 @@ class PostFilter: chains: Optional[Iterable[str]] start_date: Optional[Union[datetime, float]] end_date: Optional[Union[datetime, float]] + sort_by: Optional[SortBy] + sort_order: Optional[SortOrder] def __init__( self, @@ -122,6 +147,8 @@ def __init__( chains: Optional[Iterable[str]] = None, start_date: Optional[Union[datetime, float]] = None, end_date: Optional[Union[datetime, float]] = None, + sort_by: Optional[SortBy] = None, + sort_order: Optional[SortOrder] = None, ): self.types = types self.refs = refs @@ -132,6 +159,8 @@ def __init__( self.chains = chains self.start_date = start_date self.end_date = end_date + self.sort_by = sort_by + self.sort_order = sort_order def as_http_params(self) -> Dict[str, str]: """Convert the filters into a dict that can be used by an `aiohttp` client @@ -148,6 +177,8 @@ def as_http_params(self) -> Dict[str, str]: "chains": serialize_list(self.chains), "startDate": _date_field_to_timestamp(self.start_date), "endDate": _date_field_to_timestamp(self.end_date), + "sortBy": enum_as_str(self.sort_by), + "sortOrder": enum_as_str(self.sort_order), } # Ensure all values are strings. diff --git a/src/aleph/sdk/utils.py b/src/aleph/sdk/utils.py index 51bb04e5..fcfcf660 100644 --- a/src/aleph/sdk/utils.py +++ b/src/aleph/sdk/utils.py @@ -115,12 +115,14 @@ async def copy_async_readable_to_buffer( buffer.write(chunk) -def enum_as_str(obj: Union[str, Enum]) -> str: +def enum_as_str(obj: Optional[Union[str, Enum]]) -> Optional[str]: """Returns the value of an Enum, or the string itself when passing a string. Python 3.11 adds a new formatting of string enums. `str(MyEnum.value)` becomes `MyEnum.value` instead of `value`. """ + if obj is None: + return None if not isinstance(obj, str): raise TypeError(f"Unsupported enum type: {type(obj)}") From 010bdd012a8a60b4beb454db90b8136de63516c9 Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Mon, 19 Feb 2024 06:21:08 +0200 Subject: [PATCH 2/5] Remove useless IntEnum --- src/aleph/sdk/query/filters.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/aleph/sdk/query/filters.py b/src/aleph/sdk/query/filters.py index 0c4eaa4e..bb60af7b 100644 --- a/src/aleph/sdk/query/filters.py +++ b/src/aleph/sdk/query/filters.py @@ -2,7 +2,7 @@ from typing import Dict, Iterable, Optional, Union from aleph_message.models import MessageType from ..utils import _date_field_to_timestamp, serialize_list, enum_as_str -from enum import Enum, IntEnum +from enum import Enum class SortBy(str, Enum): From 4c076304b6b8edce432b6dc6f733007f45a12083 Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:18:51 +0200 Subject: [PATCH 3/5] Fix for isort test --- src/aleph/sdk/query/filters.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/aleph/sdk/query/filters.py b/src/aleph/sdk/query/filters.py index bb60af7b..4caee5f5 100644 --- a/src/aleph/sdk/query/filters.py +++ b/src/aleph/sdk/query/filters.py @@ -1,8 +1,10 @@ from datetime import datetime +from enum import Enum from typing import Dict, Iterable, Optional, Union + from aleph_message.models import MessageType -from ..utils import _date_field_to_timestamp, serialize_list, enum_as_str -from enum import Enum + +from ..utils import _date_field_to_timestamp, enum_as_str, serialize_list class SortBy(str, Enum): From 97b7fd3c5486dcae26f00f9e9c50254ad2f39838 Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Mon, 19 Feb 2024 13:50:22 +0200 Subject: [PATCH 4/5] Fix wrong type on enum_as_str by removing Optional --- src/aleph/sdk/utils.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/aleph/sdk/utils.py b/src/aleph/sdk/utils.py index fcfcf660..f8042898 100644 --- a/src/aleph/sdk/utils.py +++ b/src/aleph/sdk/utils.py @@ -115,14 +115,14 @@ async def copy_async_readable_to_buffer( buffer.write(chunk) -def enum_as_str(obj: Optional[Union[str, Enum]]) -> Optional[str]: +def enum_as_str(obj: Optional[Union[str, Enum]]) -> str: """Returns the value of an Enum, or the string itself when passing a string. Python 3.11 adds a new formatting of string enums. `str(MyEnum.value)` becomes `MyEnum.value` instead of `value`. """ if obj is None: - return None + return "" if not isinstance(obj, str): raise TypeError(f"Unsupported enum type: {type(obj)}") From 0ba728e782fcab3f85a325ed785800483cd6e9e3 Mon Sep 17 00:00:00 2001 From: philogicae <38438271+philogicae@users.noreply.github.com> Date: Tue, 20 Feb 2024 13:17:32 +0200 Subject: [PATCH 5/5] Revert fix on enum_as_str --- src/aleph/sdk/chains/common.py | 4 ++-- src/aleph/sdk/utils.py | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/aleph/sdk/chains/common.py b/src/aleph/sdk/chains/common.py index 9f6b45f4..3c7e634e 100644 --- a/src/aleph/sdk/chains/common.py +++ b/src/aleph/sdk/chains/common.py @@ -27,9 +27,9 @@ def get_verification_buffer(message: Dict) -> bytes: # Convert Enum values to strings return "\n".join( ( - enum_as_str(message["chain"]), + enum_as_str(message["chain"]) or "", message["sender"], - enum_as_str(message["type"]), + enum_as_str(message["type"]) or "", message["item_hash"], ) ).encode() diff --git a/src/aleph/sdk/utils.py b/src/aleph/sdk/utils.py index f8042898..ab17f44a 100644 --- a/src/aleph/sdk/utils.py +++ b/src/aleph/sdk/utils.py @@ -115,14 +115,14 @@ async def copy_async_readable_to_buffer( buffer.write(chunk) -def enum_as_str(obj: Optional[Union[str, Enum]]) -> str: +def enum_as_str(obj: Optional[Union[str, Enum]]) -> Optional[str]: """Returns the value of an Enum, or the string itself when passing a string. Python 3.11 adds a new formatting of string enums. `str(MyEnum.value)` becomes `MyEnum.value` instead of `value`. """ - if obj is None: - return "" + if not obj: + return None if not isinstance(obj, str): raise TypeError(f"Unsupported enum type: {type(obj)}")