API Reference¶
route¶
- class restful_aws_lambda.route(lambda_handler, json: Optional[dict] = None)¶
Lambda handler decorator.
The @route decorator automatically format the response from a handler into the expected API Gateway + Lambda format. It also parses API Gateway’s events into a Request object available as a “request” argument, and optionally provides body and query string parameters schema validation.
Examples
Define a ‘hello’ handler. A request argument (Request object) can be used in the handler.
>>> @route ... def hello(request): ... name = request.json["name"] ... return 200, {"message" : f"Hello {name} !"}
>>> hello({"body": '{"name": "John Doe"}'}, {}) {'statusCode': 200, 'body': '{"message": "Hello John Doe !"}'}
Route¶
Request¶
- class restful_aws_lambda.request.Request(event: dict)¶
Request objects created in @route handlers, and accessible from within the handler as the “request” argument. It contains parsed information about the API Gateway event.
- Parameters
event (dict) – The API Gateway event.
- property body: Optional[str]¶
Return the raw body field of the API Gateway event.
Examples
>>> event = {"body": '{"name": "John Doe"}'} >>> Request(event).body '{"name": "John Doe"}'
- property headers: dict¶
Return the request’s headers.
Examples
>>> event = {"headers": {"accept": "*/*"}} >>> Request(event).headers {'accept': '*/*'}
- property json: Optional[dict]¶
Return a dict parsed from the body field of the API Gateway event.
Examples
>>> event = {"body": '{"name": "John Doe"}'} >>> Request(event).json["name"] 'John Doe'
- property method: str¶
Return the request’s HTTP method.
Examples
>>> event = {"httpMethod": "GET"} >>> Request(event).method 'GET'
- property path_params: dict¶
Return the pathParameters field of the API Gateway event.
Examples
>>> event = {"pathParameters": {"user_id": 123}} >>> Request(event).path_params {'user_id': 123}
- property query_params: dict¶
Return the queryStringParameters field of the API Gateway event.
Examples
>>> event = {"queryStringParameters": {"page": "3"}} >>> Request(event).query_params {'page': '3'}