Last active
August 29, 2015 14:23
-
-
Save gtke/2840d57f3464d971b8df to your computer and use it in GitHub Desktop.
Decorator example
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
@app.route('/', methods=['POST']) | |
@json | |
@authenticate | |
def foo(): | |
do_stuff() | |
# Decorators | |
def json(f): | |
@functools.wraps(f) | |
def wrapped(*args, **kwargs): | |
response = f(*args, **kwargs) | |
return jsonify(response) | |
return wrapped | |
def authenticate(auth_callable): | |
def decorator(f): | |
@functools.wraps(f) | |
def wrapped(*args, **kwargs): | |
authentication = auth_callable(request) | |
if not authentication.valid: | |
reason = authentication.reason or "Unauthorized" | |
raise errors.ValidationError(reason, 403) | |
return f(*args, **kwargs) | |
return wrapped | |
return decorator |
safareli
commented
Jun 24, 2015
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment