#14 Output the JSON in a human-friendly way upon request
Merged 8 years ago by pingou. Opened 8 years ago by pingou.

file modified
+33 -10
@@ -92,9 +92,17 @@ 

      return (pkg, repotype)

  

  

+ def _get_pretty(request):

+     pretty = False

+     if request.query_string.lower() in ['pretty=1', 'pretty=true']:

+         pretty = True

+     return pretty

+ 

+ 

  @asyncio.coroutine

  def get_pkg(request):

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

+     pretty = _get_pretty(request)

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

      pkg, repotype = _get_pkg(branch, name)

  
@@ -135,13 +143,18 @@ 

              output['co-packages'] = []

          output['repo'] = repotype if repotype else 'release'

          session.close()

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

+ 

+     return web.Response(body=json.dumps(

+             output,

+             sort_keys=pretty, indent=4, separators=(',', ': ')

+     ).encode('utf-8'))

  

  

  @asyncio.coroutine

  def get_pkg_files(request):

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

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

+     pretty = _get_pretty(request)

      pkg, repotype = _get_pkg(branch, name)

  

      dbfile = '%s/mdapi-%s%s-filelists.sqlite' % (
@@ -153,16 +166,20 @@ 

          session2 = mdapilib.create_session('sqlite:///%s' % dbfile)

          filelist = mdapilib.get_files(session2, pkg.pkgId)

          session2.close()

-     return web.Response(body=json.dumps({

-         'files': [fileinfo.to_json() for fileinfo in filelist],

-         'repo': repotype if repotype else 'release',

-     }).encode('utf-8'))

+     return web.Response(body=json.dumps(

+         {

+             'files': [fileinfo.to_json() for fileinfo in filelist],

+             'repo': repotype if repotype else 'release',

+         },

+         sort_keys=pretty, indent=4, separators=(',', ': ')

+     ).encode('utf-8'))

  

  

  @asyncio.coroutine

  def get_pkg_changelog(request):

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

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

+     pretty = _get_pretty(request)

      pkg, repotype = _get_pkg(branch, name)

  

      dbfile = '%s/mdapi-%s%s-other.sqlite' % (
@@ -174,23 +191,29 @@ 

          session2 = mdapilib.create_session('sqlite:///%s' % dbfile)

          changelogs = mdapilib.get_changelog(session2, pkg.pkgId)

          session2.close()

-     return web.Response(body=json.dumps({

-         'files': [changelog.to_json() for changelog in changelogs],

-         'repo': repotype if repotype else 'release',

-     }).encode('utf-8'))

+     return web.Response(body=json.dumps(

+         {

+             'files': [changelog.to_json() for changelog in changelogs],

+             'repo': repotype if repotype else 'release',

+         },

+         sort_keys=pretty, indent=4, separators=(',', ': ')

+     ).encode('utf-8'))

  

  

  @asyncio.coroutine

  def list_branches(request):

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

      '''

+     pretty = _get_pretty(request)

      output = list(set([

          # Remove the front part `mdapi-` and the end part -<type>.sqlite

          filename.replace('mdapi-', '').rsplit('-', 2)[0].replace('-updates', '')

          for filename in os.listdir(CONFIG['DB_FOLDER'])

          if filename.startswith('mdapi') and filename.endswith('.sqlite')

      ]))

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

+     return web.Response(body=json.dumps(

+         output, sort_keys=pretty, indent=4, separators=(',', ': ')

+     ).encode('utf-8'))

  

  

  @asyncio.coroutine

file modified
+2 -2
@@ -91,9 +91,9 @@ 

  

      /{branch}/files/{package name}

  

- So for example, for the kernel in rawhide:

+ So for example, for the kernel-core in rawhide:

  

-     <a href="rawhide/files/kernel">/rawhide/files/kernel</a>

+     <a href="rawhide/files/kernel-core">/rawhide/files/kernel-core</a>

  

  

  Retrieve the changelog of a package

no initial comment

Some thoughts:

  • There are browser extensions for Firefox that do this for you. Chromium does it by default.
  • By including all those extra spaces, it means you're transferring possible twice the amount of data that you would otherwise, which makes it somewhat more cumbersome for programmatic usage.
  • By including all those extra spaces, it means you're transferring possible twice the amount of data that you would otherwise, which makes it somewhat more cumbersome for programmatic usage.

That's why I've made it optional and off by default :)

Ah, I see. :+1: then.

Metadata