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-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 swh.model.hashutil import hash_to_bytes, hash_to_hex | from swh.model.hashutil import hash_to_hex | ||||
from swh.model.swhids import CoreSWHID | |||||
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") | ||||
▲ Show 20 Lines • Show All 84 Lines • ▼ Show 20 Lines | def api_swhid_known(request): | ||||
limit = 1000 | limit = 1000 | ||||
if len(request.data) > limit: | if len(request.data) > limit: | ||||
raise LargePayloadExc( | raise LargePayloadExc( | ||||
"The maximum number of SWHIDs this endpoint can receive is %s" % limit | "The maximum number of SWHIDs this endpoint can receive is %s" % limit | ||||
) | ) | ||||
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": True} 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 = archive.lookup_missing_hashes(swhids_by_type) | ||||
k: set(map(hash_to_bytes, archive.lookup_missing_hashes({k: v}))) | |||||
for k, v in swhids_by_type.items() | |||||
} | |||||
for swhid in swhids: | for ty, missing_hashes in missing_hashes.items(): | ||||
if swhid.object_id not in missing_hashes[swhid.object_type]: | for hash in missing_hashes.iter(): | ||||
response[str(swhid)]["known"] = True | swhid = CoreSWHID(object_type=ty, object_id=hash) | ||||
response[str(swhid)]["known"] = False | |||||
return response | return response |