"""
    Django views file. Responsible of route URL requested to the destination
    endpoint.
"""
from django.views.decorators.csrf import csrf_exempt

from tools import const
from tools.exceptions import SystemException
from tools.service.service import Service
from django.http import QueryDict
from .calls import CALLS


MethodNotAvailable = type('MethodNotAvailable', (SystemException, ), {})


@csrf_exempt
def wrapper(request, service_str, **kwargs):
    """
        Method that wrap the url path to the function to launch.
    Parameters
    ----------
    request
    service_str
    kwargs

    Returns
    -------

    """
    http_method = request.method
    service = CALLS[service_str]
    service_dict = service.__dict__.copy()
    if "{}_REQUEST".format(http_method.upper()) not in service_dict.keys():
        return MethodNotAvailable('METHOD {} not available'.format(http_method))
    service_conf = service_dict["{}_REQUEST".format(http_method.upper())]
    del service_dict
    # Setting parser for this app #########################
    # service_conf['parser'] = const.PARSER_NONE
    # #####################################################
    service = Service(service_conf)
    service_check = service.check()
    if service_check.__class__ == Service.SERVICE_EXCEPTION:
        return service_check
    return service.run(request, **kwargs)