diff --git a/swh/web/ui/controller.py b/swh/web/ui/controller.py index 927d531ddfd8f9d4200a9c52c60504ae43ac29d7..8c6b3bdb0cc8e04cacab205a2c90d8ad2f409e58 100644 --- a/swh/web/ui/controller.py +++ b/swh/web/ui/controller.py @@ -12,7 +12,6 @@ from flask.ext.api.renderers import HTMLRenderer from swh.core.hashutil import ALGORITHMS from swh.web.ui.main import app from swh.web.ui import service, renderers -from swh.web.ui.decorators import jsonp from swh.web.ui.exc import BadInputExc, NotFoundExc @@ -148,7 +147,6 @@ def content(hash, sha): @app.route('/api/1/stat/counters') -@jsonp def api_stats(): """Return statistics as a JSON object""" return service.stat_counters() @@ -171,7 +169,6 @@ def value_not_found(error): @app.route('/api/1/search/<string:q>/') -@jsonp def api_search(q): """Return search results as a JSON object""" return {'found': service.lookup_hash(q)} @@ -186,7 +183,6 @@ def _api_lookup(criteria, lookup_fn, error_msg_if_not_found): @app.route('/api/1/origin/<int:origin_id>') -@jsonp def api_origin(origin_id): """Return information about origin.""" return _api_lookup( @@ -195,7 +191,6 @@ def api_origin(origin_id): @app.route('/api/1/person/<int:person_id>') -@jsonp def api_person(person_id): """Return information about person.""" return _api_lookup( @@ -204,7 +199,6 @@ def api_person(person_id): @app.route('/api/1/release/<string:sha1_git>') -@jsonp def api_release(sha1_git): """Return information about release with id sha1_git.""" error_msg = 'Release with sha1_git %s not found.' % sha1_git @@ -215,7 +209,6 @@ def api_release(sha1_git): @app.route('/api/1/revision/<string:sha1_git>') -@jsonp def api_revision(sha1_git): """Return information about revision with id sha1_git. @@ -228,7 +221,6 @@ def api_revision(sha1_git): @app.route('/api/1/directory/<string:sha1_git>') -@jsonp def api_directory(sha1_git): """Return information about release with id sha1_git.""" recursive_flag = request.args.get('recursive', False) @@ -240,7 +232,6 @@ def api_directory(sha1_git): @app.route('/api/1/content/<string:q>/') -@jsonp def api_content_with_details(q): """Return content information up to its origin if found. @@ -264,7 +255,6 @@ def api_content_with_details(q): @app.route('/api/1/uploadnsearch/', methods=['POST']) -@jsonp def api_uploadnsearch(): """Upload the file's content in the post body request. Compute the hash and determine if it exists in the storage. diff --git a/swh/web/ui/decorators.py b/swh/web/ui/decorators.py deleted file mode 100644 index 798d3a3d14b6815841045ab1acb8846c0cebf1d9..0000000000000000000000000000000000000000 --- a/swh/web/ui/decorators.py +++ /dev/null @@ -1,32 +0,0 @@ -# Copyright (C) 2015 The Software Heritage developers -# See the AUTHORS file at the top-level directory of this distribution -# License: GNU Affero General Public License version 3, or any later version -# See top-level LICENSE file for more information - - -from functools import wraps -from flask import request, current_app - - -def jsonp(func): - """Wraps JSONified output for JSONP requests.""" - @wraps(func) - def decorated_function(*args, **kwargs): - callback = request.args.get('callback', False) - if callback: - res = func(*args, **kwargs) - if hasattr(res, 'data'): # for a response object, data - data = res.data - if isinstance(data, bytes): # we're dealing in utf-8 bytes - data = data.decode('utf-8') - else: - data = str(data) - else: # fallback case... - data = str(res) - - content = ''.join([str(callback), '(', data, ')']) - return current_app.response_class( - content, - mimetype='application/javascript') - return func(*args, **kwargs) - return decorated_function diff --git a/swh/web/ui/renderers.py b/swh/web/ui/renderers.py index 78dd24293dafe82d64a1b8c97f388a9ed0292784..fccb3bda24f178f646bd68e6849ba955e1cf13ca 100644 --- a/swh/web/ui/renderers.py +++ b/swh/web/ui/renderers.py @@ -17,8 +17,18 @@ class YAMLRenderer(renderers.BaseRenderer): return yaml.dump(data, encoding=self.charset) +class JSONPRenderer(renderers.JSONRenderer): + def render(self, data, media_type, **options): + # Requested indentation may be set in the Accept header. + res = super().render(data, media_type, **options) + jsonp = request.args.get('callback') + if jsonp: + return '%s(%s)' % (jsonp, res) + return res + + RENDERERS = [ - 'flask.ext.api.renderers.JSONRenderer', + 'swh.web.ui.renderers.JSONPRenderer', 'flask.ext.api.renderers.BrowsableAPIRenderer', 'flask.ext.api.parsers.URLEncodedParser', 'swh.web.ui.renderers.YAMLRenderer', @@ -26,7 +36,7 @@ RENDERERS = [ RENDERERS_INSTANCE = [ - renderers.JSONRenderer(), + JSONPRenderer(), renderers.BrowsableAPIRenderer(), parsers.URLEncodedParser(), YAMLRenderer(),