Updated Branches:
refs/heads/trunk 1dd2382cf -> b6cb0699f
Refactor libcloud.common.base.Connection to avoid doing duplicate work and
remove some additional code.
Also update affected code.
Project: http://git-wip-us.apache.org/repos/asf/libcloud/repo
Commit: http://git-wip-us.apache.org/repos/asf/libcloud/commit/fe72fc13
Tree: http://git-wip-us.apache.org/repos/asf/libcloud/tree/fe72fc13
Diff: http://git-wip-us.apache.org/repos/asf/libcloud/diff/fe72fc13
Branch: refs/heads/trunk
Commit: fe72fc13a131b446e1f18cd22005f9c8fd03ce79
Parents: 1dd2382
Author: Tomaz Muraus <tomaz@apache.org>
Authored: Sun Dec 8 19:10:42 2013 +0100
Committer: Tomaz Muraus <tomaz@apache.org>
Committed: Sun Dec 8 19:10:42 2013 +0100
----------------------------------------------------------------------
libcloud/common/base.py | 46 ++++++++++++++++++++++--------------------
libcloud/common/linode.py | 15 +++++++++-----
2 files changed, 34 insertions(+), 27 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/libcloud/blob/fe72fc13/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index 34d413c..13e7baf 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -86,18 +86,27 @@ class Response(object):
:param connection: Parent connection object.
:type connection: :class:`.Connection`
"""
- self.body = self._decompress_response(response=response)
-
- if PY3:
- self.body = b(self.body).decode('utf-8')
-
- self.status = response.status
+ self.connection = connection
# http.client In Python 3 doesn't automatically lowercase the header
# names
self.headers = lowercase_keys(dict(response.getheaders()))
self.error = response.reason
- self.connection = connection
+ self.status = response.status
+
+ # This attribute is set when using LoggingConnection.
+ original_data = getattr(response, '_original_data', None)
+
+ if original_data:
+ # LoggingConnection already decompresses data so it can log it
+ # which means we don't need to decompress it here.
+ self.body = response._original_data
+ else:
+ self.body = self._decompress_response(body=response.read(),
+ headers=self.headers)
+
+ if PY3:
+ self.body = b(self.body).decode('utf-8')
if not self.success():
raise Exception(self.parse_error())
@@ -136,30 +145,23 @@ class Response(object):
:rtype: ``bool``
:return: ``True`` or ``False``
"""
- return self.status == httplib.OK or self.status == httplib.CREATED
+ return self.status in [httplib.OK, httplib.CREATED]
- def _decompress_response(self, response):
+ def _decompress_response(self, body, headers):
"""
Decompress a response body if it is using deflate or gzip encoding.
+ :param body: Response body.
+ :type body: ``str``
+
+ :param headers: Response headers.
+ :type headers: ``dict``
+
:return: Decompressed response
:rtype: ``str``
"""
- headers = lowercase_keys(dict(response.getheaders()))
encoding = headers.get('content-encoding', None)
- # This attribute is set when using LoggingConnection
- original_data = getattr(response, '_original_data', None)
-
- if original_data is not None:
- # LoggingConnection decompresses data before we get into this
- # function so it can log decompressed body.
- # If this attribute is present, this means the body has already
- # been decompressed.
- return original_data
-
- body = response.read()
-
if encoding in ['zlib', 'deflate']:
body = decompress_data('zlib', body)
elif encoding in ['gzip', 'x-gzip']:
http://git-wip-us.apache.org/repos/asf/libcloud/blob/fe72fc13/libcloud/common/linode.py
----------------------------------------------------------------------
diff --git a/libcloud/common/linode.py b/libcloud/common/linode.py
index 6947d0a..9d953f0 100644
--- a/libcloud/common/linode.py
+++ b/libcloud/common/linode.py
@@ -80,21 +80,26 @@ class LinodeResponse(JsonResponse):
:keyword response: The raw response returned by urllib
:return: parsed :class:`LinodeResponse`"""
- self.body = self._decompress_response(response=response)
- if PY3:
- self.body = b(self.body).decode('utf-8')
+ self.connection = connection
- self.status = response.status
self.headers = dict(response.getheaders())
self.error = response.reason
- self.connection = connection
+ self.status = response.status
+
+ self.body = self._decompress_response(body=response.read(),
+ headers=self.headers)
+
+ if PY3:
+ self.body = b(self.body).decode('utf-8')
+
self.invalid = LinodeException(0xFF,
"Invalid JSON received from server")
# Move parse_body() to here; we can't be sure of failure until we've
# parsed the body into JSON.
self.objects, self.errors = self.parse_body()
+
if not self.success():
# Raise the first error, as there will usually only be one
raise self.errors[0]
|