CherryPy and RESTful web api?

I don't if it's the "best" way, but here's how I do it: import cherrypy class RESTResource(object): """ Base class for providing a RESTful interface to a resource. To use this class, simply derive a class from it and implement the methods you want to support. The list of possible methods are: handle_GET handle_PUT handle_POST handle_DELETE """ @cherrypy.

Expose def default(self, *vpath, **params): method = getattr(self, "handle_" + cherrypy.request. Method, None) if not method: methods = x. Replace("handle_", "") for x in dir(self) if x.

Startswith("handle_") cherrypy.response. Headers"Allow" = ",". Join(methods) raise cherrypy.

HTTPError(405, "Method not implemented. ") return method(*vpath, **params); class FooResource(RESTResource): def handle_GET(self, *vpath, **params): retval = "Path Elements:" + ''. Join(vpath) query = '%s=>%s' % (k,v) for k,v in params.items() retval += "Query String Elements:" + \ ''.

Join(query) return retval class Root(object): foo = FooResource() @cherrypy. Expose def index(self): return "REST example. " cherrypy.

Quickstart(Root()) You simply derive from the RESTResource class and handle whichever RESTful verbs you desire (GET, PUT, POST, DELETE) with a method of the same name prefixed with handle If you do not handle a particular verb (such as POST) the base class will raise a `405 Method Not Implemented' error for you The path items are passed in vpaths and any query strings are passed in in params Using the above sample code, if you were to request foo/bar? Woo=hoo vpath0 would be bar and params would be {'woo': 'hoo'}.

I don't if it's the "best" way, but here's how I do it: import cherrypy class RESTResource(object): """ Base class for providing a RESTful interface to a resource. To use this class, simply derive a class from it and implement the methods you want to support. The list of possible methods are: handle_GET handle_PUT handle_POST handle_DELETE """ @cherrypy.

Expose def default(self, *vpath, **params): method = getattr(self, "handle_" + cherrypy.request. Method, None) if not method: methods = x. Replace("handle_", "") for x in dir(self) if x.

Startswith("handle_") cherrypy.response. Headers"Allow" = ",". Join(methods) raise cherrypy.

HTTPError(405, "Method not implemented. ") return method(*vpath, **params); class FooResource(RESTResource): def handle_GET(self, *vpath, **params): retval = "Path Elements:" + ''. Join(vpath) query = '%s=>%s' % (k,v) for k,v in params.items() retval += "Query String Elements:" + \ ''.

Join(query) return retval class Root(object): foo = FooResource() @cherrypy. Expose def index(self): return "REST example. " cherrypy.

Quickstart(Root()) You simply derive from the RESTResource class and handle whichever RESTful verbs you desire (GET, PUT, POST, DELETE) with a method of the same name prefixed with handle_. If you do not handle a particular verb (such as POST) the base class will raise a `405 Method Not Implemented' error for you. The path items are passed in vpaths and any query strings are passed in in params.

Using the above sample code, if you were to request /foo/bar? Woo=hoo, vpath0 would be bar, and params would be {'woo': 'hoo'}.

I assume you've tried partial matches as talked about in the tutorial. I find that while not great, it does get the job done most of the time. Beyond that, though I haven't tried it, Cherrypy apparently supports Routes (see cherrypy.org/wiki/PageHandlers), which gives you all kinds of RESTful options.

To answer your second question, you want to define and expose a default method: class getOrders(Object): def default(account, type): ... default. Exposed = True using this method, getOrders/x/y would map to default(account='x', type='y'). Like someone else said, it's not great, but it gets the job done.As far as RESTful applications goes, I'm pretty sure the default page handler will work for such an application.

AFAIK, for CherryPy, no such thing is available / needed. As default dispatcher is RESTful as it is and You can easily write your own, method-based for APIs and as CP is not bound to any ORM, what would You need framework for?

– hyperboreean Apr 27 '10 at 8:34 Well, I guess something like class Account(object): def type(self): return "hello" type. Exposed = True class Orders(object): account = Account() class Root(object): orders = Orders() cherrypy. Quickstart(Root))?

(EDIT: dah, no code formatting in comment) Follow tutorial... – Almad Apr 27 '10 at 19:39.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions