Skip to content

Instantly share code, notes, and snippets.

@gtke
Last active August 29, 2015 14:23
Show Gist options
  • Save gtke/2840d57f3464d971b8df to your computer and use it in GitHub Desktop.
Save gtke/2840d57f3464d971b8df to your computer and use it in GitHub Desktop.
Decorator example
@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
Copy link

var json = function(f){
  return function(){
    return JSON.stringify(f.apply(this,arguments));
  };
};

var authenticate = function(f){
  return function(){
    .......;
  };
};

var foo = authenticate(json(function(){
  return do_stuff();
}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment