-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathmessagebus.py
More file actions
49 lines (40 loc) · 1.42 KB
/
messagebus.py
File metadata and controls
49 lines (40 loc) · 1.42 KB
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
42
43
44
45
46
47
48
49
from typing import Any, Callable, Dict, List, Optional, Sequence, Type
from aws_doc_sdk_examples_tools.lliam.domain import commands, errors
from aws_doc_sdk_examples_tools.lliam.service_layer import (
create_prompts,
dedupe_reservoir,
update_doc_gen,
run_ailly,
unit_of_work,
)
# Only handling Commands for now.
Message = commands.Command
def handle(
message: Any, uow: Optional[unit_of_work.FsUnitOfWork] = None
) -> Sequence[errors.DomainError]:
if isinstance(message, commands.Command):
return handle_command(message, uow)
return [
errors.CommandExecutionError(
command_name="Unknown", message=f"{message} was not a Command"
)
]
def handle_command(
command: commands.Command, uow: Optional[unit_of_work.FsUnitOfWork]
) -> Sequence[errors.DomainError]:
handler = COMMAND_HANDLERS.get(type(command))
if not handler:
return [
errors.CommandExecutionError(
command_name=command.name, message="Handler for not found."
)
]
return handler(command, uow)
COMMAND_HANDLERS: Dict[
Type[commands.Command], Callable[..., Sequence[errors.DomainError]]
] = {
commands.CreatePrompts: create_prompts.create_prompts,
commands.RunAilly: run_ailly.handle_run_ailly,
commands.UpdateReservoir: update_doc_gen.handle_update_reservoir,
commands.DedupeReservoir: dedupe_reservoir.handle_dedupe_reservoir,
}