Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/identifiers.py
# Copyright (C) 2018-2021 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 typing import Dict, Set | |||||
from rest_framework.request import Request | |||||
from swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_bytes, hash_to_hex | ||||
from swh.model.swhids import 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.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import LargePayloadExc | from swh.web.common.exc import LargePayloadExc | ||||
from swh.web.common.identifiers import get_swhid, group_swhids, resolve_swhid | from swh.web.common.identifiers import get_swhid, group_swhids, resolve_swhid | ||||
@api_route(r"/resolve/(?P<swhid>.*)/", "api-1-resolve-swhid") | @api_route(r"/resolve/(?P<swhid>.+)/", "api-1-resolve-swhid") | ||||
@api_doc("/resolve/") | @api_doc("/resolve/") | ||||
@format_docstring() | @format_docstring() | ||||
def api_resolve_swhid(request, swhid): | def api_resolve_swhid(request: Request, swhid: str): | ||||
""" | """ | ||||
.. http:get:: /api/1/resolve/(swhid)/ | .. http:get:: /api/1/resolve/(swhid)/ | ||||
Resolve a SoftWare Heritage persistent IDentifier (SWHID) | Resolve a SoftWare Heritage persistent IDentifier (SWHID) | ||||
Try to resolve a provided `SoftWare Heritage persistent IDentifier | Try to resolve a provided `SoftWare Heritage persistent IDentifier | ||||
<https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html>`_ | <https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html>`_ | ||||
into an url for browsing the pointed archive object. | into an url for browsing the pointed archive object. | ||||
Show All 40 Lines | return { | ||||
"metadata": swhid_parsed.qualifiers(), | "metadata": swhid_parsed.qualifiers(), | ||||
"browse_url": request.build_absolute_uri(swhid_resolved["browse_url"]), | "browse_url": request.build_absolute_uri(swhid_resolved["browse_url"]), | ||||
} | } | ||||
@api_route(r"/known/", "api-1-known", methods=["POST"]) | @api_route(r"/known/", "api-1-known", methods=["POST"]) | ||||
@api_doc("/known/") | @api_doc("/known/") | ||||
@format_docstring() | @format_docstring() | ||||
def api_swhid_known(request): | def api_swhid_known(request: Request): | ||||
""" | """ | ||||
.. http:post:: /api/1/known/ | .. http:post:: /api/1/known/ | ||||
Check if a list of objects are present in the Software Heritage | Check if a list of objects are present in the Software Heritage | ||||
archive. | archive. | ||||
The objects to check existence must be provided using | The objects to check existence must be provided using | ||||
`SoftWare Heritage persistent IDentifiers | `SoftWare Heritage persistent IDentifiers | ||||
Show All 21 Lines | def api_swhid_known(request: Request): | ||||
swhids = [get_swhid(swhid) for swhid in request.data] | swhids = [get_swhid(swhid) for swhid in request.data] | ||||
response = {str(swhid): {"known": False} for swhid in swhids} | response = {str(swhid): {"known": False} for swhid in swhids} | ||||
# group swhids by their type | # group swhids by their type | ||||
swhids_by_type = group_swhids(swhids) | swhids_by_type = group_swhids(swhids) | ||||
# search for hashes not present in the storage | # search for hashes not present in the storage | ||||
missing_hashes = { | missing_hashes: Dict[ObjectType, Set[bytes]] = { | ||||
k: set(map(hash_to_bytes, archive.lookup_missing_hashes({k: v}))) | k: set(map(hash_to_bytes, archive.lookup_missing_hashes({k: v}))) | ||||
for k, v in swhids_by_type.items() | for k, v in swhids_by_type.items() | ||||
} | } | ||||
for swhid in swhids: | for swhid in swhids: | ||||
if swhid.object_id not in missing_hashes[swhid.object_type]: | if swhid.object_id not in missing_hashes[swhid.object_type]: | ||||
response[str(swhid)]["known"] = True | response[str(swhid)]["known"] = True | ||||
return response | return response |