Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/snapshot.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
from swh.web.api import utils | |||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.api.utils import enrich_snapshot | |||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
@api_route(r'/snapshot/(?P<snapshot_id>[0-9a-f]+)/', 'api-1-snapshot', | @api_route(r'/snapshot/(?P<snapshot_id>[0-9a-f]+)/', 'api-1-snapshot', | ||||
checksum_args=['snapshot_id']) | checksum_args=['snapshot_id']) | ||||
@api_doc('/snapshot/') | @api_doc('/snapshot/') | ||||
@format_docstring() | @format_docstring() | ||||
def api_snapshot(request, snapshot_id): | def api_snapshot(request, snapshot_id): | ||||
▲ Show 20 Lines • Show All 43 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/snapshot/(snapshot_id)/ | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`snapshot/6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a/` | :swh_web_api:`snapshot/6a3a2cf0b2b90ce7ae1cf0a221ed68035b686f5a/` | ||||
""" | """ | ||||
def _resolve_alias(snapshot, branch): | |||||
while branch and branch['target_type'] == 'alias': | |||||
if branch['target'] in snapshot['branches']: | |||||
branch = snapshot['branches'][branch['target']] | |||||
else: | |||||
snp = service.lookup_snapshot( | |||||
snapshot['id'], branches_from=branch['target'], | |||||
branches_count=1) | |||||
if snp and branch['target'] in snp['branches']: | |||||
branch = snp['branches'][branch['target']] | |||||
else: | |||||
branch = None | |||||
return branch | |||||
def _enrich_snapshot(snapshot): | |||||
s = snapshot.copy() | |||||
if 'branches' in s: | |||||
s['branches'] = { | |||||
k: utils.enrich_object(v) if v else None | |||||
for k, v in s['branches'].items() | |||||
} | |||||
for k, v in s['branches'].items(): | |||||
if v and v['target_type'] == 'alias': | |||||
branch = _resolve_alias(snapshot, v) | |||||
if branch: | |||||
branch = utils.enrich_object(branch) | |||||
v['target_url'] = branch['target_url'] | |||||
return s | |||||
snapshot_content_max_size = get_config()['snapshot_content_max_size'] | snapshot_content_max_size = get_config()['snapshot_content_max_size'] | ||||
branches_from = request.GET.get('branches_from', '') | branches_from = request.GET.get('branches_from', '') | ||||
branches_count = int(request.GET.get('branches_count', | branches_count = int(request.GET.get('branches_count', | ||||
snapshot_content_max_size)) | snapshot_content_max_size)) | ||||
target_types = request.GET.get('target_types', None) | target_types = request.GET.get('target_types', None) | ||||
target_types = target_types.split(',') if target_types else None | target_types = target_types.split(',') if target_types else None | ||||
results = api_lookup( | results = api_lookup( | ||||
service.lookup_snapshot, snapshot_id, branches_from, | service.lookup_snapshot, snapshot_id, branches_from, | ||||
branches_count, target_types, | branches_count, target_types, | ||||
notfound_msg='Snapshot with id {} not found.'.format(snapshot_id), | notfound_msg='Snapshot with id {} not found.'.format(snapshot_id), | ||||
enrich_fn=_enrich_snapshot) | enrich_fn=enrich_snapshot, | ||||
request=request) | |||||
response = {'results': results, 'headers': {}} | response = {'results': results, 'headers': {}} | ||||
if results['next_branch'] is not None: | if results['next_branch'] is not None: | ||||
response['headers']['link-next'] = \ | response['headers']['link-next'] = reverse( | ||||
reverse('api-1-snapshot', | 'api-1-snapshot', | ||||
url_args={'snapshot_id': snapshot_id}, | url_args={'snapshot_id': snapshot_id}, | ||||
query_params={'branches_from': results['next_branch'], | query_params={'branches_from': results['next_branch'], | ||||
'branches_count': branches_count, | 'branches_count': branches_count, | ||||
'target_types': target_types}) | 'target_types': target_types}, | ||||
request=request) | |||||
return response | return response |