Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show First 20 Lines • Show All 187 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/origin/search/(url_pattern)/ | ||||
.. 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 or a regular expression | :param string url_pattern: a string pattern | ||||
:query int limit: the maximum number of found origins to return | :query int limit: the maximum number of found origins to return | ||||
:query boolean regexp: if true, consider provided pattern as a regular | |||||
expression and search origins whose urls match it | |||||
: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 | ||||
{return_origin_array} | {return_origin_array} | ||||
{common_headers} | {common_headers} | ||||
{resheader_link} | {resheader_link} | ||||
**Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, | **Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, | ||||
:http:method:`options` | :http:method:`options` | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/search/python/?limit=2` | :swh_web_api:`origin/search/python/?limit=2` | ||||
""" | """ | ||||
result = {} | result = {} | ||||
offset = int(request.query_params.get('offset', '0')) | |||||
limit = min(int(request.query_params.get('limit', '70')), 100) | limit = min(int(request.query_params.get('limit', '70')), 100) | ||||
regexp = request.query_params.get('regexp', 'false') | scroll_token = request.query_params.get('scroll_token') | ||||
with_visit = request.query_params.get('with_visit', 'false') | with_visit = request.query_params.get('with_visit', 'false') | ||||
results = api_lookup(service.search_origin, url_pattern, offset, limit, | (results, scroll_token) = api_lookup( | ||||
bool(strtobool(regexp)), bool(strtobool(with_visit)), | service.search_origin, url_pattern, limit, | ||||
scroll_token, bool(strtobool(with_visit)), | |||||
enrich_fn=_enrich_origin) | enrich_fn=_enrich_origin) | ||||
nb_results = len(results) | if scroll_token is not None: | ||||
if nb_results == limit: | |||||
query_params = {} | query_params = {} | ||||
query_params['offset'] = offset + limit | |||||
query_params['limit'] = limit | query_params['limit'] = limit | ||||
query_params['regexp'] = regexp | query_params['scroll_token'] = scroll_token | ||||
result['headers'] = { | result['headers'] = { | ||||
'link-next': reverse('api-1-origin-search', | 'link-next': reverse('api-1-origin-search', | ||||
url_args={'url_pattern': url_pattern}, | url_args={'url_pattern': url_pattern}, | ||||
query_params=query_params) | query_params=query_params) | ||||
} | } | ||||
result.update({ | result.update({ | ||||
'results': results | 'results': list(results) | ||||
}) | }) | ||||
return result | return result | ||||
@api_route(r'/origin/metadata-search/', | @api_route(r'/origin/metadata-search/', | ||||
'api-1-origin-metadata-search') | 'api-1-origin-metadata-search') | ||||
@api_doc('/origin/metadata-search/', noargs=True, need_params=True) | @api_doc('/origin/metadata-search/', noargs=True, need_params=True) | ||||
▲ Show 20 Lines • Show All 253 Lines • Show Last 20 Lines |