2009-10-17 6 views
10

Je dois configurer une URL de type RESTful qui prennent en charge le système d'URL suivant:URL dynamique avec cherrypy MethodDispatcher

  • /parent/
  • /parent/1
  • /parent/1/enfants
  • /parent/1/chidren/1

Je veux utiliser le MethodDispatcher afin que chacun des ci-dessus peut avoir GET/POST/PUT/DELETE fonctions. Je l'ai travaillé pour le premier et le second, mais je n'arrive pas à comprendre comment configurer le répartiteur pour la partie enfants. J'ai le livre, mais il couvre à peine cela et je ne trouve aucun échantillon en ligne.

Voici comment j'ai configuré MethodDispatcher actuellement.

root = Root() 
conf = {'/' : {'request.dispatch': cherrypy.dispatch.MethodDispatcher()}}  

cherrypy.quickstart(root, '/parent', config=conf) 

Toute aide serait appréciée.

Répondre

9

http://tools.cherrypy.org/wiki/RestfulDispatch peut être ce que vous cherchez. Dans CherryPy 3.2, il y aura une nouvelle méthode _cp_dispatch que vous pouvez utiliser dans votre arborescence d'objets pour faire la même chose, ou même modifier la traversée comme elle arrive, un peu dans le sens de Quichotte. _q_lookup et _q_resolve. Voir https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32#!dynamic-dispatch-by-controllers

+1

Parfait. C'est exactement ce dont j'ai besoin, mais je n'ai pas pu le trouver parce que j'étais concentré sur MethodDispatcher dans mon Google. Merci. –

+2

Le lien vers le lien Répartition dynamique par les contrôleurs a été modifié. Vous pouvez le trouver ici, https://bitbucket.org/cherrypy/cherrypy/wiki/WhatsNewIn32 – elarson

+0

Merci d'avoir signalé la méthode _cp_dispatch! J'ai trouvé que c'était une solution élégante à un problème similaire dans la structure de mon application. –

2
#!/usr/bin/env python 
import cherrypy 

class Items(object): 
    exposed = True 
    def __init__(self): 
     pass 

    # this line will map the first argument after/to the 'id' parameter 
    # for example, a GET request to the url: 
    # http://localhost:8000/items/ 
    # will call GET with id=None 
    # and a GET request like this one: http://localhost:8000/items/1 
    # will call GET with id=1 
    # you can map several arguments using: 
    # @cherrypy.propargs('arg1', 'arg2', 'argn') 
    # def GET(self, arg1, arg2, argn) 
    @cherrypy.popargs('id') 
    def GET(self, id=None): 
     print "id: ", id 
     if not id: 
      # return all items 
     else: 
      # return only the item with id = id 

    # HTML5 
    def OPTIONS(self):              
     cherrypy.response.headers['Access-Control-Allow-Credentials'] = True 
     cherrypy.response.headers['Access-Control-Allow-Origin'] = cherrypy.request.headers['ORIGIN'] 
     cherrypy.response.headers['Access-Control-Allow-Methods'] = 'GET, PUT, DELETE'          
     cherrypy.response.headers['Access-Control-Allow-Headers'] = cherrypy.request.headers['ACCESS-CONTROL-REQUEST-HEADERS'] 

class Root(object): 
    pass 

root = Root() 
root.items = Items() 

conf = { 
    'global': { 
     'server.socket_host': '0.0.0.0', 
     'server.socket_port': 8000, 
    }, 
    '/': { 
     'request.dispatch': cherrypy.dispatch.MethodDispatcher(), 
    }, 
} 
cherrypy.quickstart(root, '/', conf)