Skip to content

Commit 37fac02

Browse files
1yamphilogicaenesitor
authored
Refactor: aleph message get to handle message status (#265)
* Refactor: aleph message get to handle message status * Fix after review * Fix: Update to last SDK package version. --------- Co-authored-by: philogicae <[email protected]> Co-authored-by: Andres D. Molins <[email protected]>
1 parent 34360fe commit 37fac02

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ classifiers = [
2424
]
2525

2626
dependencies = [
27-
"aleph-sdk-python>=1.0.0,<2",
27+
"aleph-sdk-python>=1.0.1,<2",
2828
"setuptools>=65.5.0",
2929
"aleph-message>=0.4.9",
3030
"aiohttp==3.9.5",

src/aleph_client/commands/message.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
from aleph_message.models import AlephMessage, ProgramMessage
2222
from aleph_message.models.base import MessageType
2323
from aleph_message.models.item_hash import ItemHash
24+
from aleph_message.status import MessageStatus
2425

2526
from aleph_client.commands import help_strings
2627
from aleph_client.commands.utils import (
2728
colorful_json,
2829
colorful_message_json,
30+
colorized_status,
2931
input_multiline,
3032
setup_logging,
3133
str_to_datetime,
@@ -41,8 +43,13 @@ async def get(
4143
item_hash: str = typer.Argument(..., help="Item hash of the message"),
4244
):
4345
async with AlephHttpClient(api_server=sdk_settings.API_HOST) as client:
44-
message: AlephMessage = await client.get_message(item_hash=ItemHash(item_hash))
45-
typer.echo(colorful_message_json(message))
46+
message, status = await client.get_message(item_hash=ItemHash(item_hash), with_status=True)
47+
typer.echo(f"Message Status: {colorized_status(status)}")
48+
if status == MessageStatus.REJECTED:
49+
reason = await client.get_message_error(item_hash=ItemHash(item_hash))
50+
typer.echo(colorful_json(json.dumps(reason, indent=4)))
51+
else:
52+
typer.echo(colorful_message_json(message))
4653

4754

4855
@app.command()

src/aleph_client/commands/utils.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
from aleph.sdk.conf import settings as sdk_settings
1414
from aleph.sdk.types import GenericMessage
1515
from aleph_message.models import ItemHash
16+
from aleph_message.status import MessageStatus
1617
from pygments import highlight
1718
from pygments.formatters.terminal256 import Terminal256Formatter
1819
from pygments.lexers import JsonLexer
@@ -33,6 +34,18 @@ def colorful_json(obj: str):
3334
)
3435

3536

37+
def colorized_status(status: MessageStatus) -> str:
38+
"""Return a colored status string based on its value."""
39+
status_colors = {
40+
MessageStatus.REJECTED: typer.colors.RED,
41+
MessageStatus.PROCESSED: typer.colors.GREEN,
42+
MessageStatus.PENDING: typer.colors.YELLOW,
43+
MessageStatus.FORGOTTEN: typer.colors.BRIGHT_BLACK,
44+
}
45+
color = status_colors.get(status, typer.colors.WHITE)
46+
return typer.style(status, fg=color, bold=True)
47+
48+
3649
def colorful_message_json(message: GenericMessage):
3750
"""Render a message in JSON with colors."""
3851
return colorful_json(message.json(sort_keys=True, indent=4))

0 commit comments

Comments
 (0)