Add support for prettifying JSON response body which is printed to a file
like object when using LIBCLOUD_DEBUG environment variable.
This option can be enabled by setting LIBCLOUD_DEBUG_PRETTY_PRINT_JSON
environment variable.


Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/266ade55
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/266ade55
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/266ade55

Branch: refs/heads/trunk
Commit: 266ade55730f6071b1853732a5d50db2220b71b2
Parents: 591ffb0
Author: Tomaz Muraus <to...@apache.org>
Authored: Thu Aug 14 14:17:56 2014 +0200
Committer: Tomaz Muraus <to...@apache.org>
Committed: Thu Aug 14 14:22:28 2014 +0200

----------------------------------------------------------------------
 CHANGES.rst             |  6 ++++++
 libcloud/common/base.py | 12 ++++++++++++
 2 files changed, 18 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/266ade55/CHANGES.rst
----------------------------------------------------------------------
diff --git a/CHANGES.rst b/CHANGES.rst
index 814672a..edc1711 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -11,6 +11,12 @@ General
   OpenStack Identity (Keystone) service API v3.
   [Tomaz Muraus]
 
+- Add support for prettifying JSON response body which is printed to a file
+  like object when using ``LIBCLOUD_DEBUG`` environment variable.
+  This option can be enabled by setting ``LIBCLOUD_DEBUG_PRETTY_PRINT_JSON``
+  environment variable.
+  [Tomaz Muraus]
+
 Compute
 ~~~~~~~
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/266ade55/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index a42f228..488f084 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -300,17 +300,29 @@ class LoggingConnection():
         headers = lowercase_keys(dict(r.getheaders()))
 
         encoding = headers.get('content-encoding', None)
+        content_type = headers.get('content-type', None)
 
         if encoding in ['zlib', 'deflate']:
             body = decompress_data('zlib', body)
         elif encoding in ['gzip', 'x-gzip']:
             body = decompress_data('gzip', body)
 
+        pretty_print_json = os.environ.get('LIBCLOUD_DEBUG_PRETTY_PRINT_JSON',
+                                           False)
+
         if r.chunked:
             ht += "%x\r\n" % (len(body))
             ht += u(body)
             ht += "\r\n0\r\n"
         else:
+            if pretty_print_json and content_type == 'application/json':
+                try:
+                    body = json.loads(u(body))
+                    body = json.dumps(body, sort_keys=True, indent=4)
+                except:
+                    # Invalid JSON or server is lying about content-type
+                    pass
+
             ht += u(body)
 
         if sys.version_info >= (2, 6) and sys.version_info < (2, 7):

Reply via email to