diff options
author | Lars Wirzenius <liw@liw.fi> | 2017-10-04 18:09:49 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2017-10-04 18:09:49 +0300 |
commit | ff66d5b7119a7d33c1664ce9656fd733d368a113 (patch) | |
tree | df8dd2f29605dd1f4602f54890b217af0fa0bd97 | |
parent | ecbd943ae1f99d594d9bfb45987f831196d6929b (diff) | |
download | apifw-ff66d5b7119a7d33c1664ce9656fd733d368a113.tar.gz |
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.
-rw-r--r-- | apifw/bottleapp.py | 17 |
1 files changed, 16 insertions, 1 deletions
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 |