2021-01-16 16:53:40 +03:00
|
|
|
"""`Configuration` provider strict mode example."""
|
|
|
|
|
|
|
|
from dependency_injector import containers, providers, errors
|
|
|
|
|
|
|
|
|
|
|
|
class ApiClient:
|
|
|
|
def __init__(self, api_key: str, timeout: int):
|
|
|
|
self.api_key = api_key
|
|
|
|
self.timeout = timeout
|
|
|
|
|
|
|
|
|
|
|
|
class Container(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
config = providers.Configuration(strict=True)
|
|
|
|
|
|
|
|
api_client_factory = providers.Factory(
|
|
|
|
ApiClient,
|
|
|
|
api_key=config.api.key,
|
|
|
|
timeout=config.api.timeout.as_int(),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-30 22:32:21 +03:00
|
|
|
if __name__ == "__main__":
|
2021-01-16 16:53:40 +03:00
|
|
|
container = Container()
|
|
|
|
|
|
|
|
try:
|
|
|
|
api_client = container.api_client_factory()
|
|
|
|
except errors.Error:
|
|
|
|
# raises error: Undefined configuration option "config.api.key"
|
|
|
|
...
|