You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@libcloud.apache.org by an...@apache.org on 2017/01/09 04:52:51 UTC

[31/51] [abbrv] libcloud git commit: support raw streams by using requests' pre-prepared Request class. Drop inheritance from the httplib class. Use iter_content() in requests for download streams

support raw streams by using requests' pre-prepared Request class. Drop inheritance from the httplib class. Use iter_content() in requests for download streams


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

Branch: refs/heads/trunk
Commit: c078b6873d77f9f0ae338820d9c0ab4b59a8bf9d
Parents: 4881364
Author: Anthony Shaw <an...@apache.org>
Authored: Thu Jan 5 17:39:42 2017 +1100
Committer: Anthony Shaw <an...@apache.org>
Committed: Thu Jan 5 17:39:42 2017 +1100

----------------------------------------------------------------------
 libcloud/common/base.py  | 17 +++++++----------
 libcloud/httplib_ssl.py  | 14 +++++++++++++-
 libcloud/storage/base.py |  4 ++--
 3 files changed, 22 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/libcloud/blob/c078b687/libcloud/common/base.py
----------------------------------------------------------------------
diff --git a/libcloud/common/base.py b/libcloud/common/base.py
index eb3875f..9778ca9 100644
--- a/libcloud/common/base.py
+++ b/libcloud/common/base.py
@@ -285,7 +285,7 @@ class RawResponse(Response):
     def response(self):
         if not self._response:
             response = self.connection.connection.getresponse()
-            self._response, self.body = response, response
+            self._response, self.body = response, response.text
             if not self.success():
                 self.parse_error()
         return self._response
@@ -293,7 +293,7 @@ class RawResponse(Response):
     @property
     def status(self):
         if not self._status:
-            self._status = self.response.status
+            self._status = self.response.status_code
         return self._status
 
     @property
@@ -604,14 +604,11 @@ class Connection(object):
             # @TODO: Should we just pass File object as body to request method
             # instead of dealing with splitting and sending the file ourselves?
             if raw:
-                self.connection.putrequest(method, url,
-                                           skip_host=1,
-                                           skip_accept_encoding=1)
-
-                for key, value in list(headers.items()):
-                    self.connection.putheader(key, str(value))
-
-                self.connection.endheaders()
+                self.connection.prepared_request(
+                    method=method,
+                    url=url,
+                    body=data,
+                    headers=headers)
             else:
                 if retry_enabled:
                     retry_request = retry(timeout=self.timeout,

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c078b687/libcloud/httplib_ssl.py
----------------------------------------------------------------------
diff --git a/libcloud/httplib_ssl.py b/libcloud/httplib_ssl.py
index 9dd3feb..1955855 100644
--- a/libcloud/httplib_ssl.py
+++ b/libcloud/httplib_ssl.py
@@ -153,7 +153,7 @@ class LibcloudBaseConnection(object):
             self.ca_cert = libcloud.security.CA_CERTS_PATH
 
 
-class LibcloudConnection(httplib.HTTPSConnection, LibcloudBaseConnection):
+class LibcloudConnection(LibcloudBaseConnection):
     timeout = None
     host = None
     response = None
@@ -187,6 +187,18 @@ class LibcloudConnection(httplib.HTTPSConnection, LibcloudBaseConnection):
             verify=self.ca_cert if self.ca_cert is not None else self.verify
         )
 
+    def prepared_request(self, method, url, body=None, headers=None, raw=False):
+        req = requests.Request(method, ''.join([self.host, url]), data=body, headers=headers)
+        
+        prepped = self.session.prepare_request(req)
+
+        prepped.body = body
+        
+        self.response = self.session.send(prepped,
+            stream=raw,
+            verify=self.ca_cert if self.ca_cert is not None else self.verify
+        )
+
     def getresponse(self):
         return self.response
 

http://git-wip-us.apache.org/repos/asf/libcloud/blob/c078b687/libcloud/storage/base.py
----------------------------------------------------------------------
diff --git a/libcloud/storage/base.py b/libcloud/storage/base.py
index f13dd0a..4bfe209 100644
--- a/libcloud/storage/base.py
+++ b/libcloud/storage/base.py
@@ -557,7 +557,7 @@ class StorageDriver(BaseDriver):
                 'overwrite_existing=False',
                 driver=self)
 
-        stream = libcloud.utils.files.read_in_chunks(response, chunk_size)
+        stream = response.iter_content(chunk_size)
 
         try:
             data_read = next(stream)
@@ -642,7 +642,7 @@ class StorageDriver(BaseDriver):
             upload_func_kwargs['chunked'] = False
 
         if file_size is not None and 'Content-Length' not in headers:
-            headers['Content-Length'] = file_size
+            headers['Content-Length'] = str(file_size)
 
         headers['Content-Type'] = content_type
         response = self.connection.request(request_path,