'An AWS Lambda function which parses specific API Gateway input into a
WSGI request, feeds it to our WSGI app, procceses the response, and returns
that back to the API Gateway.'

def handler(self, event, context):

    settings = self.settings
    if settings.DEBUG:
        logger.debug(u'Zappa    Event:    {}'.format(event))
    if (event.get(u'detail-type') == u'Scheduled    Event'):
        whole_function = event[u'resources'][0].split(u'/')[(-1)].split(u'-')[(-1)]
        if (u'.' in whole_function):
            app_function = self.import_module_and_get_function(whole_function)
            return self.run_function(app_function, event, context)
    elif event.get(u'command', None):
        whole_function = event[u'command']
        app_function = self.import_module_and_get_function(whole_function)
        result = self.run_function(app_function, event, context)
        print (u'Result    of    %s:' % whole_function)
        print result
        return result
    elif event.get(u'raw_command', None):
        raw_command = event[u'raw_command']
        exec raw_command
        return
    elif event.get(u'manage', None):
        from django.core import management
        try:
            from zappa.ext.django_zappa import get_django_wsgi
        except ImportError as e:
            from django_zappa_app import get_django_wsgi
        app_function = get_django_wsgi(self.settings.DJANGO_SETTINGS)
        management.call_command(*event[u'manage'].split(u'    '))
        return {}
    elif event.get(u'Records', None):
        records = event.get(u'Records')
        result = None
        whole_function = self.get_function_for_aws_event(records[0])
        if whole_function:
            app_function = self.import_module_and_get_function(whole_function)
            result = self.run_function(app_function, event, context)
            logger.debug(result)
        else:
            logger.error(u'Cannot    find    a    function    to    process    the    triggered    event.')
        return result
    elif (event.get(u'type') == u'TOKEN'):
        whole_function = self.settings.AUTHORIZER_FUNCTION
        if whole_function:
            app_function = self.import_module_and_get_function(whole_function)
            policy = self.run_function(app_function, event, context)
            return policy
        else:
            logger.error(u'Cannot    find    a    function    to    process    the    authorization    request.')
            raise Exception(u'Unauthorized')
    try:
        time_start = datetime.datetime.now()
        if event.get(u'httpMethod', None):
            script_name = u''
            headers = event.get(u'headers')
            if headers:
                host = headers.get(u'Host')
            else:
                host = None
            if host:
                if (u'amazonaws.com' in host):
                    script_name = (u'/' + settings.API_STAGE)
            elif settings.DOMAIN:
                pass
            else:
                script_name = (u'/' + settings.API_STAGE)
            environ = create_wsgi_request(event, script_name=script_name, trailing_slash=self.trailing_slash, binary_support=settings.BINARY_SUPPORT, context_header_mappings=settings.CONTEXT_HEADER_MAPPINGS)
            environ[u'HTTPS'] = u'on'
            environ[u'wsgi.url_scheme'] = u'https'
            environ[u'lambda.context'] = context
            response = Response.from_app(self.wsgi_app, environ)
            zappa_returndict = dict()
            if response.data:
                if settings.BINARY_SUPPORT:
                    if ((not response.mimetype.startswith(u'text/')) or (response.mimetype != u'application/json')):
                        zappa_returndict[u'body'] = base64.b64encode(response.data).decode(u'utf-8')
                        zappa_returndict[u'isBase64Encoded'] = u'true'
                    else:
                        zappa_returndict[u'body'] = response.data
                else:
                    zappa_returndict[u'body'] = response.get_data(as_text=True)
            zappa_returndict[u'statusCode'] = response.status_code
            zappa_returndict[u'headers'] = {}
            for (key, value) in response.headers:
                zappa_returndict[u'headers'][key] = value
            time_end = datetime.datetime.now()
            delta = (time_end - time_start)
            response_time_ms = (delta.total_seconds() * 1000)
            response.content = response.data
            common_log(environ, response, response_time=response_time_ms)
            return zappa_returndict
    except Exception as e:
        print e
        exc_info = sys.exc_info()
        message = u'An    uncaught    exception    happened    while    servicing    this    request.    You    can    investigate    this    with    the    `zappa    tail`    command.'
        if (not settings.DJANGO_SETTINGS):
            try:
                self.app_module
            except NameError as ne:
                message = u'Failed    to    import    module:    {}'.format(ne.message)
        content = collections.OrderedDict()
        content[u'statusCode'] = 500
        body = {u'message': message}
        if settings.DEBUG:
            body[u'traceback'] = traceback.format_exception(*exc_info)
        content[u'body'] = json.dumps(str(body), sort_keys=True, indent=4)
        return content
