in samtranslator/model/api/api_generator.py [0:0]
def _add_gateway_responses(self) -> None:
"""
Add Gateway Response configuration to the Swagger file, if necessary
"""
if not self.gateway_responses:
return
if self.gateway_responses and not self.definition_body:
raise InvalidResourceException(
self.logical_id,
"GatewayResponses works only with inline Swagger specified in 'DefinitionBody' property.",
)
# Make sure keys in the dict are recognized
for responses_key, responses_value in self.gateway_responses.items():
if is_intrinsic(responses_value):
# TODO: Add intrinsic support for this field.
raise InvalidResourceException(
self.logical_id,
"Unable to set GatewayResponses attribute because "
"intrinsic functions are not supported for this field.",
)
if not isinstance(responses_value, dict):
raise InvalidResourceException(
self.logical_id,
f"Invalid property type '{type(responses_value).__name__}' for GatewayResponses. "
"Expected an object of type 'GatewayResponse'.",
)
for response_key in responses_value:
if response_key not in GatewayResponseProperties:
raise InvalidResourceException(
self.logical_id,
f"Invalid property '{response_key}' in 'GatewayResponses' property '{responses_key}'.",
)
if not SwaggerEditor.is_valid(self.definition_body):
raise InvalidResourceException(
self.logical_id,
"Unable to add Auth configuration because "
"'DefinitionBody' does not contain a valid Swagger definition.",
)
swagger_editor = SwaggerEditor(self.definition_body)
# The dicts below will eventually become part of swagger/openapi definition, thus requires using Py27Dict()
gateway_responses = Py27Dict()
for response_type, response in self.gateway_responses.items():
sam_expect(response, self.logical_id, f"GatewayResponses.{response_type}").to_be_a_map()
response_parameters = response.get("ResponseParameters", Py27Dict())
response_templates = response.get("ResponseTemplates", Py27Dict())
if response_parameters:
sam_expect(
response_parameters, self.logical_id, f"GatewayResponses.{response_type}.ResponseParameters"
).to_be_a_map()
gateway_responses[response_type] = ApiGatewayResponse(
api_logical_id=self.logical_id,
response_parameters=response_parameters,
response_templates=response_templates,
status_code=response.get("StatusCode", None),
)
if gateway_responses:
swagger_editor.add_gateway_responses(gateway_responses) # type: ignore[no-untyped-call]
# Assign the Swagger back to template
self.definition_body = swagger_editor.swagger