Skip to content

Commit 6cdf3a2

Browse files
committed
call_handler_with_dependencies [messagebus_does_DI]
1 parent 8e88838 commit 6cdf3a2

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

‎src/allocation/service_layer/messagebus.py‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ def __init__(
2626
self.uow = uow
2727
self.dependencies = dict(uow=uow, send_mail=send_mail, publish=publish)
2828

29+
2930
def handle(self, message: Message):
3031
if isinstance(message, events.Event):
3132
self.handle_event(message)
@@ -44,6 +45,7 @@ def handle_event(self, event: events.Event):
4445
logger.exception('Exception handling event %s', event)
4546
continue
4647

48+
4749
def handle_command(self, command: commands.Command):
4850
logger.debug('handling command %s', command)
4951
try:
@@ -54,6 +56,15 @@ def handle_command(self, command: commands.Command):
5456
raise
5557

5658

59+
def call_handler_with_dependencies(self, handler: Callable, message: Message):
60+
params = inspect.signature(handler).parameters
61+
deps = {
62+
name: dependency for name, dependency in self.dependencies.items()
63+
if name in params
64+
}
65+
handler(message, **deps)
66+
67+
5768
EVENT_HANDLERS = {
5869
events.Allocated: [
5970
handlers.publish_allocated_event,

0 commit comments

Comments
 (0)