python-dependency-injector/examples/misc/auth_system.py

41 lines
954 B
Python
Raw Normal View History

2016-03-14 01:04:55 +03:00
"""Pythonic way for Dependency Injection - Auth System."""
2016-02-26 14:36:19 +03:00
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
2016-03-01 17:44:13 +03:00
@providers.override(get_user_info)
2016-02-26 14:36:19 +03:00
@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'}