return data
def get(self, key, *args, **kwargs):
- if args:
- return {'detail': (key, args)}
- return FooResource.elems[int(key)]
+ return {'detail': (key, args)}
def delete(self, key):
del FooResource.elems[int(key)]
return dict(key=key, **data)
+@ApiController('fooargs')
class FooArgs(RESTController):
@RESTController.args_from_json
def set(self, code, name, opt1=None, opt2=None):
self.assertJsonBody({'code': 'hello', 'name': 'world', 'opt1': None, 'opt2': 'opt2'})
def test_detail_route(self):
+ self._get('/foo/default')
+ self.assertJsonBody({'detail': ['default', []]})
+
+ self._get('/foo/default/default')
+ self.assertJsonBody({'detail': ['default', ['default']]})
+
self._get('/foo/1/detail')
self.assertJsonBody({'detail': ['1', ['detail']]})
from six import add_metaclass
from .settings import Settings
-from . import logger
+from . import logger, mgr
def ApiController(path):
@cherrypy.expose
def default(self, *vpath, **params):
+ if cherrypy.request.path_info.startswith(
+ '{}/api/{}/default'.format(mgr.url_prefix, self._cp_path_)) or \
+ cherrypy.request.path_info.startswith('/{}/default'.format(self._cp_path_)):
+ # These two calls to default() are identical: `vpath` and
+ # params` are both empty:
+ # $ curl 'http://localhost/api/cp_path/'
+ # and
+ # $ curl 'http://localhost/api/cp_path/default'
+ # But we need to distinguish them. To fix this, we need
+ # to add the missing `default`
+ vpath = ['default'] + list(vpath)
+
method, status_code = self._get_method(vpath)
if cherrypy.request.method not in ['GET', 'DELETE']: