| 228 | self.routes = routes.Mapper() |
| 229 | |
| 230 | def add_spec(self, spec, transforms): |
| 231 | info = spec.get("info", {}) |
| 232 | LOG.debug( |
| 233 | "Adding API: %s %s", |
| 234 | info.get("title", "untitled"), |
| 235 | info.get("version", "0.0.0"), |
| 236 | ) |
| 237 | |
| 238 | self.spec = spec |
| 239 | self.spec_resolver = jsonschema.RefResolver("", self.spec) |
| 240 | |
| 241 | validate(fast_deepcopy_dict(self.spec)) |
| 242 | |
| 243 | for filter in transforms: |
| 244 | for (path, methods) in six.iteritems(spec["paths"]): |
| 245 | if not re.search(filter, path): |
| 246 | continue |
| 247 | |
| 248 | for (method, endpoint) in six.iteritems(methods): |
| 249 | conditions = {"method": [method.upper()]} |
| 250 | |
| 251 | connect_kw = {} |
| 252 | if "x-requirements" in endpoint: |
| 253 | connect_kw["requirements"] = endpoint["x-requirements"] |
| 254 | |
| 255 | m = self.routes.submapper( |
| 256 | _api_path=path, _api_method=method, conditions=conditions |
| 257 | ) |
| 258 | for transform in transforms[filter]: |
| 259 | m.connect(None, re.sub(filter, transform, path), **connect_kw) |
| 260 | |
| 261 | module_name = endpoint["operationId"].split(":", 1)[0] |
| 262 | __import__(module_name) |
| 263 | |
| 264 | for route in sorted(self.routes.matchlist, key=lambda r: r.routepath): |
| 265 | LOG.debug( |
| 266 | "Route registered: %+6s %s", |
| 267 | route.conditions["method"][0], |
| 268 | route.routepath, |
| 269 | ) |
| 270 | |
| 271 | def match(self, req): |
| 272 | # NOTE: webob.url_unquote doesn't work correctly under Python 3 when paths contain non-ascii |