diff --git a/swh/web/api/views/metadata.py b/swh/web/api/views/metadata.py --- a/swh/web/api/views/metadata.py +++ b/swh/web/api/views/metadata.py @@ -189,3 +189,65 @@ response["Content-disposition"] = "attachment" return response + + +@api_route( + f"/raw-extrinsic-metadata/swhid/(?P{SWHID_RE})/authorities/", + "api-1-raw-extrinsic-metadata-swhid-authorities", +) +@api_doc("/raw-extrinsic-metadata/swhid/authorities/") +@format_docstring() +def api_raw_extrinsic_metadata_swhid_authorities(request, target): + """ + .. http:get:: /api/1/raw-extrinsic-metadata/swhid/(target)/authorities/ + + Returns a list of metadata authorities that provided metadata on + the given target. + They can then be used to get the raw `extrinsic metadata`_ collected on + that object from each of the authorities. + + .. _extrinsic metadata: https://docs.softwareheritage.org/devel/glossary.html#term-extrinsic-metadata + + :param string target: The SWHID of the object whose metadata-providing + authorities should be returned + + {common_headers} + + :>jsonarr string type: Type of authority (deposit_client, forge, registry) + :>jsonarr string url: Unique IRI identifying the authority + :>jsonarr object metadata_list_url: URL to get the list of metadata objects + on the given object from this authority + + :statuscode 200: no error + + **Example:** + + .. parsed-literal:: + + :swh_web_api:`raw-extrinsic-metadata/swhid/swh:1:dir:a2faa28028657859c16ff506924212b33f0e1307/authorities/` + """ # noqa + target_str = target + + try: + target = identifiers.CoreSWHID.from_string(target_str).to_extended() + except identifiers.ValidationError as e: + raise BadInputExc(f"Invalid target SWHID: {e.args[0]}") from None + + authorities = archive.storage.raw_extrinsic_metadata_get_authorities(target=target) + results = [ + { + **authority.to_dict(), + "metadata_list_url": reverse( + "api-1-raw-extrinsic-metadata-swhid", + url_args={"target": target_str}, + query_params={"authority": f"{authority.type.value} {authority.url}"}, + request=request, + ), + } + for authority in authorities + ] + + return { + "results": results, + "headers": {}, + } diff --git a/swh/web/tests/api/views/test_metadata.py b/swh/web/tests/api/views/test_metadata.py --- a/swh/web/tests/api/views/test_metadata.py +++ b/swh/web/tests/api/views/test_metadata.py @@ -137,3 +137,34 @@ query_params=query_params, ) check_api_get_responses(api_client, url, status_code=status_code) + + +@given(raw_extrinsic_metadata()) +def test_api_raw_extrinsic_metadata_list_authorities( + api_client, archive_data, metadata +): + archive_data.metadata_authority_add([metadata.authority]) + archive_data.metadata_fetcher_add([metadata.fetcher]) + archive_data.raw_extrinsic_metadata_add([metadata]) + + authority = metadata.authority + url = reverse( + "api-1-raw-extrinsic-metadata-swhid-authorities", + url_args={"target": str(metadata.target)}, + ) + rv = check_api_get_responses(api_client, url, status_code=200) + + expected_results = [ + { + "type": authority.type.value, + "url": authority.url, + "metadata_list_url": "http://testserver" + + reverse( + "api-1-raw-extrinsic-metadata-swhid", + url_args={"target": str(metadata.target)}, + query_params={"authority": f"{authority.type.value} {authority.url}"}, + ), + } + ] + + assert rv.data == expected_results