Skip to content

Commit 41b773b

Browse files
author
José Márquez Doblas
committed
First Commit
1 parent 10bb315 commit 41b773b

14 files changed

+356
-0
lines changed

Pipfile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
[[source]]
2+
url = "https://pypi.org/simple"
3+
verify_ssl = true
4+
name = "pypi"
5+
6+
[packages]
7+
pytest = "*"
8+
pytest-cov = "*"
9+
coverage = "*"
10+
11+
[dev-packages]
12+
13+
[requires]
14+
python_version = "3.9"

Pipfile.lock

Lines changed: 153 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

__init__.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ._exceptions import *
2+
from ._command import Command
3+
from ._command_bus import CommandBus
4+
from ._command_handler import CommandHandler
5+
from ._query import Query
6+
from ._query_bus import QueryBus
7+
from ._query_handler import QueryHandler
8+
from ._query_response import QueryResponse

_command.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
class Command:
3+
pass

_command_bus.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from ._command_handler import CommandHandler
2+
from ._command import Command
3+
from ._exceptions import CommandAlreadyExistException
4+
from ._exceptions import CommandHandlerDoesNotExistException
5+
import threading
6+
7+
8+
class CommandBus:
9+
def __init__(self):
10+
self._commands = {}
11+
self._semaphore = threading.Semaphore()
12+
13+
def subscribe(self, cmd: type, handler: CommandHandler):
14+
if cmd in self._commands:
15+
raise CommandAlreadyExistException()
16+
self._commands[cmd] = handler
17+
18+
def publish(self, cmd: Command):
19+
if cmd.__class__ not in self._commands:
20+
raise CommandHandlerDoesNotExistException()
21+
self._semaphore.acquire()
22+
self._commands[cmd.__class__].handle(cmd)
23+
self._semaphore.release()

_command_handler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from abc import ABC, abstractmethod
2+
from ._command import Command
3+
4+
5+
class CommandHandler(ABC):
6+
7+
@abstractmethod
8+
def handle(self, cmd: Command):
9+
raise NotImplementedError()

_exceptions.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
class CommandBusException(Exception):
2+
pass
3+
4+
5+
class CommandAlreadyExistException(CommandBusException):
6+
pass
7+
8+
9+
class CommandHandlerDoesNotExistException(CommandBusException):
10+
pass
11+
12+
13+
class QueryBusException(Exception):
14+
pass
15+
16+
17+
class QueryAlreadyExistException(QueryBusException):
18+
pass
19+
20+
21+
class QueryHandlerDoesNotExistException(QueryBusException):
22+
pass

_query.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
3+
class Query:
4+
pass

_query_bus.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import threading
2+
from ._query_handler import QueryHandler
3+
from ._query import Query
4+
from ._query_response import QueryResponse
5+
from ._exceptions import QueryAlreadyExistException
6+
from ._exceptions import QueryHandlerDoesNotExistException
7+
import threading
8+
9+
10+
class QueryBus:
11+
def __init__(self):
12+
self._querys = {}
13+
self._semaphore = threading.Semaphore()
14+
15+
def subscribe(self, query: Query, handler: QueryHandler):
16+
if query in self._querys:
17+
raise QueryAlreadyExistException()
18+
self._querys[query] = handler
19+
20+
def publish(self, query: Query) -> QueryResponse:
21+
if query.__class__ not in self._querys:
22+
raise QueryHandlerDoesNotExistException()
23+
self._semaphore.acquire()
24+
response: QueryResponse = self._querys[query.__class__].handle(query)
25+
self._semaphore.release()
26+
return response
27+

_query_handler.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from abc import ABC, abstractmethod
2+
from ._query import Query
3+
4+
5+
class QueryHandler(ABC):
6+
7+
@abstractmethod
8+
def handle(self, query: Query):
9+
raise NotImplementedError()

0 commit comments

Comments
 (0)