Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show All 18 Lines | if 'id' in origin: | ||||
o = origin.copy() | o = origin.copy() | ||||
o['origin_visits_url'] = \ | o['origin_visits_url'] = \ | ||||
reverse('api-origin-visits', url_args={'origin_id': origin['id']}) | reverse('api-origin-visits', url_args={'origin_id': origin['id']}) | ||||
return o | return o | ||||
return origin | return origin | ||||
@api_route(r'/origins/', 'api-origins') | |||||
@api_doc('/origins/', noargs=True) | |||||
def api_origins(request): | |||||
""" | |||||
.. http:get:: /api/1/origins/ | |||||
Get list of archived software origins. | |||||
Origins are sorted by ids before returning them. | |||||
:query int origin_from: The minimum id of the origins to return | |||||
(default to 1) | |||||
:query int origin_count: The maximum number of origins to return | |||||
(default to 100, can not exceed 10000) | |||||
:>jsonarr number id: the origin unique identifier | |||||
:>jsonarr string origin_visits_url: link to in order to get information about the | |||||
visits for that origin | |||||
:>jsonarr string type: the type of software origin (possible values are ``git``, ``svn``, | |||||
``hg``, ``deb``, ``pypi``, ``ftp`` or ``deposit``) | |||||
:>jsonarr string url: the origin canonical url | |||||
: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 | |||||
:resheader Link: indicates that a subsequent or previous result page are available | |||||
and contains the urls pointing to them | |||||
**Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, :http:method:`options` | |||||
:statuscode 200: no error | |||||
**Example:** | |||||
.. parsed-literal:: | |||||
:swh_web_api:`origins?origin_from=50000&origin_count=500` | |||||
""" # noqa | |||||
origin_from = int(request.query_params.get('origin_from', '1')) | |||||
origin_count = int(request.query_params.get('origin_count', '100')) | |||||
origin_count = min(origin_count, 10000) | |||||
results = api_lookup( | |||||
service.lookup_origins, origin_from, origin_count+1, | |||||
enrich_fn=_enrich_origin) | |||||
response = {'results': results, 'headers': {}} | |||||
if len(results) > origin_count: | |||||
origin_from = results.pop()['id'] | |||||
response['headers']['link-next'] = reverse( | |||||
'api-origins', query_params={'origin_from': origin_from, | |||||
'origin_count': origin_count}) | |||||
if len(results) > 0 and results[0]['id'] != 1: | |||||
origin_from = max(1, results[0]['id'] - origin_count) | |||||
response['headers']['link-prev'] = reverse( | |||||
'api-origins', query_params={'origin_from': origin_from, | |||||
'origin_count': origin_count}) | |||||
vlorentz: You should set `origin_count = `results[0]['id'] - origin_from` (or something similar) | |||||
anlambertAuthorUnsubmitted Done Inline ActionsThere is no need to do so as origin_count corresponds to the maximum number of origins to return. anlambert: There is no need to do so as `origin_count` corresponds to the maximum number of origins to… | |||||
vlorentzUnsubmitted Not Done Inline ActionsI know, but as this is the link-prev, having too many origins will make the two lists of origins overlap. vlorentz: I know, but as this is the `link-prev`, having too many origins will make the two lists of… | |||||
anlambertAuthorUnsubmitted Done Inline ActionsYes it can happen but I don't want to modify the origin_count parameter value. In practice, origins should be listed from the first one to the last one so that's not really anlambert: Yes it can happen but I don't want to modify the `origin_count` parameter value.
In practice… | |||||
vlorentzUnsubmitted Not Done Inline ActionsShouldn't we drop link-prev then? vlorentz: Shouldn't we drop `link-prev` then? | |||||
anlambertAuthorUnsubmitted Done Inline ActionsSold, will update accordingly after rebase (got some incoming changes in master branch that I want to push before landing that one). anlambert: Sold, will update accordingly after rebase (got some incoming changes in master branch that I… | |||||
return response | |||||
@api_route(r'/origin/(?P<origin_id>[0-9]+)/', 'api-origin') | @api_route(r'/origin/(?P<origin_id>[0-9]+)/', 'api-origin') | ||||
@api_route(r'/origin/(?P<origin_type>[a-z]+)/url/(?P<origin_url>.+)/', | @api_route(r'/origin/(?P<origin_type>[a-z]+)/url/(?P<origin_url>.+)/', | ||||
'api-origin') | 'api-origin') | ||||
@api_doc('/origin/') | @api_doc('/origin/') | ||||
def api_origin(request, origin_id=None, origin_type=None, origin_url=None): | def api_origin(request, origin_id=None, origin_type=None, origin_url=None): | ||||
""" | """ | ||||
.. http:get:: /api/1/origin/(origin_id)/ | .. http:get:: /api/1/origin/(origin_id)/ | ||||
▲ Show 20 Lines • Show All 348 Lines • Show Last 20 Lines |
You should set origin_count = results[0]['id'] - origin_from` (or something similar)