tlx/apigateway/__init__.py
def proxy_response_handler(func=None, running_local=False, quiet=True):
    """ A Decorator for lambda functions. The function to be decorated by have two positional arguments
        (event, context) as any lambda handler would.  Decorating your handler allows you to write idomatic python
        using returns and raising exception.  This handler catches and formats them as proxy response objects
        suitable for APIG.

        This function does the following:
            - prints the received `event` to the debug logs
            - Sets the generic error response
            - Ensures raised exceptions are caught and formatted correctly
            - Unforseen errors are NOT propogated back to the user
            - Forseen exceptions ARE propogated back to the user in the `response.message` field
                (Developers should raise APIGException if they want the message to return)

        The decorated function should return data that can be converted to JSON.  This can be a list, dict, string,
        number or boolean.  It should raise a APIGException if the user wants to return the error message and modify
        the return code.  Otherwise all other Exceptions are returned as 500 response codes without a detailed error
        message.

        Set running_local=True if we're running locally (eg. we're not imported/running on AWS Lambda),
        and Python stack traces will show when exceptions are raised.
        Example usage: @proxy_response_handler(running_local=__name__=="__main__")

        Set quiet=True (default) to suppress all error output including stack traces (eg. for Prod deployments)
    """
    if not func:
        return functools.partial(proxy_response_handler, running_local=running_local, quiet=quiet)

    @functools.wraps(func)
    def wrapper(*axgs):
        # Setup default response
        response = {
            "statusCode": 500,
            "body": {},
        }

        def setup_error_response(msg, code=None):
            logger.error(msg)
            response["body"] = msg
            if code:
                response["statusCode"] = code

        event, context = axgs[0], axgs[1]
        logger.info('event: {}'.format(json_dumps(event)))
        logger.debug("Received '{resource}' request with params: {queryStringParameters} and body: {body}".format(**event))

        try:  # to get successfull execution
            response["body"] = func(event, context)
            response["statusCode"] = 200

        # if not, format appropriately for proxy integration
        except APIGException as e:
            setup_error_response("Error: {e}".format(e=e), e.code)
        except Exception as e:  # Unforseen Exception arose  # pylint: disable=broad-except
            if quiet:
                pass  # Returns generic error response for production deployment
            elif running_local:
                sys.stderr.write("Local run detected, showing Python Exception stack trace:\n")
                raise e
            else:
                setup_error_response("Error: {e}".format(e=e))  # For remote testing

        # Final preparation for http reponse
        response["body"] = json_dumps(response["body"])
        logger.info("Returning repsonse: {response}".format(**locals()))
        return response
    return wrapper
 A Decorator for lambda functions. The function to be decorated by have two positional arguments
        (event, context) as any lambda handler would.  Decorating your handler allows you to write idomatic python
        using returns and raising exception.  This handler catches and formats them as proxy response objects
        suitable for APIG.

        This function does the following:
            - prints the received `event` to the debug logs
            - Sets the generic error response
            - Ensures raised exceptions are caught and formatted correctly
            - Unforseen errors are NOT propogated back to the user
            - Forseen exceptions ARE propogated back to the user in the `response.message` field
                (Developers should raise APIGException if they want the message to return)

        The decorated function should return data that can be converted to JSON.  This can be a list, dict, string,
        number or boolean.  It should raise a APIGException if the user wants to return the error message and modify
        the return code.  Otherwise all other Exceptions are returned as 500 response codes without a detailed error
        message.

        Set running_local=True if we're running locally (eg. we're not imported/running on AWS Lambda),
        and Python stack traces will show when exceptions are raised.
        Example usage: @proxy_response_handler(running_local=__name__=="__main__")

        Set quiet=True (default) to suppress all error output including stack traces (eg. for Prod deployments)
    
A Decorator for lambda functions. The function to be decorated by have two positional arguments
(event, context) as any lambda handler would.  Decorating your handler allows you to write idomatic python
using returns and raising exception.  This handler catches and formats them as proxy response objects
suitable for APIG.

This function does the following:
prints the received `event` to the debug logs
Sets the generic error response
Ensures raised exceptions are caught and formatted correctly
Unforseen errors are NOT propogated back to the user
Forseen exceptions ARE propogated back to the user in the `response.message` field
(Developers should raise APIGException if they want the message to return)

The decorated function should return data that can be converted to JSON.  This can be a list, dict, string,
number or boolean.  It should raise a APIGException if the user wants to return the error message and modify
the return code.  Otherwise all other Exceptions are returned as 500 response codes without a detailed error
message.

Set running_local=True if we're running locally ,
and Python stack traces will show when exceptions are raised.

