|
| 1 | +import logging |
| 2 | +from typing import Any, Callable, Dict, Optional |
| 3 | + |
| 4 | +from pydantic import BaseModel, ValidationError |
| 5 | + |
| 6 | +from aws_lambda_powertools.middleware_factory import lambda_handler_decorator |
| 7 | +from aws_lambda_powertools.utilities.advanced_parser.envelopes import Envelope, parse_envelope |
| 8 | + |
| 9 | +logger = logging.getLogger(__name__) |
| 10 | + |
| 11 | + |
| 12 | +@lambda_handler_decorator |
| 13 | +def parser( |
| 14 | + handler: Callable[[Dict, Any], Any], |
| 15 | + event: Dict[str, Any], |
| 16 | + context: Dict[str, Any], |
| 17 | + schema: BaseModel, |
| 18 | + envelope: Optional[Envelope] = None, |
| 19 | +) -> Any: |
| 20 | + """Decorator to conduct advanced parsing & validation for lambda handlers events |
| 21 | +
|
| 22 | + As Lambda follows (event, context) signature we can remove some of the boilerplate |
| 23 | + and also capture any exception any Lambda function throws as metadata. |
| 24 | + event will be the parsed and passed as a BaseModel pydantic class of the input type "schema" |
| 25 | + to the lambda handler. |
| 26 | + event will be extracted from the envelope in case envelope is not None. |
| 27 | + In case envelope is None, the complete event is parsed to match the schema parameter BaseModel definition. |
| 28 | + In case envelope is not None, first the event is parsed as the envelope's schema definition, and the user |
| 29 | + message is extracted and parsed again as the schema parameter's definition. |
| 30 | +
|
| 31 | + Example |
| 32 | + ------- |
| 33 | + **Lambda function using validation decorator** |
| 34 | +
|
| 35 | + @parser(schema=MyBusiness, envelope=envelopes.EVENTBRIDGE) |
| 36 | + def handler(event: MyBusiness , context: LambdaContext): |
| 37 | + ... |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + handler: input for lambda_handler_decorator, wraps the handler lambda |
| 42 | + event: AWS event dictionary |
| 43 | + context: AWS lambda context |
| 44 | + schema: pydantic BaseModel class. This is the user data schema that will replace the event. |
| 45 | + event parameter will be parsed and a new schema object will be created from it. |
| 46 | + envelope: what envelope to extract the schema from, can be any AWS service that is currently |
| 47 | + supported in the envelopes module. Can be None. |
| 48 | +
|
| 49 | + Raises |
| 50 | + ------ |
| 51 | + err |
| 52 | + TypeError - in case event is None |
| 53 | + pydantic.ValidationError - event fails validation, either of the envelope |
| 54 | + """ |
| 55 | + lambda_handler_name = handler.__name__ |
| 56 | + parsed_event = None |
| 57 | + if envelope is None: |
| 58 | + try: |
| 59 | + logger.debug("Parsing and validating event schema, no envelope is used") |
| 60 | + parsed_event = schema(**event) |
| 61 | + except (ValidationError, TypeError): |
| 62 | + logger.exception("Validation exception received from input event") |
| 63 | + raise |
| 64 | + else: |
| 65 | + parsed_event = parse_envelope(event, envelope, schema) |
| 66 | + |
| 67 | + logger.debug(f"Calling handler {lambda_handler_name}") |
| 68 | + return handler(parsed_event, context) |
0 commit comments