سؤال

I understand and have gotten RESTful routes working in my application using this guide http://docs.cherrypy.org/dev/progguide/REST.html

Does anyone know how to add a second RESTful resource nested within a first?

I'm expecting my code to look something like this, but I can't get it to work

import cherrypy

class Pets:
  exposed = True

  def GET(self, personID, petID):
    pass # GET /people/123/pets/333 return pet

  def POST(self, personID):
    pass # POST /people/123/pets create pet

class People:
  pets = Pets()

  exposed = True

  def GET(self, personID):
    pass # GET /people/123 return person

  def POST(self):
    pass # POST /people create person


config = {
  '/people': {
    'request.dispatch': cherrypy.dispatch.MethodDispatcher()
  }
}

cherrypy.tree.mount(.., '/', config)
هل كانت مفيدة؟

المحلول

See the help docs for cherrypy.popargs. It pops path components, and supplies them as keyword arguments to the next handler. In this case, use it as a decorator on the people resource, and attach a pets resource to the people resource.

@cherrypy.popargs('petID')
class Pets:
    ...
@cherrypy.popargs('personID')
class People:
    ...
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top