Skip to content

API - API Docs eway module

This module creates the API Docs resources on a provided AWS API Gateway.

Example

Typical Usage::

from stacks.resources.api_gateway.resources import api_docs

api_docs.create(stack, created_api_gateway, auth, api_doc_function_arn)

create(stack, rest_api_gateway, auth, api_doc_function_arn, model_name='docs')

Create the API resource and methods on the provided REST API Gateway.

Creates a GET method on the /{model_name} resource with a forward to /{model_name}/index.html and a ANY method with a Lambda Proxy Integration to serve the API Documentation via this Lambda function.

Attributes:

Name Type Description
stack aws_cdk.core.Stack

CDK Stack.

rest_api_gateway aws_cdk.aws_apigateway.RestApi

Rest API to add to.

auth aws_cdk.aws_cognito.CognitoUserPoolsAuthorizer

Cognito User Pool Authorizer used for the endpoint authentication.

api_doc_function_arn str

ARN of the Lambda function which is called by the method /{model_name} -> ANY.

model_name Optional[str]

The path for the resources, default docs.

Source code in infrastructure/stacks/resources/api_gateway/resources/api_docs.py
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
def create(stack: core.Stack, rest_api_gateway: apigateway.RestApi, auth: apigateway.CognitoUserPoolsAuthorizer,  api_doc_function_arn: str,  model_name: Optional[str] = "docs" ) -> None:
    """Create the API resource and methods on the provided REST API Gateway.

    Creates a GET method on the `/{model_name}` resource with a forward to `/{model_name}/index.html`
    and a ANY method with a Lambda Proxy Integration to serve the API Documentation via this Lambda function.

    Attributes:
        stack (aws_cdk.core.Stack): CDK Stack.
        rest_api_gateway (aws_cdk.aws_apigateway.RestApi): Rest API to add to.
        auth (aws_cdk.aws_cognito.CognitoUserPoolsAuthorizer): Cognito User Pool Authorizer used for the endpoint authentication.
        api_doc_function_arn (str): ARN of the Lambda function which is called by the method `/{model_name} -> ANY`.
        model_name (Optional[str]): The path for the resources, default `docs`.

    """

    api_doc_function = Function.from_function_arn(stack, "WaReviewToolApiDocsFunction", api_doc_function_arn)

    CfnPermission(
        stack,
        'WaReviewToolApiDocsFunctionPermission',
        action="lambda:InvokeFunction",
        function_name=api_doc_function_arn,
        principal='apigateway.amazonaws.com',
        source_arn=rest_api_gateway.arn_for_execute_api() 
    )

    # api_docs_model = rest_api_gateway.add_model(
    #     "WaReviewToolApiDocsModel",
    #     content_type="application/json",
    #     description="Validate the response body",
    #     model_name="ApiDocsModel",
    #     schema=apigateway.JsonSchema(
    #         type=apigateway.JsonSchemaType.OBJECT
    #     )
    # )

    docs_integration = apigateway.LambdaIntegration(
        api_doc_function,
        proxy=True,
        integration_responses=[
                apigateway.IntegrationResponse(
                    status_code="200",
                    response_parameters={
                        "method.response.header.Access-Control-Allow-Origin": "'*'"
                    },       
                ),
                apigateway.IntegrationResponse(
                    selection_pattern=".*\"failure\".*",
                    status_code="500",
                    response_templates= {
                        "application/json": '{"error": "Internal Service Error"}'
                    }
                )         
            ]
    )

    method_responses=[
        apigateway.MethodResponse(status_code='200', response_parameters= { 'method.response.header.Access-Control-Allow-Origin': True }),
        apigateway.MethodResponse(status_code='400', response_parameters= { 'method.response.header.Access-Control-Allow-Origin': True }),
        apigateway.MethodResponse(status_code='500', response_parameters= { 'method.response.header.Access-Control-Allow-Origin': True }),
    ]

    docs_resource = rest_api_gateway.root.add_resource(model_name.lower())
    docs_resource_proxy = docs_resource.add_proxy(any_method=False)
    docs_resource_proxy.add_method('ANY', integration=docs_integration, method_responses=method_responses) #, authorizer=auth, authorization_type=apigateway.AuthorizationType.COGNITO)