def _extract_from_serverless_api(self, logical_id, api_resource, collector): """ Extract APIs from AWS::Serverless::Api resource by reading and parsing Swagger documents. The result is added to the collector. Parameters ---------- logical_id : str Logical ID of the resource api_resource : dict Resource definition, including its properties collector : ApiCollector Instance of the API collector that where we will save the API information """ properties = api_resource.get("Properties", {}) body = properties.get("DefinitionBody") uri = properties.get("DefinitionUri") binary_media = properties.get("BinaryMediaTypes", []) stage_name = properties.get("StageName") stage_variables = properties.get("Variables") if not body and not uri: # Swagger is not found anywhere. LOG.debug( "Skipping resource '%s'. Swagger document not found in DefinitionBody and DefinitionUri", logical_id) return reader = SamSwaggerReader(definition_body=body, definition_uri=uri, working_dir=self.cwd) swagger = reader.read() parser = SwaggerParser(swagger) apis = parser.get_apis() LOG.debug("Found '%s' APIs in resource '%s'", len(apis), logical_id) collector.add_apis(logical_id, apis) collector.add_binary_media_types( logical_id, parser.get_binary_media_types()) # Binary media from swagger collector.add_binary_media_types( logical_id, binary_media) # Binary media specified on resource in template collector.add_stage_name(logical_id, stage_name) collector.add_stage_variables(logical_id, stage_variables)
def extract_swagger_route( stack_path: str, logical_id: str, body: Dict, uri: Union[str, Dict], binary_media: Optional[List], collector: ApiCollector, cwd: Optional[str] = None, event_type: str = Route.API, ) -> None: """ Parse the Swagger documents and adds it to the ApiCollector. Parameters ---------- stack_path : str Path of the stack the resource is located logical_id : str Logical ID of the resource body : dict The body of the RestApi uri : str or dict The url to location of the RestApi binary_media : list The link to the binary media collector : samcli.lib.providers.api_collector.ApiCollector Instance of the Route collector that where we will save the route information cwd : str Optional working directory with respect to which we will resolve relative path to Swagger file event_type : str The event type, 'Api' or 'HttpApi', see samcli/local/apigw/local_apigw_service.py:35 """ reader = SwaggerReader(definition_body=body, definition_uri=uri, working_dir=cwd) swagger = reader.read() parser = SwaggerParser(stack_path, swagger) routes = parser.get_routes(event_type) LOG.debug("Found '%s' APIs in resource '%s'", len(routes), logical_id) collector.add_routes(logical_id, routes) collector.add_binary_media_types( logical_id, parser.get_binary_media_types()) # Binary media from swagger collector.add_binary_media_types( logical_id, binary_media) # Binary media specified on resource in template
def extract_swagger_route(self, logical_id, body, uri, binary_media, collector, cwd=None, event_type=Route.API): """ Parse the Swagger documents and adds it to the ApiCollector. Parameters ---------- logical_id : str Logical ID of the resource body : dict The body of the RestApi uri : str or dict The url to location of the RestApi binary_media: list The link to the binary media collector: samcli.commands.local.lib.route_collector.RouteCollector Instance of the Route collector that where we will save the route information cwd : str Optional working directory with respect to which we will resolve relative path to Swagger file """ reader = SwaggerReader(definition_body=body, definition_uri=uri, working_dir=cwd) swagger = reader.read() parser = SwaggerParser(swagger) routes = parser.get_routes(event_type) LOG.debug("Found '%s' APIs in resource '%s'", len(routes), logical_id) collector.add_routes(logical_id, routes) collector.add_binary_media_types( logical_id, parser.get_binary_media_types()) # Binary media from swagger collector.add_binary_media_types( logical_id, binary_media) # Binary media specified on resource in template
def test_binary_media_type_returned(self, test_case_name, swagger, expected_result): parser = SwaggerParser(swagger) self.assertEquals(parser.get_binary_media_types(), expected_result)
def test_binary_media_type_returned(self, test_case_name, swagger, expected_result): parser = SwaggerParser(swagger) self.assertEquals(parser.get_binary_media_types(), expected_result)