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 @@ -10,10 +10,11 @@ import iso8601 from django.http import HttpResponse +from django.shortcuts import redirect from rest_framework.request import Request from swh.model import hashutil, swhids -from swh.model.model import MetadataAuthority, MetadataAuthorityType +from swh.model.model import MetadataAuthority, MetadataAuthorityType, Origin from swh.web.api.apidoc import api_doc, format_docstring from swh.web.api.apiurls import api_route from swh.web.common import archive, converters @@ -235,10 +236,9 @@ This endpoint should only be used directly to retrieve metadata from core SWHIDs (with type ``cnt``, ``dir``, ``rev``, ``rel``, and ``snp``). - For "extended" SWHIDs such as origins, the URL in the - ``origin_metadata_authorities_url`` field of - :http:get:`/api/1/origin/(origin_url)/get/` should be used instead of building - this URL directly. + For "extended" SWHIDs such as origins, + :http:get:`/api/1/raw-extrinsic-metadata/origin/(origin_url)/authorities/` + should be used instead of building this URL directly. :param string target: The core SWHID of the object whose metadata-providing authorities should be returned @@ -284,3 +284,42 @@ "results": results, "headers": {}, } + + +@api_route( + "/raw-extrinsic-metadata/origin/(?P.*)/authorities/", + "api-1-raw-extrinsic-metadata-origin-authorities", +) +@api_doc("/raw-extrinsic-metadata/origin/authorities/") +@format_docstring() +def api_raw_extrinsic_metadata_origin_authorities(request: Request, origin_url: str): + """ + .. http:get:: /api/1/raw-extrinsic-metadata/origin/(origin_url)/authorities/ + + Similar to + :http:get:`/api/1/raw-extrinsic-metadata/swhid/(target)/authorities/` + but to get metadata on origins instead of objects + + :param string origin_url: The URL of the origin 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/origin/https://github.com/rdicosmo/parmap/authorities/` + """ # noqa + url = reverse( + "api-1-raw-extrinsic-metadata-swhid-authorities", + url_args={"target": Origin(url=origin_url).swhid()}, + ) + return redirect(url) 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 @@ -235,3 +235,21 @@ ] assert rv.data == expected_results + + +def test_api_raw_extrinsic_metadata_origin_redirect(api_client, archive_data): + origin = Origin(url="http://example.com/repo.git") + archive_data.origin_add([origin]) + + url = reverse( + "api-1-raw-extrinsic-metadata-origin-authorities", + url_args={"origin_url": origin.url}, + ) + rv = check_http_get_response(api_client, url, status_code=302) + + redirect_url = reverse( + "api-1-raw-extrinsic-metadata-swhid-authorities", + url_args={"target": str(origin.swhid())}, + ) + + assert rv["location"] == redirect_url