Repository: libcloud Updated Branches: refs/heads/trunk 26118a33e -> 039ecec20
Add support for pretty-formatting XML response bodies when LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE environment variable is set. Also fix issue with curl log lines under Python 3. Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/039ecec2 Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/039ecec2 Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/039ecec2 Branch: refs/heads/trunk Commit: 039ecec2036d2910a467f8fe717f8bbf9c5af777 Parents: 26118a3 Author: Tomaz Muraus <[email protected]> Authored: Sun Sep 7 14:57:32 2014 +0200 Committer: Tomaz Muraus <[email protected]> Committed: Sun Sep 7 15:03:03 2014 +0200 ---------------------------------------------------------------------- CHANGES.rst | 8 ++--- docs/troubleshooting.rst | 69 ++++++++++++++++++++++++++++++++++++++----- libcloud/common/base.py | 19 ++++++++---- 3 files changed, 80 insertions(+), 16 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/039ecec2/CHANGES.rst ---------------------------------------------------------------------- diff --git a/CHANGES.rst b/CHANGES.rst index a1da690..42227ab 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -11,10 +11,10 @@ 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. +- Add support for prettifying JSON or XML 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_RESPONSE`` environment variable. [Tomaz Muraus] - Add support for using an HTTP proxy for outgoing HTTP and HTTPS requests. http://git-wip-us.apache.org/repos/asf/libcloud/blob/039ecec2/docs/troubleshooting.rst ---------------------------------------------------------------------- diff --git a/docs/troubleshooting.rst b/docs/troubleshooting.rst index 5f20602..8c12103 100644 --- a/docs/troubleshooting.rst +++ b/docs/troubleshooting.rst @@ -32,9 +32,11 @@ response body (if compressed) before logging it. To enable it, set ``LIBCLOUD_DEBUG`` environment variable and make it point to a file where the debug output should be saved. -If the API returns a JSON which is not human friendly, you can also set -``LIBCLOUD_DEBUG_PRETTY_PRINT_JSON`` environment variable which will cause -the JSON to be beautified / formated so it's easier for humands to read it. +If the API returns JSON or XML in the response body which is not human +friendly, you can also set ``LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE`` +environment variable which will cause the JSON or XML to be beautified +/ formated so it's easier for humans to read it. Keep in mind that this +only works for non-chunked responses. Example 1 - Logging output to standard error ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ @@ -68,10 +70,10 @@ Example output: # -------- end 4431824872:4431825232 response ---------- -Example 2 - Making JSON response human friendly -~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Example 2 - Making JSON / XML response human friendly +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -Non-formatted response: +Non-formatted JSON response: .. sourcecode:: bash @@ -95,7 +97,7 @@ Human friendly formatted JSON response: .. sourcecode:: bash - LIBCLOUD_DEBUG=/dev/stderr LIBCLOUD_DEBUG_PRETTY_PRINT_JSON=1 python my_script.py + LIBCLOUD_DEBUG=/dev/stderr LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE=1 python my_script.py .. sourcecode:: bash @@ -180,3 +182,56 @@ Human friendly formatted JSON response: ] } # -------- end 41102928:41133624 response ---------- + +Non-formatted XML response: + +.. sourcecode:: bash + + LIBCLOUD_DEBUG=/dev/stderr python my_script.py + +.. sourcecode:: bash + + # -------- begin 33145616:33126160 response ---------- + HTTP/1.1 200 OK + X-Amzn-Requestid: e84f62d0-368e-11e4-820b-8bf013dc269e + Date: Sun, 07 Sep 2014 13:00:13 GMT + Content-Length: 457 + Content-Type: text/xml + + <?xml version="1.0"?> + <ListHostedZonesResponse xmlns="https://route53.amazonaws.com/doc/2012-02-29/"><HostedZones><HostedZone><Id>/hostedzone/Z14L0C73CHH1DN</Id><Name>example1.com.</Name><CallerReference>41747982-568E-0DFC-8C11-71C23757C740</CallerReference><Config><Comment>test</Comment></Config><ResourceRecordSetCount>9</ResourceRecordSetCount></HostedZone></HostedZones><IsTruncated>false</IsTruncated><MaxItems>100</MaxItems></ListHostedZonesResponse> + # -------- end 33145616:33126160 response ---------- + +Human friendly formatted XML response: + +.. sourcecode:: bash + + LIBCLOUD_DEBUG=/dev/stderr LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE=1 python my_script.py + +.. sourcecode:: bash + + # -------- begin 19444496:19425040 response ---------- + HTTP/1.1 200 OK + X-Amzn-Requestid: 01c02441-368f-11e4-b616-9b9bd7509a8f + Date: Sun, 07 Sep 2014 13:00:56 GMT + Content-Length: 457 + Content-Type: text/xml + + <?xml version="1.0" ?> + <ListHostedZonesResponse xmlns="https://route53.amazonaws.com/doc/2012-02-29/"> + <HostedZones> + <HostedZone> + <Id>/hostedzone/Z14L0C73CHH1DN</Id> + <Name>example1.com.</Name> + <CallerReference>41747982-568E-0DFC-8C11-71C23757C740</CallerReference> + <Config> + <Comment>test</Comment> + </Config> + <ResourceRecordSetCount>9</ResourceRecordSetCount> + </HostedZone> + </HostedZones> + <IsTruncated>false</IsTruncated> + <MaxItems>100</MaxItems> + </ListHostedZonesResponse> + + # -------- end 19444496:19425040 response ---------- http://git-wip-us.apache.org/repos/asf/libcloud/blob/039ecec2/libcloud/common/base.py ---------------------------------------------------------------------- diff --git a/libcloud/common/base.py b/libcloud/common/base.py index f559c22..1d05b4b 100644 --- a/libcloud/common/base.py +++ b/libcloud/common/base.py @@ -20,6 +20,8 @@ import copy import binascii import time +import xml.dom.minidom + try: from lxml import etree as ET except ImportError: @@ -308,21 +310,28 @@ class LoggingConnection(): elif encoding in ['gzip', 'x-gzip']: body = decompress_data('gzip', body) - pretty_print_json = os.environ.get('LIBCLOUD_DEBUG_PRETTY_PRINT_JSON', - False) + pretty_print = os.environ.get('LIBCLOUD_DEBUG_PRETTY_PRINT_RESPONSE', + False) if r.chunked: ht += "%x\r\n" % (len(body)) - ht += u(body) + ht += body.decode('utf-8') ht += "\r\n0\r\n" else: - if pretty_print_json and content_type == 'application/json': + if pretty_print and content_type == 'application/json': try: - body = json.loads(u(body)) + body = json.loads(body.decode('utf-8')) body = json.dumps(body, sort_keys=True, indent=4) except: # Invalid JSON or server is lying about content-type pass + elif pretty_print and content_type == 'text/xml': + try: + elem = xml.dom.minidom.parseString(body.decode('utf-8')) + body = elem.toprettyxml() + except Exception: + # Invalid XML + pass ht += u(body)
