Skip to content

Commit 0d24fae

Browse files
committed
switch to a notifications class
1 parent 08ed126 commit 0d24fae

6 files changed

Lines changed: 43 additions & 8 deletions

File tree

‎src/allocation/adapters/email.py‎

Lines changed: 0 additions & 2 deletions
This file was deleted.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
class EmailNotifications(AbstractNotifications):
18+
19+
def __init__(self, smtp_host=DEFAULT_HOST, port=DEFAULT_PORT):
20+
self.server = smtplib.SMTP(smtp_host, port=port)
21+
self.server.noop()
22+
23+
def send(self, destination, message):
24+
msg = f'Subject: allocation service notification\n{message}'
25+
self.server.sendmail(
26+
from_addr='allocations@example.com',
27+
to_addrs=[destination],
28+
msg=msg
29+
)

‎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
):
@@ -59,9 +61,9 @@ def change_batch_quantity(
5961
#pylint: disable=unused-argument
6062

6163
def send_out_of_stock_notification(
62-
event: events.OutOfStock, send_mail: Callable,
64+
event: events.OutOfStock, notifications: notifications.AbstractNotifications,
6365
):
64-
send_mail(
66+
notifications.send(
6567
'stock@made.com',
6668
f'Out of stock for {event.sku}',
6769
)

‎src/allocation/service_layer/messagebus.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
from . import handlers
88

99
if TYPE_CHECKING:
10-
from allocation.service_layer import unit_of_work
10+
from . import unit_of_work
11+
from allocation.adapters import notifications
1112

1213
logger = logging.getLogger(__name__)
1314

@@ -20,11 +21,11 @@ class MessageBus:
2021
def __init__(
2122
self,
2223
uow: unit_of_work.AbstractUnitOfWork,
23-
send_mail: Callable,
24+
notifications: notifications.AbstractNotifications,
2425
publish: Callable,
2526
):
2627
self.uow = uow
27-
self.dependencies = dict(uow=uow, send_mail=send_mail, publish=publish)
28+
self.dependencies = dict(uow=uow, notifications=notifications, publish=publish)
2829

2930

3031
def handle(self, message: Message):

‎tests/integration/test_views.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def sqlite_bus(sqlite_session_factory):
1212
uow = unit_of_work.SqlAlchemyUnitOfWork(sqlite_session_factory)
1313
bus = messagebus.MessageBus(
1414
uow=uow,
15-
send_mail=mock.Mock(),
15+
notifications=mock.Mock(),
1616
publish=mock.Mock(),
1717
)
1818
uow.bus = bus

0 commit comments

Comments
 (0)