Last active
August 29, 2015 14:11
-
-
Save akash0x53/49b80559bdf1384c4566 to your computer and use it in GitHub Desktop.
Python decorator with argument to check HTTP request method.
This file contains hidden or 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
class CheckHttpMethod(object): | |
""" | |
Decorator check whether HTTP request is as per your need, | |
if not it redirects to '/'. | |
This is a decorator with *mandatory* argument, a HTTP method | |
you want to check. | |
>>>@CheckHttpMethod('POST') | |
...def handler(*args, *kwargs): | |
... pass | |
""" | |
def __init__(self, method=None): | |
assert method, None | |
self.method = method | |
def __call__(self, fn): | |
def __check_method(method, *args, **kwargs): | |
if method != self.method: | |
return web.seeother('/') | |
return fn(method, *args, **kwargs) | |
return __check_method |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment