Changeset View
Changeset View
Standalone View
Standalone View
swh/web/vault/api_views.py
# Copyright (C) 2015-2022 The Software Heritage developers | # Copyright (C) 2015-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 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 rest_framework.request import Request | from rest_framework.request import Request | ||||
from swh.model.hashutil import hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.swhids import CoreSWHID, ObjectType | from swh.model.swhids 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 APIUrls, api_route | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.utils import SWHID_RE, archive, query, reverse | from swh.web.utils import SWHID_RE, archive, query, reverse | ||||
from swh.web.utils.exc import BadInputExc | from swh.web.utils.exc import BadInputExc | ||||
###################################################### | ###################################################### | ||||
# Common | # Common | ||||
Show All 32 Lines | ) -> Dict[str, Any]: | ||||
if add_legacy_items: | if add_legacy_items: | ||||
d["obj_type"] = vault_response["swhid"].object_type.name.lower() | d["obj_type"] = vault_response["swhid"].object_type.name.lower() | ||||
d["obj_id"] = hash_to_hex(vault_response["swhid"].object_id) | d["obj_id"] = hash_to_hex(vault_response["swhid"].object_id) | ||||
return d | return d | ||||
vault_api_urls = APIUrls() | |||||
###################################################### | ###################################################### | ||||
# 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", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
throttle_scope="swh_vault_cooking", | throttle_scope="swh_vault_cooking", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/flat/", category="Batch download") | @api_doc("/vault/flat/", category="Batch download") | ||||
@format_docstring() | @format_docstring() | ||||
def api_vault_cook_flat(request: Request, swhid: str): | def api_vault_cook_flat(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/flat/(swhid)/ | .. http:get:: /api/1/vault/flat/(swhid)/ | ||||
.. http:post:: /api/1/vault/flat/(swhid)/ | .. http:post:: /api/1/vault/flat/(swhid)/ | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
@api_route( | @api_route( | ||||
r"/vault/directory/(?P<dir_id>[0-9a-f]+)/", | r"/vault/directory/(?P<dir_id>[0-9a-f]+)/", | ||||
"api-1-vault-cook-directory", | "api-1-vault-cook-directory", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
checksum_args=["dir_id"], | checksum_args=["dir_id"], | ||||
throttle_scope="swh_vault_cooking", | throttle_scope="swh_vault_cooking", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/directory/", category="Batch download", tags=["deprecated"]) | @api_doc("/vault/directory/", category="Batch download", tags=["deprecated"]) | ||||
@format_docstring() | @format_docstring() | ||||
def api_vault_cook_directory(request: Request, dir_id: str): | def api_vault_cook_directory(request: Request, dir_id: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/directory/(dir_id)/ | .. http:get:: /api/1/vault/directory/(dir_id)/ | ||||
This endpoint was replaced by :http:get:`/api/1/vault/flat/(swhid)/` | This endpoint was replaced by :http:get:`/api/1/vault/flat/(swhid)/` | ||||
Show All 10 Lines | res["fetch_url"] = reverse( | ||||
request=request, | request=request, | ||||
) | ) | ||||
return _vault_response(res, add_legacy_items=True) | return _vault_response(res, add_legacy_items=True) | ||||
@api_route( | @api_route( | ||||
f"/vault/flat/(?P<swhid>{SWHID_RE})/raw/", | f"/vault/flat/(?P<swhid>{SWHID_RE})/raw/", | ||||
"api-1-vault-fetch-flat", | "api-1-vault-fetch-flat", | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/flat/raw/", category="Batch download") | @api_doc("/vault/flat/raw/", category="Batch download") | ||||
def api_vault_fetch_flat(request: Request, swhid: str): | def api_vault_fetch_flat(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/flat/(swhid)/raw/ | .. http:get:: /api/1/vault/flat/(swhid)/raw/ | ||||
Fetch the cooked archive for a flat bundle. | Fetch the cooked archive for a flat bundle. | ||||
Show All 23 Lines | def api_vault_fetch_flat(request: Request, swhid: str): | ||||
) | ) | ||||
return response | return response | ||||
@api_route( | @api_route( | ||||
r"/vault/directory/(?P<dir_id>[0-9a-f]+)/raw/", | r"/vault/directory/(?P<dir_id>[0-9a-f]+)/raw/", | ||||
"api-1-vault-fetch-directory", | "api-1-vault-fetch-directory", | ||||
checksum_args=["dir_id"], | checksum_args=["dir_id"], | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc( | @api_doc( | ||||
"/vault/directory/raw/", category="Batch download", tags=["hidden", "deprecated"] | "/vault/directory/raw/", category="Batch download", tags=["hidden", "deprecated"] | ||||
) | ) | ||||
def api_vault_fetch_directory(request: Request, dir_id: str): | def api_vault_fetch_directory(request: Request, dir_id: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/directory/(dir_id)/raw/ | .. http:get:: /api/1/vault/directory/(dir_id)/raw/ | ||||
Show All 13 Lines | |||||
@api_route( | @api_route( | ||||
f"/vault/gitfast/(?P<swhid>{SWHID_RE})/", | f"/vault/gitfast/(?P<swhid>{SWHID_RE})/", | ||||
"api-1-vault-cook-gitfast", | "api-1-vault-cook-gitfast", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
throttle_scope="swh_vault_cooking", | throttle_scope="swh_vault_cooking", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/gitfast/", category="Batch download") | @api_doc("/vault/gitfast/", category="Batch download") | ||||
@format_docstring() | @format_docstring() | ||||
def api_vault_cook_gitfast(request: Request, swhid: str): | def api_vault_cook_gitfast(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/gitfast/(swhid)/ | .. http:get:: /api/1/vault/gitfast/(swhid)/ | ||||
.. http:post:: /api/1/vault/gitfast/(swhid)/ | .. http:post:: /api/1/vault/gitfast/(swhid)/ | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | |||||
@api_route( | @api_route( | ||||
r"/vault/revision/(?P<rev_id>[0-9a-f]+)/gitfast/", | r"/vault/revision/(?P<rev_id>[0-9a-f]+)/gitfast/", | ||||
"api-1-vault-cook-revision_gitfast", | "api-1-vault-cook-revision_gitfast", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
checksum_args=["rev_id"], | checksum_args=["rev_id"], | ||||
throttle_scope="swh_vault_cooking", | throttle_scope="swh_vault_cooking", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/revision/gitfast/", category="Batch download", tags=["deprecated"]) | @api_doc("/vault/revision/gitfast/", category="Batch download", tags=["deprecated"]) | ||||
@format_docstring() | @format_docstring() | ||||
def api_vault_cook_revision_gitfast(request: Request, rev_id: str): | def api_vault_cook_revision_gitfast(request: Request, rev_id: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/revision/(rev_id)/gitfast/ | .. http:get:: /api/1/vault/revision/(rev_id)/gitfast/ | ||||
This endpoint was replaced by :http:get:`/api/1/vault/gitfast/(swhid)/` | This endpoint was replaced by :http:get:`/api/1/vault/gitfast/(swhid)/` | ||||
Show All 10 Lines | res["fetch_url"] = reverse( | ||||
request=request, | request=request, | ||||
) | ) | ||||
return _vault_response(res, add_legacy_items=True) | return _vault_response(res, add_legacy_items=True) | ||||
@api_route( | @api_route( | ||||
f"/vault/gitfast/(?P<swhid>{SWHID_RE})/raw/", | f"/vault/gitfast/(?P<swhid>{SWHID_RE})/raw/", | ||||
"api-1-vault-fetch-gitfast", | "api-1-vault-fetch-gitfast", | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/gitfast/raw/", category="Batch download") | @api_doc("/vault/gitfast/raw/", category="Batch download") | ||||
def api_vault_fetch_revision_gitfast(request: Request, swhid: str): | def api_vault_fetch_revision_gitfast(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/gitfast/(swhid)/raw/ | .. http:get:: /api/1/vault/gitfast/(swhid)/raw/ | ||||
Fetch the cooked gitfast archive for a revision. | Fetch the cooked gitfast archive for a revision. | ||||
Show All 23 Lines | def api_vault_fetch_revision_gitfast(request: Request, swhid: str): | ||||
) | ) | ||||
return response | return response | ||||
@api_route( | @api_route( | ||||
r"/vault/revision/(?P<rev_id>[0-9a-f]+)/gitfast/raw/", | r"/vault/revision/(?P<rev_id>[0-9a-f]+)/gitfast/raw/", | ||||
"api-1-vault-fetch-revision_gitfast", | "api-1-vault-fetch-revision_gitfast", | ||||
checksum_args=["rev_id"], | checksum_args=["rev_id"], | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc( | @api_doc( | ||||
"/vault/revision_gitfast/raw/", | "/vault/revision_gitfast/raw/", | ||||
category="Batch download", | category="Batch download", | ||||
tags=["hidden", "deprecated"], | tags=["hidden", "deprecated"], | ||||
) | ) | ||||
def _api_vault_revision_gitfast_raw(request: Request, rev_id: str): | def _api_vault_revision_gitfast_raw(request: Request, rev_id: str): | ||||
""" | """ | ||||
Show All 12 Lines | |||||
@api_route( | @api_route( | ||||
f"/vault/git-bare/(?P<swhid>{SWHID_RE})/", | f"/vault/git-bare/(?P<swhid>{SWHID_RE})/", | ||||
"api-1-vault-cook-git-bare", | "api-1-vault-cook-git-bare", | ||||
methods=["GET", "POST"], | methods=["GET", "POST"], | ||||
throttle_scope="swh_vault_cooking", | throttle_scope="swh_vault_cooking", | ||||
never_cache=True, | never_cache=True, | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/git-bare/", category="Batch download") | @api_doc("/vault/git-bare/", category="Batch download") | ||||
@format_docstring() | @format_docstring() | ||||
def api_vault_cook_git_bare(request: Request, swhid: str): | def api_vault_cook_git_bare(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/git-bare/(swhid)/ | .. http:get:: /api/1/vault/git-bare/(swhid)/ | ||||
.. http:post:: /api/1/vault/git-bare/(swhid)/ | .. http:post:: /api/1/vault/git-bare/(swhid)/ | ||||
▲ Show 20 Lines • Show All 59 Lines • ▼ Show 20 Lines | elif parsed_swhid.object_type == ObjectType.DIRECTORY: | ||||
) | ) | ||||
else: | else: | ||||
raise BadInputExc("Only revisions can be cooked as 'git-bare' bundles.") | raise BadInputExc("Only revisions can be cooked as 'git-bare' bundles.") | ||||
@api_route( | @api_route( | ||||
f"/vault/git-bare/(?P<swhid>{SWHID_RE})/raw/", | f"/vault/git-bare/(?P<swhid>{SWHID_RE})/raw/", | ||||
"api-1-vault-fetch-git-bare", | "api-1-vault-fetch-git-bare", | ||||
api_urls=vault_api_urls, | |||||
) | ) | ||||
@api_doc("/vault/git-bare/raw/", category="Batch download") | @api_doc("/vault/git-bare/raw/", category="Batch download") | ||||
def api_vault_fetch_revision_git_bare(request: Request, swhid: str): | def api_vault_fetch_revision_git_bare(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/vault/git-bare/(swhid)/raw/ | .. http:get:: /api/1/vault/git-bare/(swhid)/raw/ | ||||
Fetch the cooked git-bare archive for a revision. | Fetch the cooked git-bare archive for a revision. | ||||
Show All 25 Lines |