Set quiet=True (default) to suppress all error output including stack traces
A Decorator for lambda functions . The function to be decorated by have two positional arguments ( event context ) as any lambda handler would . Decorating your handler allows you to write idomatic python using returns and raising exception . This handler catches and formats them as proxy response objects suitable for APIG . This function does the following : prints the received ` event ` to the debug logs Sets the generic error response Ensures raised exceptions are caught and formatted correctly Unforseen errors are NOT propogated back to the user Forseen exceptions ARE propogated back to the user in the ` response . message ` field ( Developers should raise APIGException if they want the message to return ) The decorated function should return data that can be converted to JSON . This can be a list dict string number or boolean . It should raise a APIGException if the user wants to return the error message and modify the return code . Otherwise all other Exceptions are returned as 500 response codes without a detailed error message . Set running_local = True if we ' re running locally and Python stack traces will show when exceptions are raised . Set quiet = True ( default ) to suppress all error output including stack traces
def proxy_response_handler(func=None, running_local=False, quiet=True):
    if not func:
        return functools.partial(proxy_response_handler, running_local=running_local, quiet=quiet)
    @functools.wraps(func)
    def wrapper(*axgs):
        response = {
            "statusCode": 500,
            "body": {},
        }
        def setup_error_response(msg, code=None):
            logger.error(msg)
            response["body"] = msg
            if code:
                response["statusCode"] = code
        event, context = axgs[0], axgs[1]
        logger.info('event: {}'.format(json_dumps(event)))
        logger.debug("Received '{resource}' request with params: {queryStringParameters} and body: {body}".format(**event))
        try:  
            response["body"] = func(event, context)
            response["statusCode"] = 200
        except APIGException as e:
            setup_error_response("Error: {e}".format(e=e), e.code)
        except Exception as e:  
            if quiet:
                pass  
            elif running_local:
                sys.stderr.write("Local run detected, showing Python Exception stack trace:\n")
                raise e
            else:
                setup_error_response("Error: {e}".format(e=e))  
        response["body"] = json_dumps(response["body"])
        logger.info("Returning repsonse: {response}".format(**locals()))
        return response
    return wrapper
def proxy_response_handler ( func = None , running_local = False , quiet = True ) : if not func : return functools . partial ( proxy_response_handler , running_local = running_local , quiet = quiet ) @ functools . wraps ( func ) def wrapper ( * axgs ) : response = { "statusCode" : 500 , "body" : { } , } def setup_error_response ( msg , code = None ) : logger . error ( msg ) response [ "body" ] = msg if code : response [ "statusCode" ] = code event , context = axgs [ 0 ] , axgs [ 1 ] logger . info ( 'event: {}' . format ( json_dumps ( event ) ) ) logger . debug ( "Received '{resource}' request with params: {queryStringParameters} and body: {body}" . format ( ** event ) ) try : response [ "body" ] = func ( event , context ) response [ "statusCode" ] = 200 except APIGException as e : setup_error_response ( "Error: {e}" . format ( e = e ) , e . code ) except Exception as e : if quiet : pass elif running_local : sys . stderr . write ( "Local run detected, showing Python Exception stack trace:\n" ) raise e else : setup_error_response ( "Error: {e}" . format ( e = e ) ) response [ "body" ] = json_dumps ( response [ "body" ] ) logger . info ( "Returning repsonse: {response}" . format ( ** locals ( ) ) ) return response return wrapper
A Decorator for lambda functions.
A Decorator for lambda functions .
""" A Decorator for lambda functions. The function to be decorated by have two positional arguments
        (event, context) as any lambda handler would.  Decorating your handler allows you to write idomatic python
        using returns and raising exception.  This handler catches and formats them as proxy response objects
        suitable for APIG.

        This function does the following:
            - prints the received `event` to the debug logs
            - Sets the generic error response
            - Ensures raised exceptions are caught and formatted correctly
            - Unforseen errors are NOT propogated back to the user
            - Forseen exceptions ARE propogated back to the user in the `response.message` field
                (Developers should raise APIGException if they want the message to return)

        The decorated function should return data that can be converted to JSON.  This can be a list, dict, string,
        number or boolean.  It should raise a APIGException if the user wants to return the error message and modify
        the return code.  Otherwise all other Exceptions are returned as 500 response codes without a detailed error
        message.

        Set running_local=True if we're running locally (eg. we're not imported/running on AWS Lambda),
        and Python stack traces will show when exceptions are raised.
        Example usage: @proxy_response_handler(running_local=__name__=="__main__")

        Set quiet=True (default) to suppress all error output including stack traces (eg. for Prod deployments)
    """ # Setup default response # to get successfull execution # if not, format appropriately for proxy integration # Unforseen Exception arose  # pylint: disable=broad-except # Returns generic error response for production deployment # For remote testing # Final preparation for http reponse
func
running_local
quiet