Changeset View
Changeset View
Standalone View
Standalone View
swh/web/api/views/origin.py
# Copyright (C) 2015-2018 The Software Heritage developers | # Copyright (C) 2015-2018 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU Affero General Public License version 3, or any later version | # License: GNU Affero General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
from distutils.util import strtobool | from distutils.util import strtobool | ||||
from rest_framework import status | |||||
from rest_framework.response import Response | |||||
from swh.web.common import service | from swh.web.common import service | ||||
from swh.web.common.utils import ( | from swh.web.common.utils import ( | ||||
reverse, get_origin_visits | reverse, get_origin_visits | ||||
) | ) | ||||
from swh.web.api.apidoc import api_doc | from swh.web.api.apidoc import api_doc | ||||
from swh.web.api.apiurls import api_route | from swh.web.api.apiurls import api_route | ||||
from swh.web.api.views.utils import api_lookup | from swh.web.api.views.utils import api_lookup | ||||
▲ Show 20 Lines • Show All 154 Lines • ▼ Show 20 Lines | def api_origin_search(request, url_pattern): | ||||
result.update({ | result.update({ | ||||
'results': results | 'results': results | ||||
}) | }) | ||||
return result | return result | ||||
@api_route(r'/origin/metadata-search/', | |||||
'api-origin-metadata-search') | |||||
@api_doc('/origin/metadata-search/') | |||||
def api_origin_metadata_search(request): | |||||
""" | |||||
.. http:get:: /api/1/origin/metadata-search/ | |||||
Search for software origins whose metadata (expressed as a | |||||
JSON-LD/CodeMeta dictionary) match the provided criteria. | |||||
For now, only full-text search on this dictionary is supported. | |||||
:query str fulltext: a string that will be matched against origin metadata; | |||||
results are ranked and ordered starting with the best ones. | |||||
:query int limit: the maximum number of found origins to return | |||||
zack: We should've an hardcoded max for limit: uses can specify a larger value than that, but in that… | |||||
:>jsonarr number origin_id: the origin unique identifier | |||||
:>jsonarr dict metadata: metadata of the origin (as a JSON-LD/CodeMeta dictionary) | |||||
:>jsonarr string from_revision: the revision used to extract these | |||||
metadata (the current HEAD or one of the former HEADs) | |||||
:>jsonarr dict tool: the tool used to extract these metadata | |||||
: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 | |||||
**Allowed HTTP Methods:** :http:method:`get`, :http:method:`head`, :http:method:`options` | |||||
:statuscode 200: no error | |||||
**Example:** | |||||
.. parsed-literal:: | |||||
:swh_web_api:`origin/metadata-search/?limit=2&fulltext=Jane%20Doe` | |||||
""" # noqa | |||||
fulltext = request.query_params.get('fulltext', None) | |||||
limit = int(request.query_params.get('limit', '70')) | |||||
if not fulltext: | |||||
content = '"fulltext" must be provided and non-empty.' | |||||
Done Inline ActionsYou must raise a swh.web.common.exc.BadInputExc here. An exception filter is set by the api_doc decorator that will translate it to a response with anlambert: You must raise a `swh.web.common.exc.BadInputExc` here.
An exception filter is set by the… | |||||
Done Inline ActionsFixed, thanks. I also added a test for that, which fails because Django infinite-loops while handling the exception -_- vlorentz: Fixed, thanks.
I also added a test for that, which fails because Django infinite-loops while… | |||||
Done Inline Actionsinfinite-recurses * vlorentz: infinite-recurses * | |||||
return Response(content, status=status.HTTP_400_BAD_REQUEST) | |||||
results = api_lookup(service.search_origin, fulltext, limit) | |||||
return { | |||||
'results': results, | |||||
} | |||||
@api_route(r'/origin/(?P<origin_id>[0-9]+)/visits/', 'api-origin-visits') | @api_route(r'/origin/(?P<origin_id>[0-9]+)/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 | ||||
▲ Show 20 Lines • Show All 148 Lines • Show Last 20 Lines |
We should've an hardcoded max for limit: uses can specify a larger value than that, but in that case the hardcoded value would win, restricting the result set size.