diff --git a/swh/web/ui/views/api.py b/swh/web/ui/views/api.py --- a/swh/web/ui/views/api.py +++ b/swh/web/ui/views/api.py @@ -3,6 +3,8 @@ # License: GNU Affero General Public License version 3, or any later version # See top-level LICENSE file for more information +import functools + from types import GeneratorType from flask import render_template, request, url_for @@ -34,6 +36,41 @@ pointing to it""" +def _api_lookup(lookup_fn, *args, + notfound_msg='Object not found', + enrich_fn=lambda x: x): + """Capture a redundant behavior of: + - looking up the backend with a criteria (be it an identifier or checksum) + passed to the function lookup_fn + - if nothing is found, raise an NotFoundExc exception with error + message notfound_msg. + - Otherwise if something is returned: + - either as list, map or generator, map the enrich_fn function to it + and return the resulting data structure as list. + - either as dict and pass to enrich_fn and return the dict enriched. + + Args: + - criteria: discriminating criteria to lookup + - lookup_fn: function expects one criteria and optional supplementary + *args. + - notfound_msg: if nothing matching the criteria is found, + raise NotFoundExc with this error message. + - enrich_fn: Function to use to enrich the result returned by + lookup_fn. Default to the identity function if not provided. + - *args: supplementary arguments to pass to lookup_fn. + + Raises: + NotFoundExp or whatever `lookup_fn` raises. + + """ + res = lookup_fn(*args) + if not res: + raise NotFoundExc(notfound_msg) + if isinstance(res, (map, list, GeneratorType)): + return [enrich_fn(x) for x in res] + return enrich_fn(res) + + @app.route('/api/1/') def api_endpoints(): """Display the list of opened api endpoints. @@ -107,9 +144,8 @@ return ov r = _api_lookup( - origin_id, - _lookup_origin_visits, - error_msg_if_not_found='No origin %s found' % origin_id, + _lookup_origin_visits, origin_id, + notfound_msg='No origin {} found'.format(origin_id), enrich_fn=_enrich_origin_visit) if r: @@ -167,11 +203,10 @@ return ov return _api_lookup( - origin_id, - service.lookup_origin_visit, - 'No visit %s for origin %s found' % (visit_id, origin_id), - _enrich_origin_visit, - visit_id) + service.lookup_origin_visit, origin_id, visit_id, + notfound_msg=('No visit {} for origin {} found' + .format(visit_id, origin_id)), + enrich_fn=_enrich_origin_visit) @app.route('/api/1/content/symbol/', methods=['POST']) @@ -211,11 +246,9 @@ return service.lookup_expression(exp, last_sha1, per_page) symbols = _api_lookup( - q, - lookup_fn=lookup_exp, - error_msg_if_not_found='No indexed raw content match expression \'' - '%s\'.' % q, - enrich_fn=lambda x: utils.enrich_content(x, top_url=True)) + lookup_exp, q, + notfound_msg="No indexed raw content match expression '{}'.".format(q), + enrich_fn=functools.partial(utils.enrich_content, top_url=True)) if symbols: l = len(symbols) @@ -311,43 +344,6 @@ return response -def _api_lookup(criteria, - lookup_fn, - error_msg_if_not_found, - enrich_fn=lambda x: x, - *args): - """Capture a redundant behavior of: - - looking up the backend with a criteria (be it an identifier or checksum) - passed to the function lookup_fn - - if nothing is found, raise an NotFoundExc exception with error - message error_msg_if_not_found. - - Otherwise if something is returned: - - either as list, map or generator, map the enrich_fn function to it - and return the resulting data structure as list. - - either as dict and pass to enrich_fn and return the dict enriched. - - Args: - - criteria: discriminating criteria to lookup - - lookup_fn: function expects one criteria and optional supplementary - *args. - - error_msg_if_not_found: if nothing matching the criteria is found, - raise NotFoundExc with this error message. - - enrich_fn: Function to use to enrich the result returned by - lookup_fn. Default to the identity function if not provided. - - *args: supplementary arguments to pass to lookup_fn. - - Raises: - NotFoundExp or whatever `lookup_fn` raises. - - """ - res = lookup_fn(criteria, *args) - if not res: - raise NotFoundExc(error_msg_if_not_found) - if isinstance(res, (map, list, GeneratorType)): - return [enrich_fn(x) for x in res] - return enrich_fn(res) - - @app.route('/api/1/origin//') @app.route('/api/1/origin//url/') @doc.route('/api/1/origin/') @@ -397,8 +393,8 @@ return origin return _api_lookup( - ori_dict, lookup_fn=service.lookup_origin, - error_msg_if_not_found=error_msg, + service.lookup_origin, ori_dict, + notfound_msg=error_msg, enrich_fn=_enrich_origin) @@ -416,8 +412,8 @@ """ return _api_lookup( - person_id, lookup_fn=service.lookup_person, - error_msg_if_not_found='Person with id %s not found.' % person_id) + service.lookup_person, person_id, + notfound_msg='Person with id {} not found.'.format(person_id)) @app.route('/api/1/release//') @@ -441,9 +437,8 @@ """ error_msg = 'Release with sha1_git %s not found.' % sha1_git return _api_lookup( - sha1_git, - lookup_fn=service.lookup_release, - error_msg_if_not_found=error_msg, + service.lookup_release, sha1_git, + notfound_msg=error_msg, enrich_fn=utils.enrich_release) @@ -588,15 +583,11 @@ """ ts = utils.parse_timestamp(ts) return _api_lookup( - origin_id, - service.lookup_revision_by, - 'Revision with (origin_id: %s, branch_name: %s' - ', ts: %s) not found.' % (origin_id, - branch_name, - ts), - utils.enrich_revision, - branch_name, - ts) + service.lookup_revision_by, origin_id, branch_name, ts, + notfound_msg=('Revision with (origin_id: {}, branch_name: {}' + ', ts: {}) not found.'.format(origin_id, + branch_name, ts)), + enrich_fn=utils.enrich_revision) @app.route('/api/1/revision//prev//') @@ -620,8 +611,7 @@ return utils.enrich_revision(revision, context) return _api_lookup( - sha1_git, - service.lookup_revision, + service.lookup_revision, sha1_git, 'Revision with sha1_git %s not found.' % sha1_git, _enrich_revision) @@ -739,9 +729,8 @@ return service.lookup_revision_log(s, limit) error_msg = 'Revision with sha1_git %s not found.' % sha1_git - rev_get = _api_lookup(sha1_git, - lookup_fn=lookup_revision_log_with_limit, - error_msg_if_not_found=error_msg, + rev_get = _api_lookup(lookup_revision_log_with_limit, sha1_git, + notfound_msg=error_msg, enrich_fn=utils.enrich_revision) l = len(rev_get) @@ -767,10 +756,10 @@ else: rev_forward_ids = prev_sha1s.split('/') - rev_forward = _api_lookup(rev_forward_ids, - lookup_fn=service.lookup_revision_multiple, - error_msg_if_not_found=error_msg, - enrich_fn=utils.enrich_revision) + rev_forward = _api_lookup( + service.lookup_revision_multiple, rev_forward_ids, + notfound_msg=error_msg, + enrich_fn=utils.enrich_revision) revisions = rev_forward + rev_backward result.update({ @@ -948,9 +937,8 @@ return p return _api_lookup( - q, - lookup_fn=service.lookup_content_provenance, - error_msg_if_not_found='Content with %s not found.' % q, + service.lookup_content_provenance, q, + notfound_msg='Content with {} not found.'.format(q), enrich_fn=_enrich_revision) @@ -970,10 +958,8 @@ """ return _api_lookup( - q, - lookup_fn=service.lookup_content_filetype, - error_msg_if_not_found='No filetype information found ' - 'for content %s.' % q, + service.lookup_content_filetype, q, + notfound_msg='No filetype information found for content {}.'.format(q), enrich_fn=utils.enrich_metadata_endpoint) @@ -994,10 +980,8 @@ """ return _api_lookup( - q, - lookup_fn=service.lookup_content_language, - error_msg_if_not_found='No language information found ' - 'for content %s.' % q, + service.lookup_content_language, q, + notfound_msg='No language information found for content %s.'.format(q), enrich_fn=utils.enrich_metadata_endpoint) @@ -1017,10 +1001,8 @@ """ return _api_lookup( - q, - lookup_fn=service.lookup_content_license, - error_msg_if_not_found='No license information found ' - 'for content %s.' % q, + service.lookup_content_license, q, + notfound_msg='No license information found for content {}.'.format(q), enrich_fn=utils.enrich_metadata_endpoint) @@ -1041,10 +1023,8 @@ """ return _api_lookup( - q, - lookup_fn=service.lookup_content_ctags, - error_msg_if_not_found='No ctags symbol found ' - 'for content %s.' % q, + service.lookup_content_ctags, q, + notfound_msg='No ctags symbol found for content {}.'.format(q), enrich_fn=utils.enrich_metadata_endpoint) @@ -1108,9 +1088,8 @@ """ return _api_lookup( - q, - lookup_fn=service.lookup_content, - error_msg_if_not_found='Content with %s not found.' % q, + service.lookup_content, q, + notfound_msg='Content with {} not found.'.format(q), enrich_fn=utils.enrich_content)