Cache FlaskInternalApi object on request keyed by authentication token.

This commit is contained in:
2016-10-13 10:01:29 +02:00
parent 10ecb2158e
commit aa953f76a1

View File

@@ -35,24 +35,29 @@ def pillar_server_endpoint_static():
def pillar_api(token=None): def pillar_api(token=None):
# Cache API objects on the request. # Cache API objects on the request per token.
api = getattr(request, 'pillar_api', None) api = getattr(request, 'pillar_api', {}).get(token)
if api is not None: if api is not None:
return api return api
# Check if current_user is initialized (in order to support manage.py # Check if current_user is initialized (in order to support manage.py
# scripts and non authenticated server requests). # scripts and non authenticated server requests).
use_token = token
if token is None and current_user and current_user.is_authenticated: if token is None and current_user and current_user.is_authenticated:
token = current_user.id use_token = current_user.id
api = FlaskInternalApi( api = FlaskInternalApi(
endpoint=pillar_server_endpoint(), endpoint=pillar_server_endpoint(),
username=None, username=None,
password=None, password=None,
token=token token=use_token
) )
request.pillar_api = api if token is None:
if not hasattr(request, 'pillar_api'):
request.pillar_api = {}
request.pillar_api[token] = api
return api return api