From 52f2eedd6aa7a3d0b0b997a06b095b1ec19b4031 Mon Sep 17 00:00:00 2001 From: Roman Mogilatov Date: Fri, 26 Feb 2016 13:36:19 +0200 Subject: [PATCH] Add auth system example --- examples/auth_system.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 examples/auth_system.py diff --git a/examples/auth_system.py b/examples/auth_system.py new file mode 100644 index 00000000..d7cad423 --- /dev/null +++ b/examples/auth_system.py @@ -0,0 +1,40 @@ +"""Pythonic way for Dependency Injection.""" + +from dependency_injector import providers +from dependency_injector import injections + + +@providers.DelegatedCallable +def get_user_info(user_id): + """Return user info.""" + raise NotImplementedError() + + +@providers.Factory +@injections.inject(get_user_info=get_user_info) +class AuthComponent(object): + """Some authentication component.""" + + def __init__(self, get_user_info): + """Initializer.""" + self.get_user_info = get_user_info + + def authenticate_user(self, token): + """Authenticate user by token.""" + user_info = self.get_user_info(user_id=token + '1') + return user_info + + +print AuthComponent +print get_user_info + + +@get_user_info.override +@providers.DelegatedCallable +def get_user_info(user_id): + """Return user info.""" + return {'user_id': user_id} + + +print AuthComponent().authenticate_user(token='abc') +# {'user_id': 'abc1'}