In my last post, I spoke about Richardson Maturity model and its useful to demarcate web services based on their implementation maturity. We also understood, how CRUD Web Services fall under Level2 in Maturity model. Let’s take an example of CRUD Web Service developed with Tornado.
In this example, we develop a ‘blogger’ Web service by using multiple logical resources (/blog, /blog/id, /blogs) and HTTP Verbs (GET, POST, PUT and DELETE) to perform various operations offered by the blogger service. Services offered are detailed below.
|http://localhost/blog/||POST||Creates a blog|
|http://localhost/blog/id||GET||Displays the blog|
|http://localhost/blog/id||PUT||Updates the blog|
|http://localhost/blog/id||DELETE||Deletes the blog|
|http://localhost/blogs/||GET||Lists all the blogs|
|http://localhost/blogs/||DELETE||Deletes all the blogs|
In the above code snippet:
- We have a base class Home that initializes (method initialize) the blogger service with a Welcome blog. (as it’s true with Google blog or WordPress).
- Class Blog inherits from class Home for the initialization and exports methods get(), put() and delete() for performing operations like reading, updating and deleting a blog (based on blog id).
- post() method doesn’t take any argument but it creates a new blog and assigns a blogid. It also creates a resource /blog/id and returns a HTTP status code of 201 along with Location in Http headers.
- Class Blogs export operations to be performed on all blogs and enables GET and DELETE Http operations to enable the user to view all his blogs or delete all blogs respectively.
- All the responses from the blogger web service are JSON based (application/json).
The client more or less uses all the capabilities of blogger web service and displays the JSON responses and Http headers as sent by the web service.