libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From to...@apache.org
Subject svn commit: r1239468 - in /libcloud/trunk: libcloud/common/base.py test/test_response_classes.py
Date Thu, 02 Feb 2012 06:48:32 GMT
Author: tomaz
Date: Thu Feb  2 06:48:32 2012
New Revision: 1239468

URL: http://svn.apache.org/viewvc?rev=1239468&view=rev
Log:
Add support for decompressing responses inside the base Response class. Also add
tests.

Modified:
    libcloud/trunk/libcloud/common/base.py
    libcloud/trunk/test/test_response_classes.py

Modified: libcloud/trunk/libcloud/common/base.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/libcloud/common/base.py?rev=1239468&r1=1239467&r2=1239468&view=diff
==============================================================================
--- libcloud/trunk/libcloud/common/base.py (original)
+++ libcloud/trunk/libcloud/common/base.py Thu Feb  2 06:48:32 2012
@@ -16,6 +16,8 @@
 import sys
 import ssl
 import time
+import zlib
+import gzip
 
 from xml.etree import ElementTree as ET
 from pipes import quote as pquote
@@ -42,6 +44,7 @@ from libcloud.httplib_ssl import Libclou
 
 LibcloudHTTPConnection = httplib.HTTPConnection
 
+
 class Response(object):
     """
     A Base Response class to derive from.
@@ -57,7 +60,7 @@ class Response(object):
     parse_zero_length_body = False
 
     def __init__(self, response, connection):
-        self.body = response.read().strip()
+        self.body = self._decompress_response(response=response)
 
         if PY3:
             self.body = b(self.body).decode('utf-8')
@@ -106,6 +109,33 @@ class Response(object):
         """
         return self.status == httplib.OK or self.status == httplib.CREATED
 
+    def _decompress_response(self, response):
+        """
+        Decompress a response body if it is using deflate or gzip encoding.
+
+        @return: Decompressed response
+        """
+        headers = lowercase_keys(dict(response.getheaders()))
+        encoding = headers.get('content-encoding', None)
+
+        body = response.read()
+
+        if encoding in  ['zlib', 'deflate']:
+            body = zlib.decompress(body)
+        elif encoding in ['gzip', 'x-gzip']:
+            # TODO: Should export BytesIO as StringIO in libcloud.utils.py3
+            if PY3:
+                from io import BytesIO
+                cls = BytesIO
+            else:
+                cls = StringIO
+
+            body = gzip.GzipFile(fileobj=cls(body)).read()
+        else:
+            body = body.strip()
+
+        return body
+
 
 class JsonResponse(Response):
     """

Modified: libcloud/trunk/test/test_response_classes.py
URL: http://svn.apache.org/viewvc/libcloud/trunk/test/test_response_classes.py?rev=1239468&r1=1239467&r2=1239468&view=diff
==============================================================================
--- libcloud/trunk/test/test_response_classes.py (original)
+++ libcloud/trunk/test/test_response_classes.py Thu Feb  2 06:48:32 2012
@@ -15,11 +15,13 @@
 
 import sys
 import unittest
+import zlib
+import gzip
 
 from mock import Mock
 
-from libcloud.utils.py3 import httplib
-from libcloud.common.base import XmlResponse, JsonResponse
+from libcloud.utils.py3 import httplib, b, StringIO, PY3
+from libcloud.common.base import Response, XmlResponse, JsonResponse
 from libcloud.common.types import MalformedResponseError
 
 
@@ -87,6 +89,63 @@ class ResponseClassesTests(unittest.Test
         parsed = response.parse_body()
         self.assertEqual(parsed, '')
 
+    def test_deflate_encoding(self):
+        original_data = 'foo bar ponies, wooo zlib'
+        compressed_data = zlib.compress(b(original_data))
+
+        self._mock_response.read.return_value = compressed_data
+        self._mock_response.getheaders.return_value = \
+                {'Content-Encoding': 'deflate'}
+
+        response = Response(response=self._mock_response,
+                            connection=self._mock_connection)
+
+        body = response.parse_body()
+        self.assertEqual(body, original_data)
+
+        self._mock_response.getheaders.return_value = \
+                {'Content-Encoding': 'zlib'}
+
+        response = Response(response=self._mock_response,
+                            connection=self._mock_connection)
+
+        body = response.parse_body()
+        self.assertEqual(body, original_data)
+
+    def test_gzip_encoding(self):
+        original_data = 'foo bar ponies, wooo gzip'
+
+        if PY3:
+            from io import BytesIO
+            string_io = BytesIO()
+        else:
+            string_io = StringIO()
+
+        stream = gzip.GzipFile(fileobj=string_io, mode='w')
+        stream.write(b(original_data))
+        stream.close()
+        compressed_data = string_io.getvalue()
+
+        self._mock_response.read.return_value = compressed_data
+        self._mock_response.getheaders.return_value = \
+                {'Content-Encoding': 'gzip'}
+
+        response = Response(response=self._mock_response,
+                            connection=self._mock_connection)
+
+        body = response.parse_body()
+        self.assertEqual(body, original_data)
+
+        self._mock_response.getheaders.return_value = \
+                {'Content-Encoding': 'x-gzip'}
+
+        response = Response(response=self._mock_response,
+                            connection=self._mock_connection)
+
+        body = response.parse_body()
+        self.assertEqual(body, original_data)
+
+
 
 if __name__ == '__main__':
     sys.exit(unittest.main())



Mime
View raw message