blob: e82480d4c85fbb1405a62e2c4d88a921d70112bf [file] [log] [blame]
import logging
import thrift
from django.shortcuts import render
from . import utils
logger = logging.getLogger(__name__)
class AiravataClientMiddleware:
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
# If user is logged in create an airavata api client for the request
if request.user.is_authenticated:
request.airavata_client = utils.airavata_api_client_pool
response = self.get_response(request)
else:
response = self.get_response(request)
return response
def process_exception(self, request, exception):
if isinstance(exception,
thrift.transport.TTransport.TTransportException):
return render(
request,
'django_airavata/error_page.html',
status=500,
context={
'title': 'Airavata is down',
'text': """The Airavata API server is not reachable. Please try again."""})
else:
return None
def sharing_client(get_response):
"Open and close Sharing registry client for each request"
def middleware(request):
# If user is logged in create a sharing registry client for the request
if request.user.is_authenticated:
request.sharing_client = utils.sharing_api_client_pool
response = get_response(request)
else:
response = get_response(request)
return response
return middleware
def profile_service_client(get_response):
"""Open and close Profile Service client for each request.
Usage:
request.profile_service['group_manager'].getGroup(
request.authz_token, groupId)
"""
def middleware(request):
# If user is logged in create an profile service client for the request
if request.user.is_authenticated:
request.profile_service = {
'group_manager': utils.group_manager_client_pool,
'iam_admin': utils.iamadmin_client_pool,
'tenant_profile': utils.tenant_profile_client_pool,
'user_profile': utils.user_profile_client_pool,
}
response = get_response(request)
else:
response = get_response(request)
return response
return middleware