Skip to content

Commit 4da849a

Browse files
committed
switch to a notifications class [notifications_class]
1 parent 49951b0 commit 4da849a

File tree

5 files changed

+48
-7
lines changed

5 files changed

+48
-7
lines changed

src/allocation/adapters/email.py

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# pylint: disable=too-few-public-methods
2+
import abc
3+
import smtplib
4+
from allocation import config
5+
6+
7+
class AbstractNotifications(abc.ABC):
8+
@abc.abstractmethod
9+
def send(self, destination, message):
10+
raise NotImplementedError
11+
12+
13+
DEFAULT_HOST = config.get_email_host_and_port()["host"]
14+
DEFAULT_PORT = config.get_email_host_and_port()["port"]
15+
16+
17+
class EmailNotifications(AbstractNotifications):
18+
def __init__(self, smtp_host=DEFAULT_HOST, port=DEFAULT_PORT):
19+
self.server = smtplib.SMTP(smtp_host, port=port)
20+
self.server.noop()
21+
22+
def send(self, destination, message):
23+
msg = f"Subject: allocation service notification\n{message}"
24+
self.server.sendmail(
25+
from_addr="[email protected]",
26+
to_addrs=[destination],
27+
msg=msg,
28+
)

src/allocation/bootstrap.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,27 @@
11
import inspect
22
from typing import Callable
3-
from allocation.adapters import email, orm, redis_eventpublisher
3+
from allocation.adapters import orm, redis_eventpublisher
4+
from allocation.adapters.notifications import (
5+
AbstractNotifications,
6+
EmailNotifications,
7+
)
48
from allocation.service_layer import handlers, messagebus, unit_of_work
59

610

711
def bootstrap(
812
start_orm: bool = True,
913
uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(),
10-
send_mail: Callable = email.send,
14+
notifications: AbstractNotifications = None,
1115
publish: Callable = redis_eventpublisher.publish,
1216
) -> messagebus.MessageBus:
1317

18+
if notifications is None:
19+
notifications = EmailNotifications()
20+
1421
if start_orm:
1522
orm.start_mappers()
1623

17-
dependencies = {"uow": uow, "send_mail": send_mail, "publish": publish}
24+
dependencies = {"uow": uow, "notifications": notifications, "publish": publish}
1825
injected_event_handlers = {
1926
event_type: [
2027
inject_dependencies(handler, dependencies)

src/allocation/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,10 @@ def get_redis_host_and_port():
1919
host = os.environ.get("REDIS_HOST", "localhost")
2020
port = 63791 if host == "localhost" else 6379
2121
return dict(host=host, port=port)
22+
23+
24+
def get_email_host_and_port():
25+
host = os.environ.get("EMAIL_HOST", "localhost")
26+
port = 11025 if host == "localhost" else 1025
27+
http_port = 18025 if host == "localhost" else 8025
28+
return dict(host=host, port=port, http_port=http_port)

src/allocation/service_layer/handlers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from allocation.domain.model import OrderLine
77

88
if TYPE_CHECKING:
9+
from allocation.adapters import notifications
910
from . import unit_of_work
1011

1112

@@ -61,9 +62,9 @@ def change_batch_quantity(
6162

6263
def send_out_of_stock_notification(
6364
event: events.OutOfStock,
64-
send_mail: Callable,
65+
notifications: notifications.AbstractNotifications,
6566
):
66-
send_mail(
67+
notifications.send(
6768
6869
f"Out of stock for {event.sku}",
6970
)

0 commit comments

Comments
 (0)