From ff66d5b7119a7d33c1664ce9656fd733d368a113 Mon Sep 17 00:00:00 2001 From: Lars Wirzenius Date: Wed, 4 Oct 2017 18:09:49 +0300 Subject: Add: optional list of resources to create_bottle_application This allows routes to be set up from the beginning. It's a workaround for a probelm in Qvarn where /foos/notifications is 404 untile /foos has been accessed. --- apifw/bottleapp.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/apifw/bottleapp.py b/apifw/bottleapp.py index 0d61e09..774e1c2 100644 --- a/apifw/bottleapp.py +++ b/apifw/bottleapp.py @@ -235,6 +235,18 @@ class BottleApplication: else: raise + def add_routes_for_resource_type(self, rt): + routes = self._api.find_missing_route(rt.get_path()) + for route in routes: + callback = self._callback_with_body(route['callback']) + route_dict = { + 'method': route.get('method', 'GET'), + 'path': route['path'], + 'callback': callback, + } + self._bottleapp.route(**route_dict) + self._authz.set_route_authorization(route) + def _callback_with_body(self, callback): def wrapper(*args, **kwargs): kwargs['raw_uri_path'] = bottle.request.environ['RAW_URI'] @@ -260,7 +272,7 @@ class BottleApplication: raise bottle.HTTPError(status=apifw.HTTP_BAD_REQUEST, body=str(e)) -def create_bottle_application(api, counter, logger, config): +def create_bottle_application(api, counter, logger, config, resource_types=None): # Create a new bottle.Bottle application, set it up, and return it # so that gunicorn can execute it from the main program. @@ -280,4 +292,7 @@ def create_bottle_application(api, counter, logger, config): app.add_plugin(authz) app.set_authorization_plugin(authz) + for rt in resource_types or []: + app.add_routes_for_resource_type(rt) + return bottleapp -- cgit v1.2.1