python-dependency-injector/examples/miniapps/services_v2/container.py

58 lines
1.3 KiB
Python
Raw Normal View History

2018-06-22 09:52:58 +03:00
"""Example of dependency injection in Python."""
import logging
import sqlite3
import boto3
from dependency_injector import containers, providers
from example import services, main
2018-06-22 09:52:58 +03:00
class IocContainer(containers.DeclarativeContainer):
"""Application IoC container."""
config = providers.Configuration('config')
logger = providers.Singleton(logging.Logger, name='example')
# Gateways
database_client = providers.Singleton(sqlite3.connect, config.database.dsn)
s3_client = providers.Singleton(
boto3.client, 's3',
aws_access_key_id=config.aws.access_key_id,
aws_secret_access_key=config.aws.secret_access_key,
)
2018-06-22 09:52:58 +03:00
# Services
users_service = providers.Factory(
services.UsersService,
2018-06-22 09:52:58 +03:00
db=database_client,
logger=logger,
)
2018-06-22 09:52:58 +03:00
auth_service = providers.Factory(
services.AuthService,
2018-06-22 09:52:58 +03:00
token_ttl=config.auth.token_ttl,
db=database_client,
logger=logger,
)
2018-06-22 09:52:58 +03:00
photos_service = providers.Factory(
services.PhotosService,
2018-06-22 09:52:58 +03:00
db=database_client,
s3=s3_client,
logger=logger,
)
2018-06-22 09:52:58 +03:00
# Misc
main = providers.Callable(
main.main,
2018-06-22 09:52:58 +03:00
users_service=users_service,
auth_service=auth_service,
photos_service=photos_service,
)