Skip to content

API Gateway

Utilities for working with AWS API Gateway.


get_rest_api

get_rest_api(api_name, region=None)

Get a REST API by name.

Parameters:

Name Type Description Default
api_name str

The name of the REST API to find.

required
region Optional[str]

AWS region. Defaults to None (uses default region).

None

Raises:

Type Description
ResourceNotFoundError

If no REST API with the given name is found.

Returns:

Type Description
RestApiTypeDef

The REST API configuration.

Source code in src/aibs_informatics_aws_utils/apigateway.py
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
def get_rest_api(api_name: str, region: Optional[str] = None) -> RestApiTypeDef:
    """Get a REST API by name.

    Args:
        api_name (str): The name of the REST API to find.
        region (Optional[str]): AWS region. Defaults to None (uses default region).

    Raises:
        ResourceNotFoundError: If no REST API with the given name is found.

    Returns:
        The REST API configuration.
    """
    apigw = get_apigateway_client(region=region)

    paginator = apigw.get_paginator("get_rest_apis")
    rest_apis: List[RestApiTypeDef] = paginator.paginate(
        PaginationConfig={"MaxItems": 100}
    ).build_full_result()["items"]

    for rest_api in rest_apis:
        # In theory, only one api should be associated with env-base
        if rest_api.get("name") == api_name:
            return rest_api
    else:
        raise ResourceNotFoundError(f"Could not resolve REST Api with {api_name}")

get_rest_api_endpoint

get_rest_api_endpoint(rest_api, stage='prod', region=None)

Get the endpoint URL for a REST API.

Parameters:

Name Type Description Default
rest_api RestApiTypeDef

The REST API configuration from get_rest_api().

required
stage str

The API Gateway stage name. Defaults to "prod".

'prod'
region Optional[str]

AWS region. Defaults to None (uses default region).

None

Returns:

Type Description
str

The fully qualified endpoint URL for the REST API.

Source code in src/aibs_informatics_aws_utils/apigateway.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
def get_rest_api_endpoint(
    rest_api: RestApiTypeDef, stage: str = "prod", region: Optional[str] = None
) -> str:
    """Get the endpoint URL for a REST API.

    Args:
        rest_api (RestApiTypeDef): The REST API configuration from get_rest_api().
        stage (str): The API Gateway stage name. Defaults to "prod".
        region (Optional[str]): AWS region. Defaults to None (uses default region).

    Returns:
        The fully qualified endpoint URL for the REST API.
    """
    api_id = rest_api["id"]  # type: ignore  # mypy_boto3 TypeDict makes optional, but actually is required
    region = get_region(region)
    return f"https://{api_id}.execute-api.{region}.amazonaws.com/{stage}"