#53 Add support for jsonp as returned format
Merged 7 years ago by pingou. Opened 7 years ago by pingou.

file modified
+36 -3
@@ -22,10 +22,11 @@ 

  '''

  Top level of the mdapi aiohttp application.

  '''

+ import functools

  import json

  import os

  import urllib

- 

+ from urllib.parse import parse_qs

  

  import asyncio

  import werkzeug
@@ -55,6 +56,31 @@ 

      INDEX = INDEX.replace('$PREFIX', CONFIG.get('PREFIX', ''))

  

  

+ def allows_jsonp(function):

+     ''' Add support for JSONP queries to the endpoint decorated. '''

+ 

+     @functools.wraps(function)

+     def wrapper(request, *args, **kwargs):

+         ''' Actually does the job with the arguments provided.

+ 

+         :arg request: the request that was called that we want to add JSONP

+         support to

+         :type request: aiohttp.web_request.Request

+         

+         '''

+         response = yield from function(request, *args, **kwargs)

+         url_arg = parse_qs(request.query_string)

+         callback = url_arg.get('callback')

+         if callback and request.method == 'GET':

+             if isinstance(callback, list):

+                 callback = callback[0]

+             response.mimetype = 'application/javascript'

+             response.text = '%s(%s);' % (callback, response.text)

+ 

+         return response

+ 

+     return wrapper

+ 

  @asyncio.coroutine

  def _get_pkg(branch, name=None, action=None, srcname=None):

      ''' Return the pkg information for the given package in the specified
@@ -173,6 +199,7 @@ 

  

  

  @asyncio.coroutine

+ @allows_jsonp

  def get_pkg(request):

      branch = request.match_info.get('branch')

      pretty = _get_pretty(request)
@@ -185,11 +212,13 @@ 

      if pretty:

          args = dict(sort_keys=True, indent=4, separators=(',', ': '))

  

-     return web.Response(body=json.dumps(output, **args).encode('utf-8'),

+     output = web.Response(body=json.dumps(output, **args).encode('utf-8'),

                          content_type='application/json')

+     return output

  

  

  @asyncio.coroutine

+ @allows_jsonp

  def get_src_pkg(request):

      branch = request.match_info.get('branch')

      pretty = _get_pretty(request)
@@ -206,6 +235,7 @@ 

                          content_type='application/json')

  

  @asyncio.coroutine

+ @allows_jsonp

  def get_pkg_files(request):

      branch = request.match_info.get('branch')

      name = request.match_info.get('name')
@@ -236,6 +266,7 @@ 

  

  

  @asyncio.coroutine

+ @allows_jsonp

  def get_pkg_changelog(request):

      branch = request.match_info.get('branch')

      name = request.match_info.get('name')
@@ -266,6 +297,7 @@ 

  

  

  @asyncio.coroutine

+ @allows_jsonp

  def list_branches(request):

      ''' Return the list of all branches currently supported by mdapi

      '''
@@ -284,7 +316,8 @@ 

      return web.Response(body=json.dumps(output, **args).encode('utf-8'),

                          content_type='application/json')

  

- 

+ @asyncio.coroutine

+ @allows_jsonp

  def process_dep(request, action):

      ''' Return the information about the packages having the specified

      action (provides, requires, obsoletes...)

Signed-off-by: Pierre-Yves Chibon pingou@pingoured.fr

For PEP-8 there should only be one space after the =.

Since request is always args[0], you could put it in the signature of wrapper above and drop this line. It'd be a little cleaner, and you can also add request to the docblock so its clear to the users of this wrapper that the first arg must be a request.

This change seems unrelated to the JSONP stuff. I recommend a separate commit for it so this commit is atomic.

I recommend targeted tests for the new code. LGTM!

Good idea, I'll give it a try

This entire project would require tests, but that's a task for another day :)

rebased

7 years ago

1 new commit added

  • Add missing coroutine declaration
7 years ago

All adjusted and split into 2 commits :)

Thanks for the review!

Pull-Request has been merged by pingou

7 years ago
Metadata