Changeset 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 | from swh.web.common import service, utils | ||||
from swh.web.common.utils import resolve_swh_persistent_id | from swh.web.common.utils import ( | ||||
resolve_swh_persistent_id, | |||||
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 | ||||
@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() | ||||
Show All 40 Lines | def api_resolve_swh_pid(request, swh_id): | ||||
object_type = swh_id_parsed.object_type | object_type = swh_id_parsed.object_type | ||||
object_id = swh_id_parsed.object_id | object_id = swh_id_parsed.object_id | ||||
service.lookup_object(object_type, object_id) | service.lookup_object(object_type, object_id) | ||||
# id is well-formed and the pointed object exists | # id is well-formed and the pointed object exists | ||||
swh_id_data = swh_id_parsed._asdict() | swh_id_data = swh_id_parsed._asdict() | ||||
swh_id_data['browse_url'] = request.build_absolute_uri( | swh_id_data['browse_url'] = request.build_absolute_uri( | ||||
swh_id_resolved['browse_url']) | swh_id_resolved['browse_url']) | ||||
return swh_id_data | return swh_id_data | ||||
@api_route(r'/known/', | |||||
'api-1-swh-pid-known', methods=['POST']) | |||||
@api_doc('/known/', noargs=True, tags=['hidden']) | |||||
@format_docstring() | |||||
def api_swh_pid_known(request): | |||||
""" | |||||
.. http:post:: /api/1/known/ | |||||
Check if a list of Software Heritage persistent identifier is present | |||||
in the archive depending on their id (sha1_git). | |||||
Returns: | |||||
A dictionary with: | |||||
zack: the indentation of the dictionary keys do not look right here | |||||
keys(str): Persistent identifier | |||||
Not Done Inline ActionsI don't like "found" as a key here, as the name of the action is neither "search" nor "find" here. I think it should be:
The latter would allow to convey more information in the future, e.g., an object that is present but hidden. AFAICT this would be the first time we convey a non-boolean status of objects in a public API, so we probably do not want to go there; we can reconsider for API 2.0. Hence, my proposal is "known: true/false" for now. (Naming is the most difficult problem in CS (cit.) *g*) zack: I don't like "found" as a key here, as the name of the action is neither "search" nor "find"… | |||||
Not Done Inline Actions
It would imply breaking the API compatibility, so no. vlorentz: > The latter would allow to convey more information in the future, e.g., an object that is… | |||||
Not Done Inline Actions
This is not the place to have this conversation, but nack. Because (1) with API version 1 we make no API compatibility promises yet (that will happen starting version 2); and (2) adding fields to an existing dictionary should be possible (and documented) as a way to evolve an API that clients should expect. zack: > It would imply breaking the API compatibility, so no.
This is not the place to have this… | |||||
values(dict): A dictionary containing the key 'known'. (true if | |||||
the pid is present, False otherwise) | |||||
""" | |||||
persistent_ids = [get_persistent_identifier(pid) | |||||
for pid in request.data] | |||||
response = {str(pid): {'known': False} for pid in persistent_ids} | |||||
# group pids by their type | |||||
pids_by_type = utils.group_swh_persistent_identifiers(persistent_ids) | |||||
Not Done Inline Actionsshorter version: persistent_ids = [parse_persistent_identifier(pid) for pid in request.data] vlorentz: shorter version:
```
persistent_ids = [parse_persistent_identifier(pid) for pid in request. | |||||
# search for hashes not present in the storage | |||||
missing_hashes = service.lookup_missing_hashes(pids_by_type) | |||||
Not Done Inline ActionsYou can use a dict comprehension: response = {str(pid): {'found': False} for pid in persistent_ids} vlorentz: You can use a dict comprehension:
```
response = {str(pid): {'found': False} for pid in… | |||||
for pid in persistent_ids: | |||||
if pid.object_id not in missing_hashes: | |||||
response[str(pid)]['known'] = True | |||||
return response | |||||
Not Done Inline ActionsYou can assign the dicts with content directly: response was already initialized with found set to False for all pids, so you only need to do this: for pid in persistent_ids: if pid.object_id not in missing_hashes: response[str(pid)]['found'] = True vlorentz: You can assign the dicts with content directly:
`response` was already initialized with… |
the indentation of the dictionary keys do not look right here