Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/vault.py
# Copyright (C) 2015-2021 The Software Heritage developers | # Copyright (C) 2015-2021 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 typing import Any, Dict | from typing import Any, Dict | ||||
from django.http import HttpResponse | from django.http import HttpResponse | ||||
from django.shortcuts import redirect | from django.shortcuts import redirect | ||||
from swh.model.hashutil import hash_to_hex | |||||
from swh.model.identifiers import CoreSWHID, ObjectType | from swh.model.identifiers import CoreSWHID, ObjectType | ||||
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.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.common import archive, query | from swh.web.common import archive, query | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import reverse | ||||
Show All 20 Lines | elif request.method == "POST": | ||||
bundle_type, | bundle_type, | ||||
swhid, | swhid, | ||||
email, | email, | ||||
notfound_msg=f"{swhid} not found.", | notfound_msg=f"{swhid} not found.", | ||||
request=request, | request=request, | ||||
) | ) | ||||
def _vault_response(vault_response: Dict[str, Any]) -> Dict[str, Any]: | def _vault_response( | ||||
return { | vault_response: Dict[str, Any], add_legacy_items: bool | ||||
) -> Dict[str, Any]: | |||||
d = { | |||||
"fetch_url": vault_response["fetch_url"], | "fetch_url": vault_response["fetch_url"], | ||||
"progress_message": vault_response["progress_msg"], | "progress_message": vault_response["progress_msg"], | ||||
"id": vault_response["task_id"], | "id": vault_response["task_id"], | ||||
"status": vault_response["task_status"], | "status": vault_response["task_status"], | ||||
"swhid": str(vault_response["swhid"]), | "swhid": vault_response["swhid"], | ||||
} | } | ||||
if add_legacy_items: | |||||
swhid = CoreSWHID.from_string(vault_response["swhid"]) | |||||
d["obj_type"] = swhid.object_type.name.lower() | |||||
d["obj_id"] = hash_to_hex(swhid.object_id) | |||||
return d | |||||
###################################################### | ###################################################### | ||||
# Flat bundles | # Flat bundles | ||||
@api_route( | @api_route( | ||||
f"/vault/flat/(?P<swhid>{SWHID_RE})/", | f"/vault/flat/(?P<swhid>{SWHID_RE})/", | ||||
"api-1-vault-cook-flat", | "api-1-vault-cook-flat", | ||||
▲ Show 20 Lines • Show All 45 Lines • ▼ Show 20 Lines | .. http:post:: /api/1/vault/flat/(swhid)/ | ||||
(in case of POST) | (in case of POST) | ||||
""" | """ | ||||
swhid = CoreSWHID.from_string(swhid) | swhid = CoreSWHID.from_string(swhid) | ||||
if swhid.object_type == ObjectType.DIRECTORY: | if swhid.object_type == ObjectType.DIRECTORY: | ||||
res = _dispatch_cook_progress(request, "flat", swhid) | res = _dispatch_cook_progress(request, "flat", swhid) | ||||
res["fetch_url"] = reverse( | res["fetch_url"] = reverse( | ||||
"api-1-vault-fetch-flat", url_args={"swhid": str(swhid)}, request=request, | "api-1-vault-fetch-flat", url_args={"swhid": str(swhid)}, request=request, | ||||
) | ) | ||||
return _vault_response(res) | return _vault_response(res, add_legacy_items=False) | ||||
elif swhid.object_type == ObjectType.CONTENT: | elif swhid.object_type == ObjectType.CONTENT: | ||||
raise BadInputExc( | raise BadInputExc( | ||||
"Content objects do not need to be cooked, " | "Content objects do not need to be cooked, " | ||||
"use `/api/1/content/raw/` instead." | "use `/api/1/content/raw/` instead." | ||||
) | ) | ||||
elif swhid.object_type == ObjectType.REVISION: | elif swhid.object_type == ObjectType.REVISION: | ||||
# TODO: support revisions too? (the vault allows it) | # TODO: support revisions too? (the vault allows it) | ||||
raise BadInputExc( | raise BadInputExc( | ||||
Show All 24 Lines | _, obj_id = query.parse_hash_with_algorithms_or_throws( | ||||
dir_id, ["sha1"], "Only sha1_git is supported." | dir_id, ["sha1"], "Only sha1_git is supported." | ||||
) | ) | ||||
swhid = f"swh:1:dir:{obj_id.hex()}" | swhid = f"swh:1:dir:{obj_id.hex()}" | ||||
res = _dispatch_cook_progress(request, "flat", CoreSWHID.from_string(swhid)) | res = _dispatch_cook_progress(request, "flat", CoreSWHID.from_string(swhid)) | ||||
res["fetch_url"] = reverse( | res["fetch_url"] = reverse( | ||||
"api-1-vault-fetch-flat", url_args={"swhid": swhid}, request=request, | "api-1-vault-fetch-flat", url_args={"swhid": swhid}, request=request, | ||||
) | ) | ||||
return _vault_response(res) | return _vault_response(res, add_legacy_items=True) | ||||
@api_route( | @api_route( | ||||
f"/vault/flat/(?P<swhid>{SWHID_RE})/raw/", "api-1-vault-fetch-flat", | f"/vault/flat/(?P<swhid>{SWHID_RE})/raw/", "api-1-vault-fetch-flat", | ||||
) | ) | ||||
@api_doc("/vault/flat/raw/") | @api_doc("/vault/flat/raw/") | ||||
def api_vault_fetch_flat(request, swhid): | def api_vault_fetch_flat(request, swhid): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 107 Lines • ▼ Show 20 Lines | def api_vault_cook_gitfast(request, swhid): | ||||
swhid = CoreSWHID.from_string(swhid) | swhid = CoreSWHID.from_string(swhid) | ||||
if swhid.object_type == ObjectType.REVISION: | if swhid.object_type == ObjectType.REVISION: | ||||
res = _dispatch_cook_progress(request, "gitfast", swhid) | res = _dispatch_cook_progress(request, "gitfast", swhid) | ||||
res["fetch_url"] = reverse( | res["fetch_url"] = reverse( | ||||
"api-1-vault-fetch-gitfast", | "api-1-vault-fetch-gitfast", | ||||
url_args={"swhid": str(swhid)}, | url_args={"swhid": str(swhid)}, | ||||
request=request, | request=request, | ||||
) | ) | ||||
return _vault_response(res) | return _vault_response(res, add_legacy_items=False) | ||||
elif swhid.object_type == ObjectType.CONTENT: | elif swhid.object_type == ObjectType.CONTENT: | ||||
raise BadInputExc( | raise BadInputExc( | ||||
"Content objects do not need to be cooked, " | "Content objects do not need to be cooked, " | ||||
"use `/api/1/content/raw/` instead." | "use `/api/1/content/raw/` instead." | ||||
) | ) | ||||
elif swhid.object_type == ObjectType.DIRECTORY: | elif swhid.object_type == ObjectType.DIRECTORY: | ||||
raise BadInputExc( | raise BadInputExc( | ||||
"Only revisions can be cooked as 'gitfast' bundles. " | "Only revisions can be cooked as 'gitfast' bundles. " | ||||
Show All 23 Lines | _, obj_id = query.parse_hash_with_algorithms_or_throws( | ||||
rev_id, ["sha1"], "Only sha1_git is supported." | rev_id, ["sha1"], "Only sha1_git is supported." | ||||
) | ) | ||||
swhid = f"swh:1:rev:{obj_id.hex()}" | swhid = f"swh:1:rev:{obj_id.hex()}" | ||||
res = _dispatch_cook_progress(request, "gitfast", CoreSWHID.from_string(swhid)) | res = _dispatch_cook_progress(request, "gitfast", CoreSWHID.from_string(swhid)) | ||||
res["fetch_url"] = reverse( | res["fetch_url"] = reverse( | ||||
"api-1-vault-fetch-gitfast", url_args={"swhid": swhid}, request=request, | "api-1-vault-fetch-gitfast", url_args={"swhid": swhid}, request=request, | ||||
) | ) | ||||
return _vault_response(res) | return _vault_response(res, add_legacy_items=True) | ||||
@api_route( | @api_route( | ||||
f"/vault/gitfast/(?P<swhid>{SWHID_RE})/raw/", "api-1-vault-fetch-gitfast", | f"/vault/gitfast/(?P<swhid>{SWHID_RE})/raw/", "api-1-vault-fetch-gitfast", | ||||
) | ) | ||||
@api_doc("/vault/gitfast/raw/") | @api_doc("/vault/gitfast/raw/") | ||||
def api_vault_fetch_revision_gitfast(request, swhid): | def api_vault_fetch_revision_gitfast(request, swhid): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | def api_vault_cook_git_bare(request, swhid): | ||||
swhid = CoreSWHID.from_string(swhid) | swhid = CoreSWHID.from_string(swhid) | ||||
if swhid.object_type == ObjectType.REVISION: | if swhid.object_type == ObjectType.REVISION: | ||||
res = _dispatch_cook_progress(request, "git_bare", swhid) | res = _dispatch_cook_progress(request, "git_bare", swhid) | ||||
res["fetch_url"] = reverse( | res["fetch_url"] = reverse( | ||||
"api-1-vault-fetch-git-bare", | "api-1-vault-fetch-git-bare", | ||||
url_args={"swhid": str(swhid)}, | url_args={"swhid": str(swhid)}, | ||||
request=request, | request=request, | ||||
) | ) | ||||
return _vault_response(res) | return _vault_response(res, add_legacy_items=False) | ||||
elif swhid.object_type == ObjectType.CONTENT: | elif swhid.object_type == ObjectType.CONTENT: | ||||
raise BadInputExc( | raise BadInputExc( | ||||
"Content objects do not need to be cooked, " | "Content objects do not need to be cooked, " | ||||
"use `/api/1/content/raw/` instead." | "use `/api/1/content/raw/` instead." | ||||
) | ) | ||||
elif swhid.object_type == ObjectType.DIRECTORY: | elif swhid.object_type == ObjectType.DIRECTORY: | ||||
raise BadInputExc( | raise BadInputExc( | ||||
"Only revisions can be cooked as 'git-bare' bundles. " | "Only revisions can be cooked as 'git-bare' bundles. " | ||||
▲ Show 20 Lines • Show All 41 Lines • Show Last 20 Lines |