diff --git a/swh/web/api/views/origin.py b/swh/web/api/views/origin.py --- a/swh/web/api/views/origin.py +++ b/swh/web/api/views/origin.py @@ -432,3 +432,43 @@ enrich_fn=partial(_enrich_origin_visit, with_origin_url=True, with_origin_visit_url=False)) + + +@api_route(r'/origin/(?P[0-9]+)/metadata', + 'api-origin-metdata-get') +@api_doc('/origin/') +def api_origin_metadata_get(request, origin_id, visit_id): + """ + .. http:get:: /api/1/origin/(origin_id)/metadata + + Get metadata of a software origin. + + :param int origin_id: a software origin identifier + + :reqheader Accept: the requested response content type, + either ``application/json`` (default) or ``application/yaml`` + :resheader Content-Type: this depends on :http:header:`Accept` header of request + + :>jsonarr number origin_id: the origin unique identifier + :>jsonarr dict metadata: metadata of the origin (as a JSON-LD/CodeMeta dictionary) + :>jsonarr string from_revision: the revision used to extract these + metadata (the current HEAD or one of the former HEADs) + :>jsonarr dict tool: the tool used to extract these metadata + + **Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, :http:method:`options` + + :statuscode 200: no error + :statuscode 404: requested origin or visit can not be found in the archive + + **Example:** + + .. parsed-literal:: + + :swh_web_api:`origin/1500/metadata` + """ # noqa + origin_ids = [int(origin_id)] + results = api_lookup(service.lookup_origin_metadata, origin_ids) + + return { + 'results': results, + } diff --git a/swh/web/common/service.py b/swh/web/common/service.py --- a/swh/web/common/service.py +++ b/swh/web/common/service.py @@ -289,6 +289,27 @@ return results +def lookup_origin_metadata(origin_ids): + """Search for origins whose origin_id matches given origin_id. + + Args: + origin_id: unique identifier for a origin + + Returns: + list of origin metadata. + + """ + matches = idx_storage.origin_metadata_get_by(origin_ids) + results = [] + for match in matches: + match['from_revision'] = hashutil.hash_to_hex(match['from_revision']) + result = converters.from_origin( + storage.origin_get({'id': match.pop('id')})) + result['metadata'] = match + results.append(result) + return results + + def lookup_person(person_id): """Return information about the person with id person_id.