faster_sam.adapter module

exception faster_sam.adapter.GatewayLookupError

Bases: LookupError

Raised when performing API Gateway lookup, usually when multiple gateways are available requiring an explicit target.

class faster_sam.adapter.SAM(
template_path=None,
)

Bases: object

Adapter class for FastAPI applications allowing map API routes defined on a CloudFormation template or OpenAPI file.

Parameters:

template_path (Optional[str]) – Path to the CloudFormation template file.

template

Instance of CloudformationTemplate based on the provided template_path.

Type:

CloudformationTemplate

configure_api(
app,
gateway_id=None,
)

Configures the FastAPI application with routes based on one of the following template files: CloudFormation, OpenAPI.

Parameters:
  • app (FastAPI) – The FastAPI application instance to be configured.

  • gateway_id (Optional[str], optional) – Optional API Gateway resource ID defined on the CloudFormation template. When multiple API Gateways are defined on the template this argument is required to identify which gateway is being mapped.

Raises:

GatewayLookupError – Raised if multiple API Gateways are defined on the CloudFormation template and the gateway_id are not set.

lambda_mapper(
gateway_id,
)

Generate a route map extracted from the lambda functions schema corresponding to the given gateway id.

Parameters:

gateway_id (Optional[str]) – Optional gateway id to filter the routes for a specific API Gateway.

Returns:

Dictionary containing the routes.

Return type:

Dict[str, Any]

openapi_mapper(
openapi_schema,
)

Create a route map extracted from the given OpenAPI schema.

Notice that this is an OpenAPI schema used by AWS SAM to configure an API Gateway, therefore it requires AWS extensions pointing to event consumers, most of the time Lambda Functions.

e.g.

>>> # The OpenAPI input schema is omitting information for brevity
>>> schema = '''
... paths:
...   /health:
...     get:
...       x-amazon-apigateway-integration:
...          passthroughBehavior: when_no_match
...          httpMethod: POST
...          type: aws_proxy
...          uri:
...            Fn::Sub: "arn:aws:apigateway::lambda:path/2015-03-31/
...            functions/${HealthFunction.Arn}/invocations"
...          credentials: "arn:aws:iam:::role/apigateway-invoke-lambda-role"
... '''
>>> SAM().openapi_mapper(schema)
{"/health": {"get": {"handler": "src.handlers.health"}}}
Parameters:

openapi_schema (Dict[str, Any]) – OpenAPI schema dictionary.

Returns:

Dictionary containing the routes.

Return type:

Dict[str, Any]

register_routes(
app,
routes,
)

Registers FastAPI routes based on the provided route map into the given FastAPI application.

Parameters:
  • app (FastAPI) – The FastAPI application instance.

  • routes (Dict[str, Any]) – Dictionary containing FastAPI routes.