Skip to content

Commit e05749f

Browse files
committed
switch to a notifications class [notifications_class]
1 parent d6ed812 commit e05749f

File tree

5 files changed

+49
-7
lines changed

5 files changed

+49
-7
lines changed

src/allocation/adapters/email.py

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
9+
@abc.abstractmethod
10+
def send(self, destination, message):
11+
raise NotImplementedError
12+
13+
14+
DEFAULT_HOST = config.get_email_host_and_port()['host']
15+
DEFAULT_PORT = config.get_email_host_and_port()['port']
16+
17+
18+
19+
class EmailNotifications(AbstractNotifications):
20+
21+
def __init__(self, smtp_host=DEFAULT_HOST, port=DEFAULT_PORT):
22+
self.server = smtplib.SMTP(smtp_host, port=port)
23+
self.server.noop()
24+
25+
def send(self, destination, message):
26+
msg = f'Subject: allocation service notification\n{message}'
27+
self.server.sendmail(
28+
from_addr='[email protected]',
29+
to_addrs=[destination],
30+
msg=msg
31+
)

src/allocation/bootstrap.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,26 @@
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, EmailNotifications
6+
)
47
from allocation.service_layer import handlers, messagebus, unit_of_work
58

69

710
def bootstrap(
811
start_orm: bool = True,
912
uow: unit_of_work.AbstractUnitOfWork = unit_of_work.SqlAlchemyUnitOfWork(),
10-
send_mail: Callable = email.send,
13+
notifications: AbstractNotifications = None,
1114
publish: Callable = redis_eventpublisher.publish,
1215
) -> messagebus.MessageBus:
1316

17+
if notifications is None:
18+
notifications = EmailNotifications()
19+
1420
if start_orm:
1521
orm.start_mappers()
1622

17-
dependencies = {'uow': uow, 'send_mail': send_mail, 'publish': publish}
23+
dependencies = {'uow': uow, 'notifications': notifications, 'publish': publish}
1824
injected_event_handlers = {
1925
event_type: [
2026
inject_dependencies(handler, dependencies)

src/allocation/config.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,8 @@ def get_redis_host_and_port():
1919
port = 63791 if host == 'localhost' else 6379
2020
return dict(host=host, port=port)
2121

22+
def get_email_host_and_port():
23+
host = os.environ.get('EMAIL_HOST', 'localhost')
24+
port = 11025 if host == 'localhost' else 1025
25+
http_port = 18025 if host == 'localhost' else 8025
26+
return dict(host=host, port=port, http_port=http_port)

src/allocation/service_layer/handlers.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,15 @@
55
from allocation.domain import commands, events, model
66
from allocation.domain.model import OrderLine
77
if TYPE_CHECKING:
8+
from allocation.adapters import notifications
89
from . import unit_of_work
910

1011

1112
class InvalidSku(Exception):
1213
pass
1314

1415

16+
1517
def add_batch(
1618
cmd: commands.CreateBatch, uow: unit_of_work.AbstractUnitOfWork
1719
):
@@ -56,9 +58,9 @@ def change_batch_quantity(
5658
#pylint: disable=unused-argument
5759

5860
def send_out_of_stock_notification(
59-
event: events.OutOfStock, send_mail: Callable,
61+
event: events.OutOfStock, notifications: notifications.AbstractNotifications,
6062
):
61-
send_mail(
63+
notifications.send(
6264
6365
f'Out of stock for {event.sku}',
6466
)

0 commit comments

Comments
 (0)