Command¶
command
¶
Module defining the Command base class for commands.
Command
¶
Bases: Message[RawCommandType]
Base class for all commands.
Commands represent an intent to do something in the system.
They are requests that may succeed or fail, and are handled by a command handler.
Commands are named in imperative mood (e.g., CreateOrder, RegisterCustomer,
ProcessPayment).
Example
class CreateOrderPayload:
def __init__(self, customer_id: str, items: list) -> None:
self.customer_id = customer_id
self.items = items
class CreateOrder(Command[CreateOrderPayload]):
def __init__(
self, customer_id: str, items: list, metadata: MessageMetadata | None = None
):
super().__init__(metadata)
self._customer_id = customer_id
self._items = items
@property
def _payload(self) -> CreateOrderPayload:
return CreateOrderPayload(
customer_id=self._customer_id,
items=self._items,
)
@classmethod
def from_payload_fields(
cls, data: CreateOrderPayload, metadata: MessageMetadata
) -> "CreateOrder":
return cls(
customer_id=data.customer_id,
items=data.items,
metadata=metadata,
)
@property
def value(self) -> CreateOrderPayload:
return self._payload