Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/identifiers.py
Show All 17 Lines | |||||
@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): | ||||
""" | """ | ||||
.. http:get:: /api/1/resolve/(swh_id)/ | .. http:get:: /api/1/resolve/(swh_id)/ | ||||
Resolve a Software Heritage persistent identifier. | Resolve a Software Heritage persistent identifier. | ||||
Try to resolve a provided `persistent identifier <https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html>`_ | Try to resolve a provided `persistent identifier | ||||
<https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html>`_ | |||||
into an url for browsing the pointed archive object. If the provided | into an url for browsing the pointed archive object. If the provided | ||||
identifier is valid, the existence of the object in the archive | identifier is valid, the existence of the object in the archive | ||||
will also be checked. | will also be checked. | ||||
:param string swh_id: a Software Heritage persistent identifier | :param string swh_id: a Software Heritage persistent identifier | ||||
:>json string browse_url: the url for browsing the pointed object | :>json string browse_url: the url for browsing the pointed object | ||||
:>json object metadata: object holding optional parts of the persistent identifier | :>json object metadata: object holding optional parts of the | ||||
persistent identifier | |||||
:>json string namespace: the persistent identifier namespace | :>json string namespace: the persistent identifier namespace | ||||
:>json string object_id: the hash identifier of the pointed object | :>json string object_id: the hash identifier of the pointed object | ||||
:>json string object_type: the type of the pointed object | :>json string object_type: the type of the pointed object | ||||
:>json number scheme_version: the scheme version of the persistent identifier | :>json number scheme_version: the scheme version of the persistent | ||||
identifier | |||||
{common_headers} | {common_headers} | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
:statuscode 400: an invalid persistent identifier has been provided | :statuscode 400: an invalid persistent identifier has been provided | ||||
:statuscode 404: the pointed object does not exist in the archive | :statuscode 404: the pointed object does not exist in the archive | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`resolve/swh:1:rev:96db9023b881d7cd9f379b0c154650d6c108e9a3;origin=https://github.com/openssl/openssl/` | :swh_web_api:`resolve/swh:1:rev:96db9023b881d7cd9f379b0c154650d6c108e9a3;origin=https://github.com/openssl/openssl/` | ||||
""" # noqa | """ | ||||
# try to resolve the provided pid | # try to resolve the provided pid | ||||
swh_id_resolved = resolve_swh_persistent_id(swh_id) | swh_id_resolved = resolve_swh_persistent_id(swh_id) | ||||
# id is well-formed, now check that the pointed | # id is well-formed, now check that the pointed | ||||
# object is present in the archive, NotFoundExc | # object is present in the archive, NotFoundExc | ||||
# will be raised otherwise | # will be raised otherwise | ||||
swh_id_parsed = swh_id_resolved['swh_id_parsed'] | swh_id_parsed = swh_id_resolved['swh_id_parsed'] | ||||
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_route(r'/known/', | ||||
'api-1-swh-pid-known', methods=['POST']) | 'api-1-known', methods=['POST']) | ||||
@api_doc('/known/', tags=['hidden']) | @api_doc('/known/') | ||||
@format_docstring() | @format_docstring() | ||||
def api_swh_pid_known(request): | def api_swh_pid_known(request): | ||||
""" | """ | ||||
.. http:post:: /api/1/known/ | .. http:post:: /api/1/known/ | ||||
Check if a list of Software Heritage persistent identifier is present | Check if a list of objects are present in the Software Heritage | ||||
in the archive depending on their id (sha1_git). | archive. | ||||
The objects to check existence must be provided using Software Heritage | |||||
`persistent identifiers | |||||
<https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html>`_. | |||||
:<jsonarr string -: input array of Software Heritage persistent | |||||
identifiers, its length can not exceed 1000. | |||||
:>json object <swh_pid>: an object whose keys are input persistent | |||||
identifiers and values objects with the following keys: | |||||
Returns: | * **known (bool)**: whether the object was found | ||||
A dictionary with: | |||||
keys(str): Persistent identifier | {common_headers} | ||||
values(dict): A dictionary containing the key 'known'. (true if | |||||
the pid is present, False otherwise) | :statuscode 200: no error | ||||
:statuscode 400: an invalid persistent identifier was provided | |||||
:statuscode 413: the input array of persistent identifiers is too large | |||||
vlorentz: could be a link to https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html | |||||
Done Inline Actionsack anlambert: ack | |||||
""" | """ | ||||
limit = 1000 | limit = 1000 | ||||
if len(request.data) > limit: | if len(request.data) > limit: | ||||
raise LargePayloadExc('The maximum number of PIDs this endpoint can ' | raise LargePayloadExc('The maximum number of PIDs this endpoint can ' | ||||
'receive is %s' % limit) | 'receive is %s' % limit) | ||||
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 | ||||
Not Done Inline Actionswas provided vlorentz: was provided | |||||
Done Inline Actionsack anlambert: ack | |||||
pids_by_type = utils.group_swh_persistent_identifiers(persistent_ids) | pids_by_type = utils.group_swh_persistent_identifiers(persistent_ids) | ||||
Not Done Inline ActionsWhy noqa? The line "Allowed HTTP Methods" can't have a newline? vlorentz: Why noqa? The line "Allowed HTTP Methods" can't have a newline? | |||||
Done Inline ActionsIt could if I update the parsing code in apidoc, let me check if it easily feasible anlambert: It could if I update the parsing code in `apidoc`, let me check if it easily feasible | |||||
Done Inline ActionsYes it is. This HTTP methods parsing stuff is only needed for the HTML documentation displayed on the archive website and is not related to sphinxcontrib-httpdomain. So I will update the code to extract the HTTP methods from the docstrings and then remove the **Allowed HTTP Methods:** lines anlambert: Yes it is. This HTTP methods parsing stuff is only needed for the HTML documentation displayed… | |||||
Done Inline Actionsanlambert: D2679 | |||||
# 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 |
could be a link to https://docs.softwareheritage.org/devel/swh-model/persistent-identifiers.html