Last active
December 21, 2015 20:18
-
-
Save rgrinberg/6359923 to your computer and use it in GitHub Desktop.
webapp2 handler as a decorator. when defining a full class seems "heavy"
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
def webapp2_handler(method): | |
allowed_methods = set(['GET', 'POST', 'DELETE', 'PUT']) | |
if method not in allowed_methods: | |
raise ValueError("%s is an invalid HTTP method" % method) | |
def wrap(f): | |
return type('', (webapp2.RequestHandler,), { | |
method.lower(): f | |
}) | |
return wrap | |
# example | |
@webapp2_handler(method='DELETE') | |
def test_handler(self, digits): | |
self.response.write("given digits: %s" % digits) | |
APPLICATION = webapp2.WSGIApplication([ ('/test/(\d+)', test_handler), ], debug=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment