|
1 | 1 | # pylint: disable=bare-except |
2 | 2 | from __future__ import annotations |
3 | 3 | import logging |
| 4 | +import inspect |
4 | 5 | from typing import List, Dict, Callable, Type, Union, TYPE_CHECKING |
5 | 6 | from allocation.domain import commands, events |
6 | 7 | from . import handlers |
|
13 | 14 | Message = Union[commands.Command, events.Event] |
14 | 15 |
|
15 | 16 |
|
16 | | -def handle(message: Message, uow: unit_of_work.AbstractUnitOfWork): |
17 | | - if isinstance(message, events.Event): |
18 | | - handle_event(message, uow) |
19 | | - elif isinstance(message, commands.Command): |
20 | | - handle_command(message, uow) |
21 | | - else: |
22 | | - raise Exception(f'{message} was not an Event or Command') |
| 17 | +class MessageBus: |
| 18 | + |
| 19 | + def __init__( |
| 20 | + self, |
| 21 | + uow: unit_of_work.AbstractUnitOfWork, |
| 22 | + send_mail: Callable, |
| 23 | + publish: Callable, |
| 24 | + ): |
| 25 | + self.uow = uow |
| 26 | + self.dependencies = dict(uow=uow, send_mail=send_mail, publish=publish) |
| 27 | + |
| 28 | + def handle(self, message: Message): |
| 29 | + if isinstance(message, events.Event): |
| 30 | + self.handle_event(message) |
| 31 | + elif isinstance(message, commands.Command): |
| 32 | + self.handle_command(message) |
| 33 | + else: |
| 34 | + raise Exception(f'{message} was not an Event or Command') |
23 | 35 |
|
24 | 36 |
|
25 | 37 | def handle_event(event: events.Event, uow: unit_of_work.AbstractUnitOfWork): |
|
0 commit comments