[Python-modules-commits] [python-digitalocean] 08/19: Adding support for request timeout to the library.

Andrew Starr-Bochicchio asb at moszumanska.debian.org
Tue Jun 20 00:31:28 UTC 2017


This is an automated email from the git hooks/post-receive script.

asb pushed a commit to annotated tag 1.10.1
in repository python-digitalocean.

commit fae81bb3a296b0e06215e13645eb8124758bf12b
Author: Joro <acev at chessdom.com>
Date:   Mon Oct 10 13:34:31 2016 -0700

    Adding support for request timeout to the library.
---
 digitalocean/Metadata.py |  3 ++-
 digitalocean/baseapi.py  | 20 ++++++++++++++++++--
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/digitalocean/Metadata.py b/digitalocean/Metadata.py
index 12e865b..5fc473f 100644
--- a/digitalocean/Metadata.py
+++ b/digitalocean/Metadata.py
@@ -27,7 +27,8 @@ class Metadata(BaseAPI):
         """
         url = urljoin(self.end_point, url)
 
-        response = requests.get(url, headers=headers, params=params)
+        timeout = self.extract_timeout(params)
+        response = requests.get(url, headers=headers, params=params, timeout=timeout)
 
         if render_json:
             return response.json()
diff --git a/digitalocean/baseapi.py b/digitalocean/baseapi.py
index adca55d..8800ecd 100644
--- a/digitalocean/baseapi.py
+++ b/digitalocean/baseapi.py
@@ -56,6 +56,8 @@ class BaseAPI(object):
         if params is None:
             params = {}
 
+        timeout = self.extract_timeout(params)
+
         if not self.token:
             raise TokenError("No token provided. Please use a valid token")
 
@@ -79,14 +81,28 @@ class BaseAPI(object):
         requests_method, headers, payload, transform = lookup[type]
         headers.update({'Authorization': 'Bearer ' + self.token})
         kwargs = {'headers': headers, payload: transform(params)}
+        if timeout:
+            kwargs['timeout'] = timeout
 
         # remove token from log
         headers_str = str(headers).replace(self.token.strip(), 'TOKEN')
-        self._log.debug('%s %s %s:%s %s' %
-                        (type, url, payload, params, headers_str))
+        self._log.debug('%s %s %s:%s %s %s' %
+                        (type, url, payload, params, headers_str, timeout))
 
         return requests_method(url, **kwargs)
 
+    def extract_timeout(self, params):
+        """
+            This method checks if there is a timeout specified in the params for
+            the request and extracts it from there. As a result, the params hash
+            is modified and the 'timeout' key is deleted.
+        """
+        timeout = None
+        if 'timeout' in params:
+            timeout = params['timeout']
+            del params['timeout']
+        return timeout
+
     def get_data(self, url, type=GET, params=None):
         """
             This method is a basic implementation of __call_api that checks

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/python-modules/packages/python-digitalocean.git



More information about the Python-modules-commits mailing list