Using parser functionalities jointly with APIGatewayRestResolver #2288
-
``Hi folks, I'm trying to use the The goal is to ensure that the body of the REST POST API looks like the one in the following example (see also class
What I tried to do is the following: # appropriate imports here
logger = Logger(service="APP")
app = APIGatewayRestResolver()
class ParallelizationBody(BaseModel):
function_name: str
input_array: List[Any]
calculation_settings: Optional[Dict]
class ParallelizationPayload(APIGatewayProxyEventV2Model):
body: ParallelizationBody
# other stuff being done here
@logger.inject_lambda_context(
correlation_id_path=correlation_paths.API_GATEWAY_REST, log_event=True
)
@event_parser(model=ParallelizationPayload)
def handler(event: ParallelizationPayload, context: LambdaContext):
"""
Lambda entry point - currently supporting only:
+ one POST method for submitting calculations
"""
return app.resolve(event, context) I also tried using
To reiterate the question on top: is there any blueprint or boilerplate that I might re-use? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 4 replies
-
Updates:
As for the first 2 I believe there is a bug in class Hence, the current version: headers: Dict[str, str]
multiValueHeaders: Dict[str, List[str]] should be changed to headers: Optional[Dict[str, str]]
multiValueHeaders: Optional[Dict[str, List[str]]] As for the third one, I am still a bit puzzled by the meaning of the error and I should dive a bit further. |
Beta Was this translation helpful? Give feedback.
-
Hi @gomboc1985! I'll look into it tomorrow and get back to you. Thank you |
Beta Was this translation helpful? Give feedback.
-
Hi @gomboc1985, thanks for bringing this discussion on board. I'm going to split this answer into 3 parts for easier explanation. 1 - Documentation of APIGatewayProxyEventModel 2 - multiValueHeader and Headers fields 3 - The body value is not a valid dict I created a code and you can see these things working together. import json
from typing import Dict, Optional, List, Any
from aws_lambda_powertools import Logger, Tracer
from aws_lambda_powertools.event_handler import APIGatewayRestResolver
from aws_lambda_powertools.logging import correlation_paths
from aws_lambda_powertools.utilities.typing import LambdaContext
from aws_lambda_powertools.utilities.parser.models import APIGatewayProxyEventModel
from aws_lambda_powertools.utilities.parser import BaseModel, parse, validator
tracer = Tracer()
logger = Logger()
app = APIGatewayRestResolver(debug=True)
class ParallelizationBody(BaseModel):
function_name: str
input_array: List[Any]
calculation_settings: Optional[Dict]
class ParallelizationPayload(APIGatewayProxyEventModel):
body: ParallelizationBody
@validator("body", pre=True, allow_reuse=True)
def body_str_to_dict(cls, value):
return json.loads(value)
@app.post("/hello")
@tracer.capture_method
def post_todos():
print(app.current_event.body)
return {"hello": "word"}
@logger.inject_lambda_context(correlation_id_path=correlation_paths.API_GATEWAY_REST)
@tracer.capture_lambda_handler
def lambda_handler(event: ParallelizationPayload, context: LambdaContext) -> dict:
# Use standalone parse function instead the event_parser because the decorator event_parser can't deserialize the custom class ParallelizationPayload
# See https://awslabs.github.io/aws-lambda-powertools-python/2.5.0/utilities/parser/#parse-function
parse_event: ParallelizationPayload = parse(event=event, model=ParallelizationPayload)
logger.info(parse_event)
return app.resolve(event, context) API Call Please let me know if you need any other help and we can make it work together. Thank you |
Beta Was this translation helpful? Give feedback.
Hi @gomboc1985, thanks for bringing this discussion on board. I'm going to split this answer into 3 parts for easier explanation.
1 - Documentation of APIGatewayProxyEventModel
Yes, I agree with you that we have room to improve our documentation and make it explicit that this template only works with v1.
2 - multiValueHeader and Headers fields
The
APIGatewayProxyEventModel
Model is intended to work with RestAPI and expects to receive an event from API Gateway. Even if you don't add any header on the client side (curl/postman/others) the API Gateway will inject headers automatically and pass them to Lambda since its using a PROXY integration. So, in this case, and according to AWS Document…