Middleware
MODULE | DESCRIPTION |
---|---|
base |
|
openapi_validation |
|
schema_validation |
|
CLASS | DESCRIPTION |
---|---|
BaseMiddlewareHandler |
Base implementation for Middlewares to run code before and after in a chain. |
NextMiddleware |
|
BaseMiddlewareHandler ¶
Bases: Generic[EventHandlerInstance]
, ABC
Base implementation for Middlewares to run code before and after in a chain.
This is the middleware handler function where middleware logic is implemented.
The next middleware handler is represented by next_middleware
, returning a Response object.
Example
Correlation ID Middleware
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
METHOD | DESCRIPTION |
---|---|
handler |
The Middleware Handler |
handler
abstractmethod
¶
handler(
app: EventHandlerInstance,
next_middleware: NextMiddleware,
) -> Response
The Middleware Handler
PARAMETER | DESCRIPTION |
---|---|
app
|
An instance of an Event Handler that implements ApiGatewayResolver
TYPE:
|
next_middleware
|
The next middleware handler in the chain
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
The response from the next middleware handler in the chain |
Source code in aws_lambda_powertools/event_handler/middlewares/base.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
base ¶
CLASS | DESCRIPTION |
---|---|
BaseMiddlewareHandler |
Base implementation for Middlewares to run code before and after in a chain. |
NextMiddleware |
|
BaseMiddlewareHandler ¶
Bases: Generic[EventHandlerInstance]
, ABC
Base implementation for Middlewares to run code before and after in a chain.
This is the middleware handler function where middleware logic is implemented.
The next middleware handler is represented by next_middleware
, returning a Response object.
Example
Correlation ID Middleware
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
|
METHOD | DESCRIPTION |
---|---|
handler |
The Middleware Handler |
handler
abstractmethod
¶
handler(
app: EventHandlerInstance,
next_middleware: NextMiddleware,
) -> Response
The Middleware Handler
PARAMETER | DESCRIPTION |
---|---|
app
|
An instance of an Event Handler that implements ApiGatewayResolver
TYPE:
|
next_middleware
|
The next middleware handler in the chain
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
The response from the next middleware handler in the chain |
Source code in aws_lambda_powertools/event_handler/middlewares/base.py
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
|
openapi_validation ¶
CLASS | DESCRIPTION |
---|---|
OpenAPIValidationMiddleware |
OpenAPIValidationMiddleware is a middleware that validates the request against the OpenAPI schema defined by the |
OpenAPIValidationMiddleware ¶
OpenAPIValidationMiddleware(
validation_serializer: (
Callable[[Any], str] | None
) = None,
)
Bases: BaseMiddlewareHandler
OpenAPIValidationMiddleware is a middleware that validates the request against the OpenAPI schema defined by the
Lambda handler. It also validates the response against the OpenAPI schema defined by the Lambda handler. It
should not be used directly, but rather through the enable_validation
parameter of the ApiGatewayResolver
.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
PARAMETER | DESCRIPTION |
---|---|
validation_serializer
|
Optional serializer to use when serializing the response for validation. Use it when you have a custom type that cannot be serialized by the default jsonable_encoder.
TYPE:
|
Source code in aws_lambda_powertools/event_handler/middlewares/openapi_validation.py
61 62 63 64 65 66 67 68 69 70 71 |
|
schema_validation ¶
CLASS | DESCRIPTION |
---|---|
SchemaValidationMiddleware |
Middleware to validate API request and response against JSON Schema using the Validation utility. |
SchemaValidationMiddleware ¶
SchemaValidationMiddleware(
inbound_schema: dict,
inbound_formats: dict | None = None,
outbound_schema: dict | None = None,
outbound_formats: dict | None = None,
)
Bases: BaseMiddlewareHandler
Middleware to validate API request and response against JSON Schema using the Validation utility.
Example
Validating incoming event
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
PARAMETER | DESCRIPTION |
---|---|
inbound_schema
|
JSON Schema to validate incoming event
TYPE:
|
inbound_formats
|
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool, by default None JSON Schema to validate outbound event, by default None
TYPE:
|
outbound_formats
|
Custom formats containing a key (e.g. int64) and a value expressed as regex or callback returning bool, by default None
TYPE:
|
METHOD | DESCRIPTION |
---|---|
handler |
Validates incoming JSON payload (body) against JSON Schema provided. |
Source code in aws_lambda_powertools/event_handler/middlewares/schema_validation.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
|
handler ¶
handler(
app: EventHandlerInstance,
next_middleware: NextMiddleware,
) -> Response
Validates incoming JSON payload (body) against JSON Schema provided.
PARAMETER | DESCRIPTION |
---|---|
app
|
An instance of an Event Handler
TYPE:
|
next_middleware
|
Callable to get response from the next middleware or route handler in the chain
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Response
|
It can return three types of response objects
|
Source code in aws_lambda_powertools/event_handler/middlewares/schema_validation.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
|