This section describes integration with Amazon API Gateway.
It is useful for:
- AWS Proxy integrations (i.e. AWS Lambda) for HTTP API where Lambda functions handle events from API Gateway (Amazon API Gateway event format version 1.0 and 2.0).
- HTTP Proxy integrations for HTTP API <https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-develop-integrations-http.html> where HTTP service handle events from API Gateway.
- AWS Lambda function URLs where Lambda functions handle events from dedicated HTTP(S) endpoint (Amazon API Gateway event format version 2.0).
Amazon API Gateway defines special ANY
method that catches all HTTP methods. It is specified as x-amazon-apigateway-any-method OpenAPI extension. The extension is handled within custom path finder and can be used by setting path_finder_cls
to be APIGatewayPathFinder
:
from openapi_core.contrib.aws import APIGatewayPathFinder
config = Config(
path_finder_cls=APIGatewayPathFinder,
)
openapi = OpenAPI.from_file_path('openapi.json', config=config)
The integration defines classes useful for low level integration.
Use APIGatewayAWSProxyV2OpenAPIRequest
to create OpenAPI request from an API Gateway event (format version 2.0):
from openapi_core.contrib.aws import APIGatewayAWSProxyV2OpenAPIRequest
def handler(event, context):
openapi_request = APIGatewayAWSProxyV2OpenAPIRequest(event)
result = openapi.unmarshal_request(openapi_request)
return {
"statusCode": 200,
"body": "Hello world",
}
If you use format version 1.0, then import and use APIGatewayAWSProxyOpenAPIRequest
.
Use APIGatewayEventV2ResponseOpenAPIResponse
to create OpenAPI response from API Gateway event (format version 2.0) response:
from openapi_core.contrib.aws import APIGatewayEventV2ResponseOpenAPIResponse
def handler(event, context):
openapi_request = APIGatewayEventV2OpenAPIRequest(event)
response = {
"statusCode": 200,
"body": "Hello world",
}
openapi_response = APIGatewayEventV2ResponseOpenAPIResponse(response)
result = openapi.unmarshal_response(openapi_request, openapi_response)
return response
If you use format version 1.0, then import and use APIGatewayEventResponseOpenAPIResponse
.