Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show All 12 Lines | from swh.web.api.utils import ( | ||||
enrich_origin, | enrich_origin, | ||||
enrich_origin_search_result, | enrich_origin_search_result, | ||||
enrich_origin_visit, | enrich_origin_visit, | ||||
) | ) | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
from swh.web.common import archive | from swh.web.common import archive | ||||
from swh.web.common.exc import BadInputExc | from swh.web.common.exc import BadInputExc | ||||
from swh.web.common.origin_visits import get_origin_visits | from swh.web.common.origin_visits import get_origin_visits | ||||
from swh.web.common.utils import reverse | from swh.web.common.utils import origin_visit_types, reverse | ||||
DOC_RETURN_ORIGIN = """ | DOC_RETURN_ORIGIN = """ | ||||
:>json string origin_visits_url: link to in order to get information | :>json string origin_visits_url: link to in order to get information | ||||
about the visits for that origin | about the visits for that origin | ||||
:>json string url: the origin canonical url | :>json string url: the origin canonical url | ||||
""" | """ | ||||
DOC_RETURN_ORIGIN_ARRAY = DOC_RETURN_ORIGIN.replace(":>json", ":>jsonarr") | DOC_RETURN_ORIGIN_ARRAY = DOC_RETURN_ORIGIN.replace(":>json", ":>jsonarr") | ||||
▲ Show 20 Lines • Show All 111 Lines • ▼ Show 20 Lines | return api_lookup( | ||||
archive.lookup_origin, | archive.lookup_origin, | ||||
ori_dict, | ori_dict, | ||||
notfound_msg=error_msg, | notfound_msg=error_msg, | ||||
enrich_fn=enrich_origin, | enrich_fn=enrich_origin, | ||||
request=request, | request=request, | ||||
) | ) | ||||
def _visit_types(): | |||||
docstring = "" | |||||
# available visit types are queried using swh-search so we do it in a try | |||||
# block in case of failure (for instance in docker environment when | |||||
# elasticsearch service is not available) | |||||
try: | |||||
visit_types = [f"**{visit_type}**" for visit_type in origin_visit_types()] | |||||
docstring = ", ".join(visit_types[:-1]) + f", and {visit_types[-1]}" | |||||
except Exception: | |||||
vlorentz: I'd do it like this, but no big deal | |||||
docstring = "???" | |||||
pass | |||||
return docstring | |||||
@api_route( | @api_route( | ||||
r"/origin/search/(?P<url_pattern>.+)/", | r"/origin/search/(?P<url_pattern>.+)/", | ||||
"api-1-origin-search", | "api-1-origin-search", | ||||
throttle_scope="swh_api_origin_search", | throttle_scope="swh_api_origin_search", | ||||
) | ) | ||||
@api_doc("/origin/search/") | @api_doc("/origin/search/") | ||||
@format_docstring(return_origin_array=DOC_RETURN_ORIGIN_ARRAY) | @format_docstring( | ||||
return_origin_array=DOC_RETURN_ORIGIN_ARRAY, visit_types=_visit_types() | |||||
) | |||||
def api_origin_search(request, url_pattern): | def api_origin_search(request, url_pattern): | ||||
""" | """ | ||||
.. http:get:: /api/1/origin/search/(url_pattern)/ | .. http:get:: /api/1/origin/search/(url_pattern)/ | ||||
Search for software origins whose urls contain a provided string | Search for software origins whose urls contain a provided string | ||||
pattern or match a provided regular expression. | pattern or match a provided regular expression. | ||||
The search is performed in a case insensitive way. | The search is performed in a case insensitive way. | ||||
.. warning:: | .. warning:: | ||||
This endpoint used to provide an ``offset`` query parameter, | This endpoint used to provide an ``offset`` query parameter, | ||||
and guarantee an order on results. This is no longer true, | and guarantee an order on results. This is no longer true, | ||||
and only the Link header should be used for paginating through | and only the Link header should be used for paginating through | ||||
results. | results. | ||||
:param string url_pattern: a string pattern | :param string url_pattern: a string pattern | ||||
:query boolean use_ql: whether to use swh search query language or not | :query boolean use_ql: whether to use swh search query language or not | ||||
:query int limit: the maximum number of found origins to return | :query int limit: the maximum number of found origins to return | ||||
(bounded to 1000) | (bounded to 1000) | ||||
:query boolean with_visit: if true, only return origins with at least | :query boolean with_visit: if true, only return origins with at least | ||||
one visit by Software heritage | one visit by Software heritage | ||||
:query string visit_type: if provided, only return origins with that | |||||
specific visit type (currently the supported types are {visit_types}) | |||||
{return_origin_array} | {return_origin_array} | ||||
{common_headers} | {common_headers} | ||||
{resheader_link} | {resheader_link} | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
▲ Show 20 Lines • Show All 296 Lines • Show Last 20 Lines |
I'd do it like this, but no big deal