def response_filter(view):
@wraps(view)
async def wrapper(*args, **kwargs):
request = args[1]
resp = view(*args, **kwargs)
from inspect import isawaitable
if isawaitable(resp):
resp = await resp
if isinstance(resp, HTTPResponse):
return resp
endpoint = _path_to_endpoint(request.uri_template)
method = request.method
if method == 'HEAD':
method = 'GET'
filter = filters.get((endpoint, method), None)
if not filter:
return resp
headers = None
status = None
if isinstance(resp, tuple):
resp, status, headers = unpack(resp)
if len(filter) == 1:
if six.PY3:
status = list(filter.keys())[0]
else:
status = filter.keys()[0]
schemas = filter.get(status)
if not schemas:
# return resp, status, headers
raise ServerError('`%d` is not a defined status code.' % status, 500)
resp, errors = normalize(schemas['schema'], resp)
if schemas['headers']:
headers, header_errors = normalize(
{'properties': schemas['headers']}, headers)
errors.extend(header_errors)
if errors:
raise ServerError('Expectation Failed', 500)
return response.json(
resp,
status=status,
headers=headers,
)
return wrapper
评论列表
文章目录