aws_lambda_decorators/decorators.py
def extract(parameters, group_errors=False, allow_none_defaults=False):
    """
    Extracts a set of parameters from any function parameter passed to an AWS lambda handler.

    The extracted parameters are added as kwargs to the handler function.

    Usage:
        @extract([Parameter(path="headers/Authorization[jwt]/sub", var_name="user_id", func_param_name="event")])
        def lambda_handler(event, context, user_id=None)
            pass

    Args:
        parameters (list): A collection of Parameter type items.
        group_errors (bool): flag that indicates if error messages are to be grouped together
            (if set to False, validation will end on first error)
        allow_none_defaults: A flag to allow None defaults. If True, None defaults will be passed into the kwargs.
            If the flag is set to False, the None defaults will not be added to kwargs, and the default will be
            picked up (if exists) from the method signature.
    """
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                param = None
                errors = []
                arg_dictionary = all_func_args(func, args, kwargs)
                for param in parameters:
                    param_val = arg_dictionary[param.func_param_name]
                    return_val = param.extract_value(param_val)
                    param_errors = param.validate_path(return_val, group_errors)
                    if param_errors:
                        errors.append(param_errors)
                        if not group_errors:
                            LOGGER.error(VALIDATE_ERROR_MESSAGE, errors)
                            return failure(errors)
                    elif allow_none_defaults or return_val is not None:
                        kwargs[param.get_var_name()] = return_val
            except Exception as ex:  # noqa: pylint - broad-except
                LOGGER.error(EXCEPTION_LOG_MESSAGE, full_name(ex), str(ex),
                             param.func_param_name if param else UNKNOWN,
                             param.path if param else UNKNOWN)
                return failure(ERROR_MESSAGE)
            else:
                if group_errors and errors:
                    LOGGER.error(VALIDATE_ERROR_MESSAGE, errors)
                    return failure(errors)

                return func(*args, **kwargs)
        return wrapper
    return decorator

    Extracts a set of parameters from any function parameter passed to an AWS lambda handler.

    The extracted parameters are added as kwargs to the handler function.

    Usage:
        @extract([Parameter(path="headers/Authorization[jwt]/sub", var_name="user_id", func_param_name="event")])
        def lambda_handler(event, context, user_id=None)
            pass

    Args:
        parameters (list): A collection of Parameter type items.
        group_errors (bool): flag that indicates if error messages are to be grouped together
            (if set to False, validation will end on first error)
        allow_none_defaults: A flag to allow None defaults. If True, None defaults will be passed into the kwargs.
            If the flag is set to False, the None defaults will not be added to kwargs, and the default will be
            picked up (if exists) from the method signature.
    
Extracts a set of parameters from any function parameter passed to an AWS lambda handler.
The extracted parameters are added as kwargs to the handler function.


Extracts a set of parameters from any function parameter passed to an AWS lambda handler . The extracted parameters are added as kwargs to the handler function .
def extract(parameters, group_errors=False, allow_none_defaults=False):
    def decorator(func):
        def wrapper(*args, **kwargs):
            try:
                param = None
                errors = []
                arg_dictionary = all_func_args(func, args, kwargs)
                for param in parameters:
                    param_val = arg_dictionary[param.func_param_name]
                    return_val = param.extract_value(param_val)
                    param_errors = param.validate_path(return_val, group_errors)
                    if param_errors:
                        errors.append(param_errors)
                        if not group_errors:
                            LOGGER.error(VALIDATE_ERROR_MESSAGE, errors)
                            return failure(errors)
                    elif allow_none_defaults or return_val is not None:
                        kwargs[param.get_var_name()] = return_val
            except Exception as ex:  
                LOGGER.error(EXCEPTION_LOG_MESSAGE, full_name(ex), str(ex),
                             param.func_param_name if param else UNKNOWN,
                             param.path if param else UNKNOWN)
                return failure(ERROR_MESSAGE)
            else:
                if group_errors and errors:
                    LOGGER.error(VALIDATE_ERROR_MESSAGE, errors)
                    return failure(errors)
                return func(*args, **kwargs)
        return wrapper
    return decorator
def extract ( parameters , group_errors = False , allow_none_defaults = False ) : def decorator ( func ) : def wrapper ( * args , ** kwargs ) : try : param = None errors = [ ] arg_dictionary = all_func_args ( func , args , kwargs ) for param in parameters : param_val = arg_dictionary [ param . func_param_name ] return_val = param . extract_value ( param_val ) param_errors = param . validate_path ( return_val , group_errors ) if param_errors : errors . append ( param_errors ) if not group_errors : LOGGER . error ( VALIDATE_ERROR_MESSAGE , errors ) return failure ( errors ) elif allow_none_defaults or return_val is not None : kwargs [ param . get_var_name ( ) ] = return_val except Exception as ex : LOGGER . error ( EXCEPTION_LOG_MESSAGE , full_name ( ex ) , str ( ex ) , param . func_param_name if param else UNKNOWN , param . path if param else UNKNOWN ) return failure ( ERROR_MESSAGE ) else : if group_errors and errors : LOGGER . error ( VALIDATE_ERROR_MESSAGE , errors ) return failure ( errors ) return func ( * args , ** kwargs ) return wrapper return decorator
Extracts a set of parameters from any function parameter passed to an AWS lambda handler.
Extracts a set of parameters from any function parameter passed to an AWS lambda handler .
"""
    Extracts a set of parameters from any function parameter passed to an AWS lambda handler.

    The extracted parameters are added as kwargs to the handler function.

    Usage:
        @extract([Parameter(path="headers/Authorization[jwt]/sub", var_name="user_id", func_param_name="event")])
        def lambda_handler(event, context, user_id=None)
            pass

    Args:
        parameters (list): A collection of Parameter type items.
        group_errors (bool): flag that indicates if error messages are to be grouped together
            (if set to False, validation will end on first error)
        allow_none_defaults: A flag to allow None defaults. If True, None defaults will be passed into the kwargs.
            If the flag is set to False, the None defaults will not be added to kwargs, and the default will be
            picked up (if exists) from the method signature.
    """ # noqa: pylint - broad-except
parameters
A collection of Parameter type items.
A collection of Parameter type items .
group_errors
flag that indicates if error messages are to be grouped together
(if set to False, validation will end on first error)
flag that indicates if error messages are to be grouped together ( if set to False validation will end on first error )
allow_none_defaults
A flag to allow None defaults. If True, None defaults will be passed into the kwargs.
If the flag is set to False, the None defaults will not be added to kwargs, and the default will be
picked up (if exists) from the method signature.
A flag to allow None defaults . If True None defaults will be passed into the kwargs . If the flag is set to False the None defaults will not be added to kwargs and the default will be picked up ( if exists ) from the method signature .