Skip to content

Commit 8faeb81

Browse files
committed
Configurable requests timeout - fixes gtalarico#64
1 parent 3bded15 commit 8faeb81

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

HISTORY.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# 0.14.0
22
* Removed `mirror()` method.
3-
*
3+
* Feature: Configurable request timeout
44

55
# 0.13.0
66
* Fixed: Python 2 compatibility issues

airtable/airtable.py

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,17 +113,32 @@ class Airtable(object):
113113
API_LIMIT = 1.0 / 5 # 5 per second
114114
API_URL = posixpath.join(API_BASE_URL, VERSION)
115115

116-
def __init__(self, base_key, table_name, api_key=None):
116+
def __init__(self, base_key, table_name, api_key=None, timeout=None):
117117
"""
118-
If api_key is not provided, :any:`AirtableAuth` will attempt
119-
to use ``os.environ['AIRTABLE_API_KEY']``
118+
Instantiates a new Airtable instance
119+
120+
>>> table = Airtable('basekey', "tablename")
121+
122+
Args:
123+
base_key(``str``): Airtable base identifier
124+
table_name(``str``): Airtable table name. Value will be url encoded, so
125+
use value as shown in Airtable.
126+
127+
Keyword Args:
128+
api_key (``str``, optional): Optional API key. If not provided,
129+
it will attempt to use ``os.environ['AIRTABLE_API_KEY']``
130+
timeout (``int``, ``Tuple[int, int]``, optional): Optional timeout
131+
parameters to be used in request. `See requests timeout docs.
132+
<https://requests.readthedocs.io/en/master/user/advanced/#timeouts>`_
133+
120134
"""
121135
session = requests.Session()
122136
session.auth = AirtableAuth(api_key=api_key)
123137
self.session = session
124138
self.table_name = table_name
125139
url_safe_table_name = quote(table_name, safe="")
126140
self.url_table = posixpath.join(self.API_URL, base_key, url_safe_table_name)
141+
self.timeout = timeout
127142

128143
def _process_params(self, params):
129144
"""
@@ -166,7 +181,9 @@ def record_url(self, record_id):
166181
return posixpath.join(self.url_table, record_id)
167182

168183
def _request(self, method, url, params=None, json_data=None):
169-
response = self.session.request(method, url, params=params, json=json_data)
184+
response = self.session.request(
185+
method, url, params=params, json=json_data, timeout=self.timeout
186+
)
170187
return self._process_response(response)
171188

172189
def _get(self, url, **params):
@@ -212,7 +229,8 @@ def get_iter(self, **options):
212229
... print(record)
213230
[{'fields': ... }, ...]
214231
215-
Keyword Args:
232+
233+
Keyword Args:
216234
max_records (``int``, optional): The maximum total number of
217235
records that will be returned. See :any:`MaxRecordsParam`
218236
view (``str``, optional): The name or ID of a view.
@@ -250,7 +268,8 @@ def get_all(self, **options):
250268
>>> airtable.get_all(maxRecords=50)
251269
[{'fields': ... }, ...]
252270
253-
Keyword Args:
271+
272+
Keyword Args:
254273
max_records (``int``, optional): The maximum total number of
255274
records that will be returned. See :any:`MaxRecordsParam`
256275
view (``str``, optional): The name or ID of a view.

0 commit comments

Comments
 (0)