Friday, November 21, 2008

REST with cherrypy

I've been doing web services for a while. And while SOAP definitively has its place, if you control both sites of the web service REST can really make life easier. Cherrypy with Python Routes pretty much makes this trivial. Obviously everyone knows how to route gets, but what about the nasty post and put (REST talk for create and update). NO problem.


d = cherrypy.dispatch.RoutesDispatcher()

d.connect('create_something', '/something', controller=root.something_controller, action='create_something',conditions=dict(method=['POST']))

What this is saying is I want a to route all routes that match /something and are of the method "POST" to the controller root.something_controller and the method "create_something". Thats it. PUT is just as easy.

d.connect('create_something', '/something/:id', controller=root.something_controller, action='update_something',conditions=dict(method=['PUT']))

Same thing as above except the method update_something will get the param id.

No comments: