Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
Show First 20 Lines • Show All 48 Lines • ▼ Show 20 Lines | |||||
def api_origins(request): | def api_origins(request): | ||||
""" | """ | ||||
.. http:get:: /api/1/origins/ | .. http:get:: /api/1/origins/ | ||||
Get list of archived software origins. | Get list of archived software origins. | ||||
Origins are sorted by ids before returning them. | Origins are sorted by ids before returning them. | ||||
:query int origin_from: The first origin id that will be included | :query string origin_from: The first origin id that will be included | ||||
in returned results (default to 1) | in returned results (default to | ||||
0000000000000000000000000000000000000000) | |||||
:query int origin_count: The maximum number of origins to return | :query int origin_count: The maximum number of origins to return | ||||
(default to 100, can not exceed 10000) | (default to 100, can not exceed 10000) | ||||
:>jsonarr number id: the origin unique identifier | :>jsonarr number id: the origin unique identifier | ||||
:>jsonarr string origin_visits_url: link to in order to get information about the | :>jsonarr string origin_visits_url: link to in order to get information about the | ||||
visits for that origin | visits for that origin | ||||
:>jsonarr string type: the type of software origin (possible values are ``git``, ``svn``, | :>jsonarr string type: the type of software origin (possible values are ``git``, ``svn``, | ||||
``hg``, ``deb``, ``pypi``, ``ftp`` or ``deposit``) | ``hg``, ``deb``, ``pypi``, ``ftp`` or ``deposit``) | ||||
Show All 10 Lines | .. http:get:: /api/1/origins/ | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origins?origin_from=50000&origin_count=500` | :swh_web_api:`origins?origin_from=50000&origin_count=500` | ||||
""" # noqa | """ # noqa | ||||
origin_from = int(request.query_params.get('origin_from', '1')) | origin_from = request.query_params.get('origin_from', '00'*20) | ||||
origin_count = int(request.query_params.get('origin_count', '100')) | origin_count = int(request.query_params.get('origin_count', '100')) | ||||
origin_count = min(origin_count, 10000) | origin_count = min(origin_count, 10000) | ||||
results = api_lookup( | results = api_lookup( | ||||
service.lookup_origins, origin_from, origin_count+1, | service.lookup_origins, origin_from, origin_count+1, | ||||
enrich_fn=_enrich_origin) | enrich_fn=_enrich_origin) | ||||
response = {'results': results, 'headers': {}} | response = {'results': results, 'headers': {}} | ||||
if len(results) > origin_count: | if len(results) > origin_count: | ||||
origin_from = results.pop()['id'] | origin_from = results.pop()['id'] | ||||
response['headers']['link-next'] = reverse( | response['headers']['link-next'] = reverse( | ||||
'api-origins', query_params={'origin_from': origin_from, | 'api-origins', | ||||
query_params={'origin_from': origin_from, | |||||
'origin_count': origin_count}) | 'origin_count': origin_count}) | ||||
return response | return response | ||||
@api_route(r'/origin/(?P<origin_id>[0-9]+)/', 'api-origin') | @api_route(r'/origin/(?P<origin_id>[0-9a-f]+)/', '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)/ | ||||
Get information about a software origin. | Get information about a software origin. | ||||
▲ Show 20 Lines • Show All 47 Lines • ▼ Show 20 Lines | .. http:get:: /api/1/origin/(origin_type)/url/(origin_url)/ | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/git/url/https://github.com/python/cpython/` | :swh_web_api:`origin/git/url/https://github.com/python/cpython/` | ||||
""" # noqa | """ # noqa | ||||
ori_dict = { | ori_dict = { | ||||
'id': int(origin_id) if origin_id else None, | 'id': origin_id, | ||||
'type': origin_type, | 'type': origin_type, | ||||
'url': origin_url | 'url': origin_url | ||||
} | } | ||||
ori_dict = {k: v for k, v in ori_dict.items() if ori_dict[k]} | ori_dict = {k: v for k, v in ori_dict.items() if ori_dict[k]} | ||||
if 'id' in ori_dict: | if 'id' in ori_dict: | ||||
error_msg = 'Origin with id %s not found.' % ori_dict['id'] | error_msg = 'Origin with id %s not found.' % ori_dict['id'] | ||||
else: | else: | ||||
error_msg = 'Origin with type %s and URL %s not found' % ( | error_msg = 'Origin with type %s and URL %s not found' % ( | ||||
▲ Show 20 Lines • Show All 119 Lines • ▼ Show 20 Lines | def api_origin_metadata_search(request): | ||||
results = api_lookup(service.search_origin_metadata, fulltext, limit) | results = api_lookup(service.search_origin_metadata, fulltext, limit) | ||||
return { | return { | ||||
'results': results, | 'results': results, | ||||
} | } | ||||
@api_route(r'/origin/(?P<origin_id>[0-9]+)/visits/', 'api-origin-visits') | @api_route(r'/origin/(?P<origin_id>[0-9a-f]+)/visits/', 'api-origin-visits') | ||||
@api_doc('/origin/visits/') | @api_doc('/origin/visits/') | ||||
def api_origin_visits(request, origin_id): | def api_origin_visits(request, origin_id): | ||||
""" | """ | ||||
.. http:get:: /api/1/origin/(origin_id)/visits/ | .. http:get:: /api/1/origin/(origin_id)/visits/ | ||||
Get information about all visits of a software origin. | Get information about all visits of a software origin. | ||||
Visits are returned sorted in descending order according | Visits are returned sorted in descending order according | ||||
to their date. | to their date. | ||||
Show All 25 Lines | .. http:get:: /api/1/origin/(origin_id)/visits/ | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/1/visits/` | :swh_web_api:`origin/1/visits/` | ||||
""" # noqa | """ # noqa | ||||
result = {} | result = {} | ||||
origin_id = int(origin_id) | origin_id = origin_id | ||||
per_page = int(request.query_params.get('per_page', '10')) | per_page = int(request.query_params.get('per_page', '10')) | ||||
last_visit = request.query_params.get('last_visit') | last_visit = request.query_params.get('last_visit') | ||||
if last_visit: | if last_visit: | ||||
last_visit = int(last_visit) | last_visit = int(last_visit) | ||||
def _lookup_origin_visits( | def _lookup_origin_visits( | ||||
origin_id, last_visit=last_visit, per_page=per_page): | origin_id, last_visit=last_visit, per_page=per_page): | ||||
all_visits = get_origin_visits({'id': origin_id}) | all_visits = get_origin_visits({'id': origin_id}) | ||||
Show All 21 Lines | if results: | ||||
new_last_visit = results[-1]['visit'] | new_last_visit = results[-1]['visit'] | ||||
query_params = {} | query_params = {} | ||||
query_params['last_visit'] = new_last_visit | query_params['last_visit'] = new_last_visit | ||||
if request.query_params.get('per_page'): | if request.query_params.get('per_page'): | ||||
query_params['per_page'] = per_page | query_params['per_page'] = per_page | ||||
result['headers'] = { | result['headers'] = { | ||||
'link-next': reverse('api-origin-visits', | 'link-next': reverse( | ||||
'api-origin-visits', | |||||
url_args={'origin_id': origin_id}, | url_args={'origin_id': origin_id}, | ||||
query_params=query_params) | query_params=query_params) | ||||
} | } | ||||
result.update({ | result.update({ | ||||
'results': results | 'results': results | ||||
}) | }) | ||||
return result | return result | ||||
@api_route(r'/origin/(?P<origin_id>[0-9]+)/visit/(?P<visit_id>[0-9]+)/', | @api_route(r'/origin/(?P<origin_id>[0-9a-f]+)/visit/(?P<visit_id>[0-9]+)/', | ||||
'api-origin-visit') | 'api-origin-visit') | ||||
@api_doc('/origin/visit/') | @api_doc('/origin/visit/') | ||||
def api_origin_visit(request, origin_id, visit_id): | def api_origin_visit(request, origin_id, visit_id): | ||||
""" | """ | ||||
.. http:get:: /api/1/origin/(origin_id)/visit/(visit_id)/ | .. http:get:: /api/1/origin/(origin_id)/visit/(visit_id)/ | ||||
Get information about a specific visit of a software origin. | Get information about a specific visit of a software origin. | ||||
:param int origin_id: a software origin identifier | :param int origin_id: a software origin identifier | ||||
:param int visit_id: a visit identifier | :param int visit_id: a visit identifier | ||||
:reqheader Accept: the requested response content type, | :reqheader Accept: the requested response content type, | ||||
either ``application/json`` (default) or ``application/yaml`` | either ``application/json`` (default) or ``application/yaml`` | ||||
:resheader Content-Type: this depends on :http:header:`Accept` header of request | :resheader Content-Type: this depends on :http:header:`Accept` header of request | ||||
:>json string date: ISO representation of the visit date (in UTC) | :>json string date: ISO representation of the visit date (in UTC) | ||||
:>json number origin: the origin unique identifier | :>json dict metadata: extra information on the visit | ||||
:>json string origin: the origin unique identifier | |||||
:>json string origin_url: link to get information about the origin | :>json string origin_url: link to get information about the origin | ||||
:>jsonarr string snapshot: the snapshot identifier of the visit | :>jsonarr string snapshot: the snapshot identifier of the visit | ||||
:>jsonarr string snapshot_url: link to :http:get:`/api/1/snapshot/(snapshot_id)/` | :>jsonarr string snapshot_url: link to :http:get:`/api/1/snapshot/(snapshot_id)/` | ||||
in order to get information about the snapshot of the visit | in order to get information about the snapshot of the visit | ||||
:>json string status: status of the visit (either **full**, **partial** or **ongoing**) | :>json string status: status of the visit (either **full**, **partial** or **ongoing**) | ||||
:>json number visit: the unique identifier of the visit | :>json number visit: the unique identifier of the visit | ||||
**Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, :http:method:`options` | **Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, :http:method:`options` | ||||
:statuscode 200: no error | :statuscode 200: no error | ||||
:statuscode 404: requested origin or visit can not be found in the archive | :statuscode 404: requested origin or visit can not be found in the archive | ||||
**Example:** | **Example:** | ||||
.. parsed-literal:: | .. parsed-literal:: | ||||
:swh_web_api:`origin/1500/visit/1/` | :swh_web_api:`origin/1500/visit/1/` | ||||
""" # noqa | """ # noqa | ||||
return api_lookup( | return api_lookup( | ||||
service.lookup_origin_visit, int(origin_id), int(visit_id), | service.lookup_origin_visit, origin_id, int(visit_id), | ||||
notfound_msg=('No visit {} for origin {} found' | notfound_msg=('No visit {} for origin {} found' | ||||
.format(visit_id, origin_id)), | .format(visit_id, origin_id)), | ||||
enrich_fn=partial(_enrich_origin_visit, | enrich_fn=partial(_enrich_origin_visit, | ||||
with_origin_url=True, | with_origin_url=True, | ||||
with_origin_visit_url=False)) | with_origin_visit_url=False)) |