in samcli/commands/local/lib/swagger/parser.py [0:0]
def get_routes(self, event_type=Route.API) -> List[Route]:
"""
Parses a swagger document and returns a list of APIs configured in the document.
Swagger documents have the following structure
{
"/path1": { # path
"get": { # method
"x-amazon-apigateway-integration": { # integration
"type": "aws_proxy",
# URI contains the Lambda function ARN that needs to be parsed to get Function Name
"uri": {
"Fn::Sub":
"arn:aws:apigateway:aws:lambda:path/2015-03-31/functions/${LambdaFunction.Arn}/..."
}
}
},
"post": {
},
},
"/path2": {
...
}
}
Returns
-------
list of list of samcli.commands.local.apigw.local_apigw_service.Route
List of APIs that are configured in the Swagger document
"""
result = []
paths_dict = self.swagger.get("paths", {})
for full_path, path_config in paths_dict.items():
for method, method_config in path_config.items():
function_name = self._get_integration_function_name(method_config)
if not function_name:
LOG.debug(
"Lambda function integration not found in Swagger document at path='%s' method='%s'",
full_path,
method,
)
continue
normalized_method = method
if normalized_method.lower() == self._ANY_METHOD_EXTENSION_KEY:
# Convert to a more commonly used method notation
normalized_method = self._ANY_METHOD
payload_format_version = self._get_payload_format_version(method_config)
authorizers = method_config.get(SwaggerParser._SWAGGER_SECURITY, None)
authorizer_name = None
use_default_authorizer = True
if authorizers is not None:
if not isinstance(authorizers, list):
raise InvalidSecurityDefinition(
"Invalid security definition found, authorizers for "
f"path='{full_path}' method='{method}' must be a list"
)
if len(authorizers) > 1:
raise MultipleAuthorizerException(
"There must only be a single authorizer defined "
f"for path='{full_path}' method='{method}', found '{len(authorizers)}'"
)
if len(authorizers) == 1 and authorizers[0] != {}:
# user has authorizer defined
authorizer_object = authorizers[0]
authorizer_object = list(authorizers[0])
# make sure that authorizer actually has keys
if len(authorizer_object) != 1:
raise InvalidSecurityDefinition(
"Invalid security definition found, authorizers for "
f"path='{full_path}' method='{method}' must contain an authorizer"
)
authorizer_name = str(authorizer_object[0])
else:
# customer provided empty list, do not use default authorizer
use_default_authorizer = False
route = Route(
function_name,
full_path,
methods=[normalized_method],
event_type=event_type,
payload_format_version=payload_format_version,
operation_name=method_config.get("operationId"),
stack_path=self.stack_path,
authorizer_name=authorizer_name,
use_default_authorizer=use_default_authorizer,
)
result.append(route)
return result