Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/apiresponse.py
Show All 12 Lines | |||||
from swh.storage.exc import StorageDBError, StorageAPIError | from swh.storage.exc import StorageDBError, StorageAPIError | ||||
from swh.web.api import utils | from swh.web.api import utils | ||||
from swh.web.common.exc import NotFoundExc, ForbiddenExc, BadInputExc | from swh.web.common.exc import NotFoundExc, ForbiddenExc, BadInputExc | ||||
from swh.web.common.utils import shorten_path, gen_path_info | from swh.web.common.utils import shorten_path, gen_path_info | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
def compute_link_header(request, rv, options): | def compute_link_header(rv, options): | ||||
"""Add Link header in returned value results. | """Add Link header in returned value results. | ||||
Args: | Args: | ||||
request: a DRF Request object | request: a DRF Request object | ||||
rv (dict): dictionary with keys: | rv (dict): dictionary with keys: | ||||
- headers: potential headers with 'link-next' and 'link-prev' | - headers: potential headers with 'link-next' and 'link-prev' | ||||
keys | keys | ||||
- results: containing the result to return | - results: containing the result to return | ||||
options (dict): the initial dict to update with result if any | options (dict): the initial dict to update with result if any | ||||
Returns: | Returns: | ||||
dict: dictionary with optional keys 'link-next' and 'link-prev' | dict: dictionary with optional keys 'link-next' and 'link-prev' | ||||
""" | """ | ||||
link_headers = [] | link_headers = [] | ||||
if 'headers' not in rv: | if 'headers' not in rv: | ||||
return {} | return {} | ||||
rv_headers = rv['headers'] | rv_headers = rv['headers'] | ||||
if 'link-next' in rv_headers: | if 'link-next' in rv_headers: | ||||
link_headers.append('<%s>; rel="next"' % ( | link_headers.append('<%s>; rel="next"' % rv_headers['link-next']) | ||||
request.build_absolute_uri(rv_headers['link-next']))) | |||||
if 'link-prev' in rv_headers: | if 'link-prev' in rv_headers: | ||||
link_headers.append('<%s>; rel="previous"' % ( | link_headers.append('<%s>; rel="previous"' % rv_headers['link-prev']) | ||||
request.build_absolute_uri(rv_headers['link-prev']))) | |||||
if link_headers: | if link_headers: | ||||
link_header_str = ','.join(link_headers) | link_header_str = ','.join(link_headers) | ||||
headers = options.get('headers', {}) | headers = options.get('headers', {}) | ||||
headers.update({ | headers.update({ | ||||
'Link': link_header_str | 'Link': link_header_str | ||||
}) | }) | ||||
return headers | return headers | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | Args: | ||||
doc_data: documentation data for HTML response | doc_data: documentation data for HTML response | ||||
options: optional data that can be used to generate the response | options: optional data that can be used to generate the response | ||||
Returns: | Returns: | ||||
a DRF Response a object | a DRF Response a object | ||||
""" | """ | ||||
if data: | if data: | ||||
options['headers'] = compute_link_header(request, data, options) | options['headers'] = compute_link_header(data, options) | ||||
data = transform(data) | data = transform(data) | ||||
data = filter_by_fields(request, data) | data = filter_by_fields(request, data) | ||||
doc_env = doc_data | doc_env = doc_data | ||||
headers = {} | headers = {} | ||||
if 'headers' in options: | if 'headers' in options: | ||||
doc_env['headers_data'] = options['headers'] | doc_env['headers_data'] = options['headers'] | ||||
headers = options['headers'] | headers = options['headers'] | ||||
Show All 9 Lines | def make_api_response(request, data, doc_data={}, options={}): | ||||
# related ones and inform DRF that we request HTML template rendering | # related ones and inform DRF that we request HTML template rendering | ||||
if request.accepted_media_type == 'text/html': | if request.accepted_media_type == 'text/html': | ||||
if data: | if data: | ||||
data = json.dumps(data, sort_keys=True, | data = json.dumps(data, sort_keys=True, | ||||
indent=4, | indent=4, | ||||
separators=(',', ': ')) | separators=(',', ': ')) | ||||
doc_env['response_data'] = data | doc_env['response_data'] = data | ||||
doc_env['request'] = { | |||||
'path': request.path, | |||||
'method': request.method, | |||||
'absolute_uri': request.build_absolute_uri(), | |||||
} | |||||
doc_env['heading'] = shorten_path(str(request.path)) | doc_env['heading'] = shorten_path(str(request.path)) | ||||
if 'route' in doc_env: | if 'route' in doc_env: | ||||
doc_env['endpoint_path'] = gen_path_info(doc_env['route']) | doc_env['endpoint_path'] = gen_path_info(doc_env['route']) | ||||
response_args['data'] = doc_env | response_args['data'] = doc_env | ||||
response_args['template_name'] = 'api/apidoc.html' | response_args['template_name'] = 'api/apidoc.html' | ||||
▲ Show 20 Lines • Show All 43 Lines • Show Last 20 Lines |