Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/snapshot.py
# Copyright (C) 2018-2022 The Software Heritage developers | # Copyright (C) 2018-2022 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 rest_framework.request import Request | from rest_framework.request import Request | ||||
from swh.web.api.apidoc import api_doc, format_docstring | from swh.web.api.apidoc import api_doc, format_docstring | ||||
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.utils import enrich_snapshot | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.config import get_config | from swh.web.config import get_config | ||||
from swh.web.utils import archive, reverse | from swh.web.utils import archive, graphql, reverse | ||||
@api_route( | @api_route( | ||||
r"/snapshot/(?P<snapshot_id>[0-9a-f]+)/", | r"/snapshot/(?P<snapshot_id>[0-9a-f]+)/", | ||||
"api-1-snapshot", | "api-1-snapshot", | ||||
checksum_args=["snapshot_id"], | checksum_args=["snapshot_id"], | ||||
) | ) | ||||
@api_doc("/snapshot/", category="Archive") | @api_doc("/snapshot/", category="Archive") | ||||
▲ Show 20 Lines • Show All 42 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/` | ||||
""" | """ | ||||
snapshot_content_max_size = get_config()["snapshot_content_max_size"] | query = """ | ||||
query GetSnapshot($swhid: SWHID!) { | |||||
branches_from = request.GET.get("branches_from", "") | snapshot(swhid: $swhid) { | ||||
branches_count = int(request.GET.get("branches_count", snapshot_content_max_size)) | id | ||||
target_types_str = request.GET.get("target_types", None) | } | ||||
target_types = target_types_str.split(",") if target_types_str else None | } | ||||
""" | |||||
results = api_lookup( | return graphql.get_one( | ||||
archive.lookup_snapshot, | query, | ||||
snapshot_id, | {"swhid": f"swh:1:snp:{snapshot_id}"}, | ||||
branches_from, | query_root="snapshot", | ||||
branches_count, | error_msg=f"Snapshot with sha1_git {snapshot_id} not found.", | ||||
target_types, | |||||
branch_name_exclude_prefix=None, | |||||
notfound_msg="Snapshot with id {} not found.".format(snapshot_id), | |||||
enrich_fn=enrich_snapshot, | |||||
request=request, | request=request, | ||||
) | ) | ||||
response = {"results": results, "headers": {}} | # snapshot_content_max_size = get_config()["snapshot_content_max_size"] | ||||
if results["next_branch"] is not None: | # branches_from = request.GET.get("branches_from", "") | ||||
response["headers"]["link-next"] = reverse( | # branches_count = int(request.GET.get("branches_count", snapshot_content_max_size)) | ||||
"api-1-snapshot", | # target_types_str = request.GET.get("target_types", None) | ||||
url_args={"snapshot_id": snapshot_id}, | # target_types = target_types_str.split(",") if target_types_str else None | ||||
query_params={ | |||||
"branches_from": results["next_branch"], | # results = api_lookup( | ||||
"branches_count": str(branches_count), | # archive.lookup_snapshot, | ||||
"target_types": ",".join(target_types) if target_types else None, | # snapshot_id, | ||||
}, | # branches_from, | ||||
request=request, | # branches_count, | ||||
) | # target_types, | ||||
# branch_name_exclude_prefix=None, | |||||
# notfound_msg="Snapshot with id {} not found.".format(snapshot_id), | |||||
# enrich_fn=enrich_snapshot, | |||||
# request=request, | |||||
# ) | |||||
# response = {"results": results, "headers": {}} | |||||
# if results["next_branch"] is not None: | |||||
# response["headers"]["link-next"] = reverse( | |||||
# "api-1-snapshot", | |||||
# url_args={"snapshot_id": snapshot_id}, | |||||
# query_params={ | |||||
# "branches_from": results["next_branch"], | |||||
# "branches_count": str(branches_count), | |||||
# "target_types": ",".join(target_types) if target_types else None, | |||||
# }, | |||||
# request=request, | |||||
# ) | |||||
return response | # return response |