Skip to content

Latest commit

 

History

History
70 lines (48 loc) · 2.86 KB

aws.rst

File metadata and controls

70 lines (48 loc) · 2.86 KB

Amazon API Gateway

This section describes integration with Amazon API Gateway.

It is useful for:

ANY method

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)

Low level

The integration defines classes useful for low level integration.

AWS Proxy event

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.

Response

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.