Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between a cornice.Service and cornice.resource in Cornice?

I have read through the documentation many times over and search all over for the answer to this question but have come up short. Specifically I've looked at Defining the service and the Cornice API for a service and at Defining resource for resource.

I'm currently building a REST API that will have a similar structure to this:

GET /clients             # Gets a list of clients
GET /clients/{id}        # Gets a specific client
GET /clients/{id}/users  # Gets a specific clients users

What would be the best way to go about this? Should I use a service or a resource or both? And, if both, how?

like image 849
Dan P. Avatar asked Jan 15 '14 20:01

Dan P.


2 Answers

Resources are high-level convenience, services offer lower level control.

I am just learning cornice myself. Looking at the source code, a Resource creates Services internally, one for the item and one for the collection (if a collection path is specified). The resource also adds views to the services for each method defined with an http verb as the name or in the form collection_[verb].

So there is little difference except that resources are a neat, structured way to define services.

like image 194
Graeme Stuart Avatar answered Sep 24 '22 17:09

Graeme Stuart


The resource decorator uses a url for the collection, as well as a url pattern for an object.

collection_path=/rest/users path=/rest/users/{id}

The resource decorator is best used in view classes where you can use get/put/post/delete methods on the objects, as well as collection_get, collection_put, etc. on the collection. I have some examples here:

https://github.com/umeboshi2/trumpet/blob/master/trumpet/views/rest/users.py

Since I make heavy use of the resource decorator and view classes, I haven't found a need for the service function, but it allows you to create get,put,post decorators that wrap view callable functions.

If you are using backbone.js on the client side, the resource decorator and url examples work well with the Backbone collections and models.

like image 39
umeboshi Avatar answered Sep 21 '22 17:09

umeboshi