generated from pufereq/python-template
fix(message.py): allow providing ID via argument
This commit is contained in:
@@ -14,6 +14,7 @@ class Message:
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
|
id_: str | None,
|
||||||
category: Category,
|
category: Category,
|
||||||
action: Enum,
|
action: Enum,
|
||||||
payload: dict[str, Any] | None = None,
|
payload: dict[str, Any] | None = None,
|
||||||
@@ -21,11 +22,12 @@ class Message:
|
|||||||
"""Initialize a Message.
|
"""Initialize a Message.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
id_ (str | None): The ID of the message. If None, a new UUID will be generated.
|
||||||
category (Category): The category of the message.
|
category (Category): The category of the message.
|
||||||
action (Enum): The action of the message.
|
action (Enum): The action of the message.
|
||||||
payload (dict[str, Any] | None): The payload of the message.
|
payload (dict[str, Any] | None): The payload of the message.
|
||||||
"""
|
"""
|
||||||
self.id: str = str(uuid.uuid4())
|
self.id: str = id_ or str(uuid.uuid4())
|
||||||
self.category: Category = category
|
self.category: Category = category
|
||||||
self.action: Enum = action
|
self.action: Enum = action
|
||||||
self.payload: dict[str, Any] = payload or {}
|
self.payload: dict[str, Any] = payload or {}
|
||||||
@@ -77,6 +79,10 @@ class Message:
|
|||||||
Returns:
|
Returns:
|
||||||
Message: The created message.
|
Message: The created message.
|
||||||
"""
|
"""
|
||||||
|
if "id" not in data:
|
||||||
|
data["id"] = None
|
||||||
|
|
||||||
|
id_: str | None = data["id"]
|
||||||
category = Category(data["category"])
|
category = Category(data["category"])
|
||||||
action_str = data["action"]
|
action_str = data["action"]
|
||||||
|
|
||||||
@@ -87,7 +93,7 @@ class Message:
|
|||||||
raise ValueError(f"Unknown category: {category}")
|
raise ValueError(f"Unknown category: {category}")
|
||||||
|
|
||||||
payload = data.get("payload", {})
|
payload = data.get("payload", {})
|
||||||
return cls(category=category, action=action, payload=payload)
|
return cls(id_=id_, category=category, action=action, payload=payload)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def from_json(cls, data: str) -> Message:
|
def from_json(cls, data: str) -> Message:
|
||||||
|
|||||||
Reference in New Issue
Block a user