Skip to content

Instantly share code, notes, and snippets.

@gtke
Last active August 29, 2015 14:23

Revisions

  1. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,4 @@
    @app.route('/', methods=['POST'])
    @json
    @authenticate
    def foo():
  2. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -3,6 +3,7 @@
    def foo():
    do_stuff()


    # Decorators
    def json(f):
    @functools.wraps(f)
  3. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion decorator.py
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,6 @@ def foo():
    do_stuff()

    # Decorators

    def json(f):
    @functools.wraps(f)
    def wrapped(*args, **kwargs):
  4. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -6,10 +6,6 @@ def foo():
    # Decorators

    def json(f):
    """
    Returns a JSON response to the client
    f: route function that returns a dict
    """
    @functools.wraps(f)
    def wrapped(*args, **kwargs):
    response = f(*args, **kwargs)
  5. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 0 additions and 2 deletions.
    2 changes: 0 additions & 2 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -29,5 +29,3 @@ def wrapped(*args, **kwargs):
    return f(*args, **kwargs)
    return wrapped
    return decorator


  6. @gtkesh gtkesh revised this gist Jun 23, 2015. 1 changed file with 0 additions and 4 deletions.
    4 changes: 0 additions & 4 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -1,12 +1,8 @@


    @json
    @authenticate
    def foo():
    do_stuff()



    # Decorators

    def json(f):
  7. @gtkesh gtkesh created this gist Jun 23, 2015.
    37 changes: 37 additions & 0 deletions decorator.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,37 @@


    @json
    @authenticate
    def foo():
    do_stuff()



    # Decorators

    def json(f):
    """
    Returns a JSON response to the client
    f: route function that returns a dict
    """
    @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