Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/identifiers.py
# Copyright (C) 2018-2019 The Software Heritage developers | # Copyright (C) 2018-2019 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.web.common import service, utils | from swh.web.common import service, utils | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
resolve_swh_persistent_id, | resolve_swh_persistent_id, | ||||
get_persistent_identifier | get_persistent_identifier | ||||
) | ) | ||||
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.exc import InputSizeExc | |||||
from swh.web.config import get_config | |||||
@api_route(r'/resolve/(?P<swh_id>.*)/', | @api_route(r'/resolve/(?P<swh_id>.*)/', | ||||
'api-1-resolve-swh-pid') | 'api-1-resolve-swh-pid') | ||||
@api_doc('/resolve/') | @api_doc('/resolve/') | ||||
@format_docstring() | @format_docstring() | ||||
def api_resolve_swh_pid(request, swh_id): | def api_resolve_swh_pid(request, swh_id): | ||||
""" | """ | ||||
▲ Show 20 Lines • Show All 58 Lines • ▼ Show 20 Lines | .. http:post:: /api/1/known/ | ||||
Returns: | Returns: | ||||
A dictionary with: | A dictionary with: | ||||
keys(str): Persistent identifier | keys(str): Persistent identifier | ||||
values(dict): A dictionary containing the key 'known'. (true if | values(dict): A dictionary containing the key 'known'. (true if | ||||
the pid is present, False otherwise) | the pid is present, False otherwise) | ||||
""" | """ | ||||
max_input_size = get_config()['pids_max_size'] | |||||
if len(request.data) > max_input_size: | |||||
raise InputSizeExc('The maximum number of PIDs this endpoint can ' | |||||
'receive is %s' % max_input_size) | |||||
persistent_ids = [get_persistent_identifier(pid) | persistent_ids = [get_persistent_identifier(pid) | ||||
for pid in request.data] | for pid in request.data] | ||||
response = {str(pid): {'known': False} for pid in persistent_ids} | response = {str(pid): {'known': False} for pid in persistent_ids} | ||||
# group pids by their type | # group pids by their type | ||||
pids_by_type = utils.group_swh_persistent_identifiers(persistent_ids) | pids_by_type = utils.group_swh_persistent_identifiers(persistent_ids) | ||||
# search for hashes not present in the storage | # search for hashes not present in the storage | ||||
missing_hashes = service.lookup_missing_hashes(pids_by_type) | missing_hashes = service.lookup_missing_hashes(pids_by_type) | ||||
for pid in persistent_ids: | for pid in persistent_ids: | ||||
if pid.object_id not in missing_hashes: | if pid.object_id not in missing_hashes: | ||||
response[str(pid)]['known'] = True | response[str(pid)]['known'] = True | ||||
return response | return response |