Skip to content

Instantly share code, notes, and snippets.

@rgrinberg
Last active December 21, 2015 20:18
Show Gist options
  • Save rgrinberg/6359923 to your computer and use it in GitHub Desktop.
Save rgrinberg/6359923 to your computer and use it in GitHub Desktop.
webapp2 handler as a decorator. when defining a full class seems "heavy"
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