Pylons controller template for REST
Wednesday, January 3rd, 2007(Edit 2007-12-11: A little late to edit this, but "paster restcontroller" was quickly adopted into Pylons. What a great team they are!)
(Edit 2007-12-11: I no longer work on the SPHYRA project.)
It's a new calendar year, and with it comes the usual sense of renewal and thoughts of "I meant to do that last year, but didn't, so I should try again this year".
One of the things that I had been reading a lot about last year (and the year before, in fact) was REST. Now that I'm working on a fresh start for SPHYRA, now is the time for me to become a RESTafarian. I suppose it's only appropriate since I have dreadlocks that I've been growing and grooming for a little over a year now :)
In my search for useful information for implementing a RESTful web app using Pylons, I found information about RESTful services using Routes that came in handy. What I couldn't find was a template that could be cut-'n-pasted into a new controller module to save some typing.
Here is my first take at such a template. I haven't attached any code to it yet, so it's not guaranteed to be correct. As I attach code to it and/or receive commentary, I'll update this post so that it becomes more correct:
class ItemController(BaseController):
def index(self, format='html'):
"""GET /: All items in the collection."""
def create(self):
"""POST /: Create a new item."""
def new(self, format='html'):
"""GET /new: Form to create a new item."""
def update(self, id):
"""PUT /id: Update an existing item."""
# Forms posted to this method should contain a hidden field:
# <input type="hidden" name="_method" value="PUT" />
def delete(self, id):
"""DELETE /id: Delete an existing item."""
# Forms posted to this method should contain a hidden field:
# <input type="hidden" name="_method" value="DELETE" />
def show(self, id, format='html'):
"""GET /id: Show a specific item."""
def edit(self, id, format='html'):
"""GET /id;edit: Form to edit an existing item."""
Perhaps this could be automated with a paster restcontroller thing things command in the next version of Pylons? :)