File tree Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Expand file tree Collapse file tree 5 files changed +49
-7
lines changed Load Diff This file was deleted.
Original file line number Diff line number Diff line change
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
+
29
+ to_addrs = [destination ],
30
+ msg = msg
31
+ )
Original file line number Diff line number Diff line change 1
1
import inspect
2
2
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
+ )
4
7
from allocation .service_layer import handlers , messagebus , unit_of_work
5
8
6
9
7
10
def bootstrap (
8
11
start_orm : bool = True ,
9
12
uow : unit_of_work .AbstractUnitOfWork = unit_of_work .SqlAlchemyUnitOfWork (),
10
- send_mail : Callable = email . send ,
13
+ notifications : AbstractNotifications = None ,
11
14
publish : Callable = redis_eventpublisher .publish ,
12
15
) -> messagebus .MessageBus :
13
16
17
+ if notifications is None :
18
+ notifications = EmailNotifications ()
19
+
14
20
if start_orm :
15
21
orm .start_mappers ()
16
22
17
- dependencies = {'uow' : uow , 'send_mail ' : send_mail , 'publish' : publish }
23
+ dependencies = {'uow' : uow , 'notifications ' : notifications , 'publish' : publish }
18
24
injected_event_handlers = {
19
25
event_type : [
20
26
inject_dependencies (handler , dependencies )
Original file line number Diff line number Diff line change @@ -19,3 +19,8 @@ def get_redis_host_and_port():
19
19
port = 63791 if host == 'localhost' else 6379
20
20
return dict (host = host , port = port )
21
21
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 )
Original file line number Diff line number Diff line change 5
5
from allocation .domain import commands , events , model
6
6
from allocation .domain .model import OrderLine
7
7
if TYPE_CHECKING :
8
+ from allocation .adapters import notifications
8
9
from . import unit_of_work
9
10
10
11
11
12
class InvalidSku (Exception ):
12
13
pass
13
14
14
15
16
+
15
17
def add_batch (
16
18
cmd : commands .CreateBatch , uow : unit_of_work .AbstractUnitOfWork
17
19
):
@@ -56,9 +58,9 @@ def change_batch_quantity(
56
58
#pylint: disable=unused-argument
57
59
58
60
def send_out_of_stock_notification (
59
- event : events .OutOfStock , send_mail : Callable ,
61
+ event : events .OutOfStock , notifications : notifications . AbstractNotifications ,
60
62
):
61
- send_mail (
63
+ notifications . send (
62
64
63
65
f'Out of stock for { event .sku } ' ,
64
66
)
You can’t perform that action at this time.
0 commit comments