-
Notifications
You must be signed in to change notification settings - Fork 0
/
message_bus.py
41 lines (35 loc) · 1.28 KB
/
message_bus.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
from __future__ import annotations
import inspect
from abc import ABC, abstractmethod
from typing import Any, Callable, Type
from kingdom_sdk.domain.message import Command, Event, Message
from kingdom_sdk.ports.unit_of_work import AbstractUnitOfWork
class AbstractMessageBus(ABC):
@classmethod
@abstractmethod
def create(
cls,
uow: AbstractUnitOfWork,
event_handlers: dict[Type[Event], list[Callable]],
command_handlers: dict[Type[Command], Callable],
dependencies: dict[str, Any],
) -> AbstractMessageBus:
raise NotImplementedError
@abstractmethod
async def handle(self, message: Message) -> list[Warning]:
raise NotImplementedError
@staticmethod
def _inject_dependencies(
handler: Callable, dependencies: dict[str, Any]
) -> Callable:
"""Inspect a handler function to figure out its arguments and returns
the same handler with its arguments already set given a dependencies
mapping.
"""
params = inspect.signature(handler).parameters
set_dependencies = {
param: dependency
for param, dependency in dependencies.items()
if param in params
}
return lambda message: handler(message, **set_dependencies)