Skip to content

Commit 1aa6361

Browse files
patrick7kellymichaelckelly
authored andcommitted
Added logic for request timeouts (#90)
* Added logic for request timeouts * Move timeout arg to new line * Add extra whitespace to satisfy PEP 8
1 parent ae8c51c commit 1aa6361

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

plaid/client.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
Sandbox,
1414
Transactions,
1515
)
16-
from plaid.requester import post_request
16+
from plaid.requester import DEFAULT_TIMEOUT, post_request
1717
from plaid.utils import urljoin
1818

1919

@@ -32,7 +32,8 @@ def __init__(self,
3232
secret,
3333
public_key,
3434
environment,
35-
suppress_warnings=False):
35+
suppress_warnings=False,
36+
timeout=DEFAULT_TIMEOUT):
3637
'''
3738
Initialize a client with credentials.
3839
@@ -42,12 +43,15 @@ def __init__(self,
4243
:arg str environment: One of ``sandbox``,
4344
``development``, or ``production``.
4445
:arg bool suppress_warnings: Suppress Plaid warnings.
46+
:arg int timeout: Timeout for API requests.
47+
4548
'''
4649
self.client_id = client_id
4750
self.secret = secret
4851
self.public_key = public_key
4952
self.environment = environment
5053
self.suppress_warnings = suppress_warnings
54+
self.timeout = timeout
5155

5256
if self.environment == 'development' and not self.suppress_warnings:
5357
warnings.warn('''
@@ -94,4 +98,5 @@ def _post(self, path, data):
9498
return post_request(
9599
urljoin('https://' + self.environment + '.plaid.com', path),
96100
data=data,
101+
timeout=self.timeout
97102
)

plaid/requester.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@
88

99

1010
ALLOWED_METHODS = {'post'}
11-
TIMEOUT = 600 # 10 minutes
11+
DEFAULT_TIMEOUT = 600 # 10 minutes
1212

1313

14-
def _requests_http_request(url, method, data):
14+
def _requests_http_request(url, method, data, timeout=DEFAULT_TIMEOUT):
1515
normalized_method = method.lower()
1616
if normalized_method in ALLOWED_METHODS:
1717
return getattr(requests, normalized_method)(
@@ -20,16 +20,16 @@ def _requests_http_request(url, method, data):
2020
headers={
2121
'User-Agent': 'Plaid Python v{}'.format(__version__),
2222
},
23-
timeout=TIMEOUT,
23+
timeout=timeout,
2424
)
2525
else:
2626
raise Exception(
2727
'Invalid request method {}'.format(method)
2828
)
2929

3030

31-
def http_request(url, method=None, data=None):
32-
response = _requests_http_request(url, method, data or {})
31+
def http_request(url, method=None, data=None, timeout=DEFAULT_TIMEOUT):
32+
response = _requests_http_request(url, method, data or {}, timeout)
3333
response_body = json.loads(response.text)
3434
if response_body.get('error_type'):
3535
raise PlaidError.from_response(response_body)

0 commit comments

Comments
 (0)