libcloud-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From anthonys...@apache.org
Subject [17/33] libcloud git commit: Setup the error handling for the underlying requests library
Date Tue, 05 Apr 2016 11:17:08 GMT
Setup the error handling for the underlying requests library


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

Branch: refs/heads/requests
Commit: 9be69cc0fa3b2563b315281a9dfa688fa9e16b36
Parents: 6326f98
Author: anthony-shaw <anthony.p.shaw@gmail.com>
Authored: Wed Mar 30 14:44:21 2016 +1100
Committer: anthony-shaw <anthony.p.shaw@gmail.com>
Committed: Wed Mar 30 14:44:21 2016 +1100

----------------------------------------------------------------------
 libcloud/common/base.py           | 24 +++++++-----------------
 libcloud/common/exceptions.py     |  1 -
 libcloud/test/__init__.py         | 21 +++++++++++++++++++--
 libcloud/test/test_connection.py  |  4 ++--
 libcloud/test/test_httplib_ssl.py |  2 +-
 5 files changed, 29 insertions(+), 23 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/9be69cc0/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index badce2c..40d9c39 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -32,6 +32,8 @@ try:
 except:
     import json
 
+import requests
+
 import libcloud
 
 from libcloud.utils.py3 import PY3, PY25
@@ -149,26 +151,13 @@ class Response(object):
         # names
         self.headers = lowercase_keys(dict(response.getheaders()))
         self.error = response.reason
-        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)
+        self.status = response.status_code
 
-        if PY3:
-            self.body = b(self.body).decode('utf-8')
+        self.body = response.text
 
         if not self.success():
             raise exception_from_message(code=self.status,
-                                         message=self.parse_error(),
-                                         headers=self.headers)
+                                         message=self.parse_error())
 
         self.object = self.parse_body()
 
@@ -204,7 +193,8 @@ class Response(object):
         :rtype: ``bool``
         :return: ``True`` or ``False``
         """
-        return self.status in [httplib.OK, httplib.CREATED]
+        return self.status in [requests.codes.ok, requests.codes.created,
+                               httplib.OK, httplib.CREATED]
 
     def _decompress_response(self, body, headers):
         """

http://git-wip-us.apache.org/repos/asf/libcloud/blob/9be69cc0/libcloud/common/exceptions.py
----------------------------------------------------------------------
diff --git a/libcloud/common/exceptions.py b/libcloud/common/exceptions.py
index 14dcea8..a286ccd 100644
--- a/libcloud/common/exceptions.py
+++ b/libcloud/common/exceptions.py
@@ -70,6 +70,5 @@ def exception_from_message(code, message, headers=None):
 
     if headers and 'retry_after' in headers:
         kwargs['retry_after'] = headers['retry_after']
-
     cls = _code_map.get(code, BaseHTTPError)
     return cls(**kwargs)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/9be69cc0/libcloud/test/__init__.py
----------------------------------------------------------------------
diff --git a/libcloud/test/__init__.py b/libcloud/test/__init__.py
index 747b02c..cfd076c 100644
--- a/libcloud/test/__init__.py
+++ b/libcloud/test/__init__.py
@@ -15,6 +15,7 @@
 
 import sys
 import random
+import requests
 
 from libcloud.utils.py3 import httplib
 from libcloud.utils.py3 import StringIO
@@ -78,14 +79,14 @@ class MockResponse(object):
     A mock HTTPResponse
     """
     headers = {}
-    body = StringIO()
+    body = None
     status = 0
     reason = ''
     version = 11
 
     def __init__(self, status, body=None, headers=None, reason=None):
         self.status = status
-        self.body = StringIO(u(body)) if body else StringIO()
+        self.body = body
         self.headers = headers or self.headers
         self.reason = reason or self.reason
 
@@ -110,6 +111,17 @@ class MockResponse(object):
     def msg(self):
         raise NotImplemented
 
+    @property
+    def status_code(self):
+        return self.status
+
+    def raise_for_status(self):
+        raise requests.exceptions.HTTPError(self.status)
+
+    @property
+    def text(self):
+        return self.body
+
 
 class BaseMockHttpObject(object):
     def _get_method_name(self, type, use_param, qs, path):
@@ -327,6 +339,11 @@ class MockRawResponse(BaseMockHttpObject):
         return self._status
 
     @property
+    def status_code(self):
+        self._get_response_if_not_availale()
+        return self._status
+
+    @property
     def headers(self):
         self._get_response_if_not_availale()
         return self._headers

http://git-wip-us.apache.org/repos/asf/libcloud/blob/9be69cc0/libcloud/test/test_connection.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_connection.py b/libcloud/test/test_connection.py
index 5a97b45..53cd979 100644
--- a/libcloud/test/test_connection.py
+++ b/libcloud/test/test_connection.py
@@ -86,7 +86,7 @@ class BaseConnectionClassTestCase(unittest.TestCase):
 
         proxy_url = 'http://127.0.0.4:3128'
         conn = LibcloudConnection(host='localhost', port=80,
-                                      proxy_url=proxy_url)
+                                  proxy_url=proxy_url)
         self.assertEqual(conn.proxy_scheme, 'http')
         self.assertEqual(conn.proxy_host, '127.0.0.4')
         self.assertEqual(conn.proxy_port, 3128)
@@ -94,7 +94,7 @@ class BaseConnectionClassTestCase(unittest.TestCase):
         os.environ['http_proxy'] = proxy_url
         proxy_url = 'http://127.0.0.5:3128'
         conn = LibcloudConnection(host='localhost', port=80,
-                                      proxy_url=proxy_url)
+                                  proxy_url=proxy_url)
         self.assertEqual(conn.proxy_scheme, 'http')
         self.assertEqual(conn.proxy_host, '127.0.0.5')
         self.assertEqual(conn.proxy_port, 3128)

http://git-wip-us.apache.org/repos/asf/libcloud/blob/9be69cc0/libcloud/test/test_httplib_ssl.py
----------------------------------------------------------------------
diff --git a/libcloud/test/test_httplib_ssl.py b/libcloud/test/test_httplib_ssl.py
index 8f5885d..55d1b23 100644
--- a/libcloud/test/test_httplib_ssl.py
+++ b/libcloud/test/test_httplib_ssl.py
@@ -36,7 +36,7 @@ class TestHttpLibSSLTests(unittest.TestCase):
     def setUp(self):
         libcloud.security.VERIFY_SSL_CERT = False
         libcloud.security.CA_CERTS_PATH = ORIGINAL_CA_CERS_PATH
-        self.httplib_object = LibcloudConnection('foo.bar')
+        self.httplib_object = LibcloudConnection('foo.bar', port=80)
 
     def test_custom_ca_path_using_env_var_doesnt_exist(self):
         os.environ['SSL_CERT_FILE'] = '/foo/doesnt/exist'


Mime
View raw message