Created
November 14, 2018 15:27
-
-
Save Giancarlos/f0610987ccef296a0c9e79a6e124d1cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
import collections | |
import cherrypy | |
class SelectedMethod: | |
""" | |
Descriptor allowing a series of handler methods to satisfy | |
a variety of behaviors based on the request method. | |
Works similar to the MethodDispatcher, but doesn't | |
require replacing the whole dispatch mechanism, so | |
may be deployed selectively. | |
First, decorate your method using SelectedMethod:: | |
class App: | |
@SelectedMethod | |
def index(self): | |
return "Hello World" | |
Then, add separate handlers for specific methods:: | |
@index.GET | |
def get(self): | |
return "Your method was GET" | |
@index.POST | |
def delete(self): | |
db.delete(self.id) | |
The first handler passed will always handle any methods | |
not specified. The app may wish to | |
raise 405 in this case:: | |
@SelectedMethod | |
def index(self): | |
raise cherrypy.HTTPError("405 Method Not Allowed") | |
""" | |
__isabstractmethod__ = False | |
def __init__(self, orig): | |
self.methods = collections.defaultdict(lambda: orig) | |
def __get__(self, obj, type=None): | |
method = self.methods[cherrypy.request.method] | |
bound = functools.partial(method, obj) | |
bound._cp_config = True | |
bound.exposed = True | |
return bound | |
def __getattr__(self, method): | |
""" | |
Return a decorator suitable for wrapping another | |
""" | |
def wrapper(func): | |
self.methods[method] = func | |
return func | |
return wrapper | |
class People(object): | |
@SelectedMethod | |
def index(self): | |
return "Default Index" | |
@index.GET | |
def get(self): | |
return "GET" | |
@index.POST | |
def post(self): | |
return "POST" | |
class WebUi(object): | |
def __init__(self): | |
self.people = People() | |
@cherrypy.expose() | |
def index(self): | |
raise cherrypy.HTTPRedirect('/ui/people/') | |
class WebRoot(object): | |
def __init__(self): | |
# Front-end API end-points loaded from '/web/routes/ui.py' | |
self.ui = WebUi() | |
@cherrypy.expose() | |
def index(self): | |
raise cherrypy.HTTPRedirect('/ui/') | |
conf = {} # For purposes of this code sample let's just assume this is properly defined with all the rules it needs.... | |
cherrypy.tree.mount(WebRoot(), '/', conf) | |
cherrypy.engine.start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment