Skip to content

Commit 464c4f7

Browse files
committed
Add mail_service example
1 parent aad164d commit 464c4f7

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
"""Mail service and user registration DI container example."""
2+
3+
from dependency_injector.containers import DeclarativeContainer
4+
from dependency_injector.providers import Callable, Singleton
5+
6+
import example
7+
8+
9+
class Container(DeclarativeContainer):
10+
"""DI container."""
11+
12+
mail_service = Singleton(example.MailService,
13+
host='localhost',
14+
port=587,
15+
login='my_login',
16+
password='super_secret_password')
17+
18+
add_user = Callable(example.add_user,
19+
mailer=mail_service)
20+
21+
22+
if __name__ == '__main__':
23+
print('Using real mail service:')
24+
Container.add_user('sample@mail.com', 'password')
25+
# Using real mail service:
26+
# Connecting server localhost:587 with my_login:super_secret_password
27+
# Sending "Your password is password" to "sample@mail.com"
28+
29+
print('Using mail service stub:')
30+
Container.add_user('sample@mail.com', 'password',
31+
mailer=example.MailServiceStub())
32+
# Using mail service stub:
33+
# Emulating sending "Your password is password" to "sample@mail.com"
34+
35+
# Also you can override provider by another provider:
36+
Container.mail_service.override(Singleton(example.MailServiceStub))
37+
print('Using mail service stub by overriding mail service provider:')
38+
Container.add_user('sample@mail.com', 'password')
39+
# Using mail service stub by overriding mail service provider:
40+
# Emulating sending "Your password is password" to "sample@mail.com"
41+
Container.mail_service.reset_override() # Resetting provider overriding
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Mail service and user registration example."""
2+
3+
4+
class AbstractMailService(object):
5+
"""Abstract mail service."""
6+
7+
def send(self, email, body):
8+
"""Send email."""
9+
raise NotImplementedError()
10+
11+
12+
class MailService(AbstractMailService):
13+
"""Mail service."""
14+
15+
def __init__(self, host, port, login, password):
16+
"""Initializer."""
17+
self._host = host
18+
self._port = port
19+
self._login = login
20+
self._password = password
21+
22+
def send(self, email, body):
23+
"""Send email."""
24+
print('Connecting server {0}:{1} with {2}:{3}'.format(
25+
self._host, self._port, self._login, self._password))
26+
print('Sending "{0}" to "{1}"'.format(body, email))
27+
28+
29+
class MailServiceStub(AbstractMailService):
30+
"""Mail service stub."""
31+
32+
def send(self, email, body):
33+
"""Send email."""
34+
print('Emulating sending "{0}" to "{1}"'.format(body, email))
35+
36+
37+
def add_user(email, password, mailer):
38+
"""Register user."""
39+
mailer.send(email, 'Your password is {0}'.format(password))

0 commit comments

Comments
 (0)