Move param_required into the decorators module

This commit is contained in:
Joseph Schorr 2017-07-20 11:41:19 -04:00
parent 98e2ccf74d
commit 5d69fc2aa3
3 changed files with 20 additions and 23 deletions

View file

@ -1,7 +1,7 @@
""" Various decorators for endpoint and API handlers. """
from functools import wraps
from flask import abort
from flask import abort, request, make_response
import features
@ -35,6 +35,22 @@ def parse_repository_name(include_tag=False,
return inner
def param_required(param_name, allow_body=False):
""" Marks a route as requiring a parameter with the given name to exist in the request's arguments
or (if allow_body=True) in its body values. If the parameter is not present, the request will
fail with a 400.
"""
def wrapper(wrapped):
@wraps(wrapped)
def decorated(*args, **kwargs):
if param_name not in request.args:
if not allow_body or param_name not in request.values:
abort(make_response('Required param: %s' % param_name, 400))
return wrapped(*args, **kwargs)
return decorated
return wrapper
def anon_allowed(func):
""" Marks a method to allow anonymous access where it would otherwise be disallowed. """
func.__anon_allowed = True