Refactor single container example to use wiring

This commit is contained in:
Roman Mogylatov 2020-10-06 15:34:20 -04:00
parent e2f4ab85d5
commit fdb33ca5ce
2 changed files with 19 additions and 13 deletions

View File

@ -24,6 +24,6 @@ You should see:
.. code-block:: bash .. code-block:: bash
[2020-09-04 15:27:27,727] [DEBUG] [example.services.UserService]: User user@example.com has been found in database [2020-10-06 15:32:33,195] [DEBUG] [example.services.UserService]: User user@example.com has been found in database
[2020-09-04 15:27:27,727] [DEBUG] [example.services.AuthService]: User user@example.com has been successfully authenticated [2020-10-06 15:32:33,195] [DEBUG] [example.services.AuthService]: User user@example.com has been successfully authenticated
[2020-09-04 15:27:27,727] [DEBUG] [example.services.PhotoService]: Photo photo.jpg has been successfully uploaded by user user@example.com [2020-10-06 15:32:33,195] [DEBUG] [example.services.PhotoService]: Photo photo.jpg has been successfully uploaded by user user@example.com

View File

@ -2,23 +2,29 @@
import sys import sys
from dependency_injector.wiring import Provide
from .services import UserService, AuthService, PhotoService
from .containers import Container from .containers import Container
def main(email: str, password: str, photo: str) -> None: def main(
container = Container() email: str,
password: str,
container.configure_logging() photo: str,
container.config.from_ini('config.ini') user_service: UserService = Provide[Container.user_service],
auth_service: AuthService = Provide[Container.auth_service],
user_service = container.user_service() photo_service: PhotoService = Provide[Container.photo_service],
auth_service = container.auth_service() ):
photo_service = container.photo_service()
user = user_service.get_user(email) user = user_service.get_user(email)
auth_service.authenticate(user, password) auth_service.authenticate(user, password)
photo_service.upload_photo(user, photo) photo_service.upload_photo(user, photo)
if __name__ == '__main__': if __name__ == '__main__':
container = Container()
container.configure_logging()
container.config.from_ini('config.ini')
container.wire(modules=[sys.modules[__name__]])
main(*sys.argv[1:]) main(*sys.argv[1:])