From bbfa37d80f89583483baaac876c9dece8fe0e4fc Mon Sep 17 00:00:00 2001 From: Pat Kelly Date: Wed, 3 May 2017 10:05:46 -0700 Subject: [PATCH 1/3] Added logic for request timeouts --- plaid/client.py | 10 +++++++--- plaid/requester.py | 10 +++++----- 2 files changed, 12 insertions(+), 8 deletions(-) diff --git a/plaid/client.py b/plaid/client.py index 599b09806..152be2dcd 100644 --- a/plaid/client.py +++ b/plaid/client.py @@ -13,7 +13,7 @@ Sandbox, Transactions, ) -from plaid.requester import post_request +from plaid.requester import DEFAULT_TIMEOUT, post_request from plaid.utils import urljoin @@ -32,7 +32,8 @@ def __init__(self, secret, public_key, environment, - suppress_warnings=False): + suppress_warnings=False, + timeout=DEFAULT_TIMEOUT): ''' Initialize a client with credentials. @@ -42,12 +43,15 @@ def __init__(self, :arg str environment: One of ``sandbox``, ``development``, or ``production``. :arg bool suppress_warnings: Suppress Plaid warnings. + :arg int timeout: Timeout for API requests. + ''' self.client_id = client_id self.secret = secret self.public_key = public_key self.environment = environment self.suppress_warnings = suppress_warnings + self.timeout = timeout if self.environment == 'development' and not self.suppress_warnings: warnings.warn(''' @@ -93,5 +97,5 @@ def post_public_key(self, path, data): def _post(self, path, data): return post_request( urljoin('https://' + self.environment + '.plaid.com', path), - data=data, + data=data, timeout=self.timeout ) diff --git a/plaid/requester.py b/plaid/requester.py index 781b4cb92..f1cbe0dc1 100644 --- a/plaid/requester.py +++ b/plaid/requester.py @@ -8,10 +8,10 @@ ALLOWED_METHODS = {'post'} -TIMEOUT = 600 # 10 minutes +DEFAULT_TIMEOUT = 600 # 10 minutes -def _requests_http_request(url, method, data): +def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT): normalized_method = method.lower() if normalized_method in ALLOWED_METHODS: return getattr(requests, normalized_method)( @@ -20,7 +20,7 @@ def _requests_http_request(url, method, data): headers={ 'User-Agent': 'Plaid Python v{}'.format(__version__), }, - timeout=TIMEOUT, + timeout=timeout, ) else: raise Exception( @@ -28,8 +28,8 @@ def _requests_http_request(url, method, data): ) -def http_request(url, method=None, data=None): - response = _requests_http_request(url, method, data or {}) +def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT): + response = _requests_http_request(url, method, data or {}, timeout) response_body = json.loads(response.text) if response_body.get('error_type'): raise PlaidError.from_response(response_body) From 0ec0ed075cd3bc13aa148bf14ca325003d8ab970 Mon Sep 17 00:00:00 2001 From: Patrick Kelly Date: Fri, 5 May 2017 10:38:16 -0700 Subject: [PATCH 2/3] Move timeout arg to new line --- plaid/client.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/plaid/client.py b/plaid/client.py index 152be2dcd..a8829cc34 100644 --- a/plaid/client.py +++ b/plaid/client.py @@ -97,5 +97,6 @@ def post_public_key(self, path, data): def _post(self, path, data): return post_request( urljoin('https://' + self.environment + '.plaid.com', path), - data=data, timeout=self.timeout + data=data, + timeout=self.timeout ) From 236054c60009ab58db5790f13e7bd6f7c453d0de Mon Sep 17 00:00:00 2001 From: Patrick Kelly Date: Fri, 5 May 2017 10:40:41 -0700 Subject: [PATCH 3/3] Add extra whitespace to satisfy PEP 8 --- plaid/requester.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plaid/requester.py b/plaid/requester.py index f1cbe0dc1..ae63029c5 100644 --- a/plaid/requester.py +++ b/plaid/requester.py @@ -8,7 +8,7 @@ ALLOWED_METHODS = {'post'} -DEFAULT_TIMEOUT = 600 # 10 minutes +DEFAULT_TIMEOUT = 600 # 10 minutes def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT):