[Python-modules-commits] [python-digitalocean] 10/19: Adding support for request timeout to the library. (#168)

Andrew Starr-Bochicchio asb at moszumanska.debian.org
Tue Jun 20 00:31:29 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 fc0657f6931dc73ea6dacd9c8caa58bfbaa629e1
Author: Georgi Atsev <goodcode at users.noreply.github.com>
Date:   Mon Oct 10 22:39:28 2016 -0700

    Adding support for request timeout to the library. (#168)
    
    * Adding support for request timeout to the library.
    
    * Don't use the parameters to pass the timeout, but an env variable.
---
 digitalocean/Metadata.py |  3 ++-
 digitalocean/baseapi.py  | 18 ++++++++++++++++--
 2 files changed, 18 insertions(+), 3 deletions(-)

diff --git a/digitalocean/Metadata.py b/digitalocean/Metadata.py
index 12e865b..73623c9 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)
+        response = requests.get(url, headers=headers, params=params,
+                                timeout=self.get_timeout())
 
         if render_json:
             return response.json()
diff --git a/digitalocean/baseapi.py b/digitalocean/baseapi.py
index adca55d..a8b4db5 100644
--- a/digitalocean/baseapi.py
+++ b/digitalocean/baseapi.py
@@ -1,4 +1,5 @@
 # -*- coding: utf-8 -*-
+import os
 import json
 import logging
 import requests
@@ -12,6 +13,7 @@ GET = 'GET'
 POST = 'POST'
 DELETE = 'DELETE'
 PUT = 'PUT'
+REQUEST_TIMEOUT_ENV_VAR = 'PYTHON_DIGITALOCEAN_REQUEST_TIMEOUT_SEC'
 
 
 class Error(Exception):
@@ -80,13 +82,25 @@ class BaseAPI(object):
         headers.update({'Authorization': 'Bearer ' + self.token})
         kwargs = {'headers': headers, payload: transform(params)}
 
+        timeout = self.get_timeout()
+        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 get_timeout(self):
+        """
+            Checks if any timeout for the requests to DigitalOcean is required.
+            To set a timeout, use the REQUEST_TIMEOUT_ENV_VAR environment
+            variable.
+        """
+        return os.environ.get(REQUEST_TIMEOUT_ENV_VAR)
+
     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