|
| 1 | +""" |
| 2 | +Class to store the API configurations in the SAM Template. This class helps store both implicit and explicit |
| 3 | +routes in a standardized format |
| 4 | +""" |
| 5 | + |
| 6 | +import logging |
| 7 | +from collections import defaultdict |
| 8 | + |
| 9 | +from six import string_types |
| 10 | + |
| 11 | +from samcli.local.apigw.local_apigw_service import Route |
| 12 | +from samcli.commands.local.lib.provider import Api |
| 13 | + |
| 14 | +LOG = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class ApiCollector(object): |
| 18 | + |
| 19 | + def __init__(self): |
| 20 | + # Route properties stored per resource. |
| 21 | + self._route_per_resource = defaultdict(list) |
| 22 | + |
| 23 | + # processed values to be set before creating the api |
| 24 | + self._routes = [] |
| 25 | + self.binary_media_types_set = set() |
| 26 | + self.stage_name = None |
| 27 | + self.stage_variables = None |
| 28 | + |
| 29 | + def __iter__(self): |
| 30 | + """ |
| 31 | + Iterator to iterate through all the routes stored in the collector. In each iteration, this yields the |
| 32 | + LogicalId of the route resource and a list of routes available in this resource. |
| 33 | + Yields |
| 34 | + ------- |
| 35 | + str |
| 36 | + LogicalID of the AWS::Serverless::Api or AWS::ApiGateway::RestApi resource |
| 37 | + list samcli.commands.local.lib.provider.Api |
| 38 | + List of the API available in this resource along with additional configuration like binary media types. |
| 39 | + """ |
| 40 | + |
| 41 | + for logical_id, _ in self._route_per_resource.items(): |
| 42 | + yield logical_id, self._get_routes(logical_id) |
| 43 | + |
| 44 | + def add_routes(self, logical_id, routes): |
| 45 | + """ |
| 46 | + Stores the given routes tagged under the given logicalId |
| 47 | + Parameters |
| 48 | + ---------- |
| 49 | + logical_id : str |
| 50 | + LogicalId of the AWS::Serverless::Api or AWS::ApiGateway::RestApi resource |
| 51 | + routes : list of samcli.commands.local.agiw.local_apigw_service.Route |
| 52 | + List of routes available in this resource |
| 53 | + """ |
| 54 | + self._get_routes(logical_id).extend(routes) |
| 55 | + |
| 56 | + def _get_routes(self, logical_id): |
| 57 | + """ |
| 58 | + Returns the properties of resource with given logical ID. If a resource is not found, then it returns an |
| 59 | + empty data. |
| 60 | + Parameters |
| 61 | + ---------- |
| 62 | + logical_id : str |
| 63 | + Logical ID of the resource |
| 64 | + Returns |
| 65 | + ------- |
| 66 | + samcli.commands.local.lib.Routes |
| 67 | + Properties object for this resource. |
| 68 | + """ |
| 69 | + |
| 70 | + return self._route_per_resource[logical_id] |
| 71 | + |
| 72 | + @property |
| 73 | + def routes(self): |
| 74 | + return self._routes if self._routes else self.all_routes() |
| 75 | + |
| 76 | + @routes.setter |
| 77 | + def routes(self, routes): |
| 78 | + self._routes = routes |
| 79 | + |
| 80 | + def all_routes(self): |
| 81 | + """ |
| 82 | + Gets all the routes within the _route_per_resource |
| 83 | +
|
| 84 | + Return |
| 85 | + ------- |
| 86 | + All the routes within the _route_per_resource |
| 87 | + """ |
| 88 | + routes = [] |
| 89 | + for logical_id in self._route_per_resource.keys(): |
| 90 | + routes.extend(self._get_routes(logical_id)) |
| 91 | + return routes |
| 92 | + |
| 93 | + def get_api(self): |
| 94 | + """ |
| 95 | + Creates the api using the parts from the ApiCollector. The routes are also deduped so that there is no |
| 96 | + duplicate routes with the same function name, path, but different method. |
| 97 | +
|
| 98 | + The normalised_routes are the routes that have been processed. By default, this will get all the routes. |
| 99 | + However, it can be changed to override the default value of normalised routes such as in SamApiProvider |
| 100 | +
|
| 101 | + Return |
| 102 | + ------- |
| 103 | + An Api object with all the properties |
| 104 | + """ |
| 105 | + api = Api() |
| 106 | + api.routes = self.dedupe_function_routes(self.routes) |
| 107 | + api.binary_media_types_set = self.binary_media_types_set |
| 108 | + api.stage_name = self.stage_name |
| 109 | + api.stage_variables = self.stage_variables |
| 110 | + return api |
| 111 | + |
| 112 | + @staticmethod |
| 113 | + def dedupe_function_routes(routes): |
| 114 | + """ |
| 115 | + Remove duplicate routes that have the same function_name and method |
| 116 | +
|
| 117 | + route: list(Route) |
| 118 | + List of Routes |
| 119 | +
|
| 120 | + Return |
| 121 | + ------- |
| 122 | + A list of routes without duplicate routes with the same function_name and method |
| 123 | + """ |
| 124 | + grouped_routes = {} |
| 125 | + |
| 126 | + for route in routes: |
| 127 | + key = "{}-{}".format(route.function_name, route.path) |
| 128 | + config = grouped_routes.get(key, None) |
| 129 | + methods = route.methods |
| 130 | + if config: |
| 131 | + methods += config.methods |
| 132 | + sorted_methods = sorted(methods) |
| 133 | + grouped_routes[key] = Route(function_name=route.function_name, path=route.path, methods=sorted_methods) |
| 134 | + return list(grouped_routes.values()) |
| 135 | + |
| 136 | + def add_binary_media_types(self, logical_id, binary_media_types): |
| 137 | + """ |
| 138 | + Stores the binary media type configuration for the API with given logical ID |
| 139 | + Parameters |
| 140 | + ---------- |
| 141 | +
|
| 142 | + logical_id : str |
| 143 | + LogicalId of the AWS::Serverless::Api resource |
| 144 | +
|
| 145 | + api: samcli.commands.local.lib.provider.Api |
| 146 | + Instance of the Api which will save all the api configurations |
| 147 | +
|
| 148 | + binary_media_types : list of str |
| 149 | + List of binary media types supported by this resource |
| 150 | + """ |
| 151 | + |
| 152 | + binary_media_types = binary_media_types or [] |
| 153 | + for value in binary_media_types: |
| 154 | + normalized_value = self.normalize_binary_media_type(value) |
| 155 | + |
| 156 | + # If the value is not supported, then just skip it. |
| 157 | + if normalized_value: |
| 158 | + self.binary_media_types_set.add(normalized_value) |
| 159 | + else: |
| 160 | + LOG.debug("Unsupported data type of binary media type value of resource '%s'", logical_id) |
| 161 | + |
| 162 | + @staticmethod |
| 163 | + def normalize_binary_media_type(value): |
| 164 | + """ |
| 165 | + Converts binary media types values to the canonical format. Ex: image~1gif -> image/gif. If the value is not |
| 166 | + a string, then this method just returns None |
| 167 | + Parameters |
| 168 | + ---------- |
| 169 | + value : str |
| 170 | + Value to be normalized |
| 171 | + Returns |
| 172 | + ------- |
| 173 | + str or None |
| 174 | + Normalized value. If the input was not a string, then None is returned |
| 175 | + """ |
| 176 | + |
| 177 | + if not isinstance(value, string_types): |
| 178 | + # It is possible that user specified a dict value for one of the binary media types. We just skip them |
| 179 | + return None |
| 180 | + |
| 181 | + return value.replace("~1", "/") |
0 commit comments