[Python-modules-commits] [python-digitalocean] 09/19: Don't use the parameters to pass the timeout, but an env variable.
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 b2da2e0af2e17e7c5e1721d8cc9397ae86456bec
Author: Joro <acev at chessdom.com>
Date: Mon Oct 10 14:02:31 2016 -0700
Don't use the parameters to pass the timeout, but an env variable.
---
digitalocean/Metadata.py | 4 ++--
digitalocean/baseapi.py | 20 +++++++++-----------
2 files changed, 11 insertions(+), 13 deletions(-)
diff --git a/digitalocean/Metadata.py b/digitalocean/Metadata.py
index 5fc473f..73623c9 100644
--- a/digitalocean/Metadata.py
+++ b/digitalocean/Metadata.py
@@ -27,8 +27,8 @@ class Metadata(BaseAPI):
"""
url = urljoin(self.end_point, url)
- timeout = self.extract_timeout(params)
- response = requests.get(url, headers=headers, params=params, timeout=timeout)
+ 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 8800ecd..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):
@@ -56,8 +58,6 @@ 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")
@@ -81,6 +81,8 @@ class BaseAPI(object):
requests_method, headers, payload, transform = lookup[type]
headers.update({'Authorization': 'Bearer ' + self.token})
kwargs = {'headers': headers, payload: transform(params)}
+
+ timeout = self.get_timeout()
if timeout:
kwargs['timeout'] = timeout
@@ -91,17 +93,13 @@ class BaseAPI(object):
return requests_method(url, **kwargs)
- def extract_timeout(self, params):
+ def get_timeout(self):
"""
- 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.
+ Checks if any timeout for the requests to DigitalOcean is required.
+ To set a timeout, use the REQUEST_TIMEOUT_ENV_VAR environment
+ variable.
"""
- timeout = None
- if 'timeout' in params:
- timeout = params['timeout']
- del params['timeout']
- return timeout
+ return os.environ.get(REQUEST_TIMEOUT_ENV_VAR)
def get_data(self, url, type=GET, params=None):
"""
--
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