Dan*_* P. 12 api rest python-3.x pyramid cornice
我已经多次阅读了文档,并搜索了这个问题的答案,但是很简单.具体来说,我已经研究了为服务定义服务和Cornice API以及为资源定义资源.
我目前正在构建一个REST API,它具有与此类似的结构:
GET /clients # Gets a list of clients
GET /clients/{id} # Gets a specific client
GET /clients/{id}/users # Gets a specific clients users
Run Code Online (Sandbox Code Playgroud)
最好的方法是什么?我应该使用服务或资源还是两者兼而有之?而且,如果两者都是,怎么样?
资源装饰器使用集合的URL以及对象的url模式.
collection_path =/rest/users path =/rest/users/{id}
资源装饰器最适用于视图类,您可以在对象上使用get/put/post/delete方法,以及集合上的collection_get,collection_put等.我在这里有一些例子:
https://github.com/umeboshi2/trumpet/blob/master/trumpet/views/rest/users.py
由于我大量使用资源装饰器和视图类,我没有发现需要服务函数,但它允许你创建包含视图可调用函数的get,put,post装饰器.
如果您在客户端使用backbone.js,资源装饰器和url示例可以很好地与Backbone集合和模型配合使用.