Skip to content
Snippets Groups Projects
Commit 6063c7e1 authored by Antoine R. Dumont's avatar Antoine R. Dumont
Browse files

Update JSONRenderer to deal with jsonp

parent ab34eb70
No related branches found
No related tags found
No related merge requests found
......@@ -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.
......
# 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
......@@ -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(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment