8000 Add services mini app example to docs · aybb/python-dependency-injector@079d3f5 · GitHub
[go: up one dir, main page]

Skip to content

Commit 079d3f5

Browse files
committed
Add services mini app example to docs
1 parent 31f2a5d commit 079d3f5

File tree

8 files changed

+168
-19
lines changed

8 files changed

+168
-19
lines changed

docs/api/utils.rst

Lines changed: 0 additions & 5 deletions
This file was deleted.

docs/examples/index.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ and powered by *Dependency Injector* framework.
1616
:maxdepth: 2
1717

1818
movie_lister
19+
services_miniapp

docs/examples/services_miniapp.rst

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
Services mini application example
2+
---------------------------------
3+
4+
.. meta::
5+
:description: "Services miniapp" is an example mini application that
6+
consists from several services that have dependencies on
7+
some standard and 3rd-party libraries for logging,
8+
interaction with database and remote service via API.
9+
"Services miniapp" example demonstrates usage of
10+
Dependency Injector for creating several IoC containers.
11+
12+
"Services miniapp" is an example mini application that consists from several
13+
services that have dependencies on some standard and 3rd-party libraries for
14+
logging, interaction with database and remote service calls via API.
15+
16+
"Services miniapp" example demonstrates usage of
17+
:doc:`Dependency Injector <../index>` for creating several IoC containers.
18+
19+
Instructions for running:
20+
21+
.. code-block:: bash
22+
23+
python run.py 1 secret photo.jpg
24+
25+
Example application
26+
~~~~~~~~~~~~~~~~~~~
27+
28+
Classes diagram:
29+
30+
.. image:: /images/miniapps/services/classes.png
31+
:width: 100%
32+
:align: center
33+
34+
35+
Example application structure:
36+
37+
.. code-block:: bash
38+
39+
/example
40+
/__init__.py
41+
/main.py
42+
/services.py
43+
44+
45+
Listing of ``example/services.py``:
46+
47+
.. literalinclude:: ../../examples/miniapps/services/example/services.py
48+
:language: python
49+
:linenos:
50+
51+
Listing of ``example/main.py``:
52+
53+
.. literalinclude:: ../../examples/miniapps/services/example/main.py
54+
:language: python
55+
:linenos:
56+
57+
IoC containers
58+
~~~~~~~~~~~~~~
59+
60+
Listing of ``containers.py``:
61+
62+
.. literalinclude:: ../../examples/miniapps/services/containers.py
63+
:language: python
64+
:linenos:
65+
66+
Run application
67+
~~~~~~~~~~~~~~~
68+
69+
Listing of ``run.py``:
70+
71+
.. literalinclude:: ../../examples/miniapps/services/run.py
72+
:language: python
73+
:linenos:
48.8 KB
Loading

docs/main/changelog.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,11 @@ Development version
1111
-------------------
1212
- No features.
1313

14+
3.1.0
15+
-----
16+
- Add "Services mini application" example.
17+
- Fix minor error in ``Factory`` provider API doc.
18+
1419
3.0.1
1520
-----
1621
- Add ``*.c`` source files under version control.

examples/miniapps/services/example/main.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,26 @@
22

33

44
def main(uid, password, photo, users_service, auth_service, photos_service):
5-
"""Example main function."""
5+
"""Authenticate user and upload photo.
6+
7+
:param uid: User identifier.
8+
:type uid: int
9+
10+
:param password: User's password for verification.
11+
:type password: str
12+
13+
:param photo_path: Path to photo for uploading.
14+
:type photo_path: str
15+
16+
:param users_service: Users service.
17+
:type users_service: example.services.UsersService
18+
19+
:param auth_service: Authentication service.
20+
:type auth_service: example.services.AuthService
21+
22+
:param photo_service: Photo service.
23+
:type photo_service: example.services.PhotoService
24+
"""
625
user = users_service.get_user_by_id(uid)
726
auth_service.authenticate(user, password)
827
photos_service.upload_photo(user['uid'], photo)
Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,103 @@
11
"""Example business services module."""
22

33

4-
class UsersService(object):
4+
class BaseService(object):
5+
"""Service base class."""
6+
7+
8+
class UsersService(BaseService):
59
"""Users service."""
610

711
def __init__(self, logger, db):
8-
"""Initializer."""
12+
"""Initializer.
13+
14+
:param logger: Logger instance.
15+
:type logger: logging.Logger
16+
17+
:param db: Database connection.
18+
:type db: sqlite3.Connection
19+
"""
920
self.logger = logger
1021
self.db = db
1122

1223
def get_user_by_id(self, uid):
13-
"""Return user's information by login."""
24+
"""Return user's data by identifier.
25+
26+
:param uid: User identifier.
27+
:type uid: int
28+
29+
:rtype: dict
30+
"""
1431
self.logger.debug('User %s has been found in database', uid)
15-
return {'uid': uid,
16-
'password_hash': 'secret_hash'}
32+
return dict(uid=uid, password_hash='secret_hash')
1733

1834

19-
class AuthService(object):
20-
"""Auth service."""
35+
class AuthService(BaseService):
36+
"""Authentication service."""
2137

2238
def __init__(self, logger, db, token_ttl):
23-
"""Initializer."""
39+
"""Initializer.
40+
41+
:param logger: Logger instance.
42+
:type logger: logging.Logger
43+
44+
:param db: Database connection.
45+
:type db: sqlite3.Connection
46+
47+
:param token_ttl: Token lifetime in seconds.
48+
:type token_ttl: int
49+
"""
2450
self.logger = logger
2551
self.db = db
2652
self.token_ttl = token_ttl
2753

2854
def authenticate(self, user, password):
29-
"""Authenticate user."""
55+
"""Authenticate user.
56+
57+
:param user: User's data.
58+
:type user: dict
59+
60+
:param password: User's password for verification.
61+
:type password: str
62+
63+
:raises: AssertionError when password is wrong
64+
65+
:rtype: None
66+
"""
3067
assert user['password_hash'] == '_'.join((password, 'hash'))
3168
self.logger.debug('User %s has been successfully authenticated',
3269
user['uid'])
3370

3471

35-
class PhotosService(object):
72+
class PhotosService(BaseService):
3673
"""Photos service."""
3774

3875
def __init__(self, logger, db, s3):
39-
"""Initializer."""
76+
"""Initializer.
77+
78+
:param logger: Logger instance.
79+
:type logger: logging.Logger
80+
81+
:param db: Database connection.
82+
:type db: sqlite3.Connection
83+
84+
:param s3: AWS S3 client.
85+
:type s3: boto.s3.connection.S3Connection
86+
"""
4087
self.logger = logger
4188
self.db = db
4289
self.s3 = s3
4390

4491
def upload_photo(self, uid, photo_path):
45-
"""Upload user photo."""
92+
"""Upload user photo.
93+
94+
:param uid: User identifier.
95+
:type uid: int
96+
97+
:param photo_path: Path to photo for uploading.
98+
:type photo_path: str
99+
100+
:rtpe: None
101+
"""
46102
self.logger.debug('Photo %s has been successfully uploaded by user %s',
47103
photo_path, uid)

src/dependency_injector/__init__.py

6745
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"""Dependency injector top-level package."""
22

3-
VERSION = '3.0.1'
3+
VERSION = '3.1.0'
44
"""Version number that follows semantic versioning.
55
66
:type: str

0 commit comments

Comments
 (0)
0