mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2024-12-24 18:34:18 +00:00
combine content_negotiation related decorators
This commit is contained in:
parent
cb830a6f9b
commit
9272150212
@ -65,52 +65,34 @@ def background(f):
|
||||
return wrapped
|
||||
|
||||
|
||||
def consumes(mime_type: str, *_mime_types: str):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
provided = request.mimetype
|
||||
consumeables = {mime_type, *_mime_types}
|
||||
if provided not in consumeables:
|
||||
raise NotAcceptable()
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
return decorator
|
||||
|
||||
|
||||
def produces(mime_type: str, *_mime_types: str):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
accepted = {*request.accept_mimetypes.values()}
|
||||
produceables = {mime_type, *_mime_types}
|
||||
if len(produceables & accepted) == 0:
|
||||
raise NotAcceptable()
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
return decorator
|
||||
|
||||
|
||||
def content_negotiation(
|
||||
produces: Union[str, List[str]],
|
||||
consumes: Union[str, List[str]]
|
||||
produces: Union[str, List[str], None] = None,
|
||||
consumes: Union[str, List[str], None] = None
|
||||
):
|
||||
def decorator(f):
|
||||
@wraps(f)
|
||||
def decorated_function(*args, **kwargs):
|
||||
provided = request.mimetype
|
||||
if isinstance(consumes, str):
|
||||
if consumes is None:
|
||||
consumeables = None
|
||||
elif isinstance(consumes, str):
|
||||
consumeables = {consumes}
|
||||
else:
|
||||
elif isinstance(consumes, list) and all(isinstance(x, str) for x in consumes):
|
||||
consumeables = {*consumes}
|
||||
accepted = {*request.accept_mimetypes.values()}
|
||||
if isinstance(produces, str):
|
||||
produceables = {produces}
|
||||
else:
|
||||
raise TypeError()
|
||||
accepted = {*request.accept_mimetypes.values()}
|
||||
if produces is None:
|
||||
produceables = None
|
||||
elif isinstance(produces, str):
|
||||
produceables = {produces}
|
||||
elif isinstance(produces, list) and all(isinstance(x, str) for x in produces):
|
||||
produceables = {*produces}
|
||||
if len(produceables & accepted) == 0:
|
||||
else:
|
||||
raise TypeError()
|
||||
if produceables is not None and len(produceables & accepted) == 0:
|
||||
raise NotAcceptable()
|
||||
if provided not in consumeables:
|
||||
if consumeables is not None and provided not in consumeables:
|
||||
raise NotAcceptable()
|
||||
return f(*args, **kwargs)
|
||||
return decorated_function
|
||||
|
Loading…
Reference in New Issue
Block a user