From 8faeb810a29934e6e2ed82fd490644a8e18728dd Mon Sep 17 00:00:00 2001 From: gtalarico Date: Sat, 14 Mar 2020 23:41:29 -0700 Subject: [PATCH] Configurable requests timeout - fixes #64 --- HISTORY.md | 2 +- airtable/airtable.py | 31 +++++++++++++++++++++++++------ 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/HISTORY.md b/HISTORY.md index 0d9b525e..043838bd 100644 --- a/HISTORY.md +++ b/HISTORY.md @@ -1,6 +1,6 @@ # 0.14.0 * Removed `mirror()` method. -* +* Feature: Configurable request timeout # 0.13.0 * Fixed: Python 2 compatibility issues diff --git a/airtable/airtable.py b/airtable/airtable.py index 40dcc7bc..c2150652 100644 --- a/airtable/airtable.py +++ b/airtable/airtable.py @@ -113,10 +113,24 @@ class Airtable(object): API_LIMIT = 1.0 / 5 # 5 per second API_URL = posixpath.join(API_BASE_URL, VERSION) - def __init__(self, base_key, table_name, api_key=None): + def __init__(self, base_key, table_name, api_key=None, timeout=None): """ - If api_key is not provided, :any:`AirtableAuth` will attempt - to use ``os.environ['AIRTABLE_API_KEY']`` + Instantiates a new Airtable instance + + >>> table = Airtable('basekey', "tablename") + + Args: + base_key(``str``): Airtable base identifier + table_name(``str``): Airtable table name. Value will be url encoded, so + use value as shown in Airtable. + + Keyword Args: + api_key (``str``, optional): Optional API key. If not provided, + it will attempt to use ``os.environ['AIRTABLE_API_KEY']`` + timeout (``int``, ``Tuple[int, int]``, optional): Optional timeout + parameters to be used in request. `See requests timeout docs. + `_ + """ session = requests.Session() session.auth = AirtableAuth(api_key=api_key) @@ -124,6 +138,7 @@ def __init__(self, base_key, table_name, api_key=None): self.table_name = table_name url_safe_table_name = quote(table_name, safe="") self.url_table = posixpath.join(self.API_URL, base_key, url_safe_table_name) + self.timeout = timeout def _process_params(self, params): """ @@ -166,7 +181,9 @@ def record_url(self, record_id): return posixpath.join(self.url_table, record_id) def _request(self, method, url, params=None, json_data=None): - response = self.session.request(method, url, params=params, json=json_data) + response = self.session.request( + method, url, params=params, json=json_data, timeout=self.timeout + ) return self._process_response(response) def _get(self, url, **params): @@ -212,7 +229,8 @@ def get_iter(self, **options): ... print(record) [{'fields': ... }, ...] - Keyword Args: + + Keyword Args: max_records (``int``, optional): The maximum total number of records that will be returned. See :any:`MaxRecordsParam` view (``str``, optional): The name or ID of a view. @@ -250,7 +268,8 @@ def get_all(self, **options): >>> airtable.get_all(maxRecords=50) [{'fields': ... }, ...] - Keyword Args: + + Keyword Args: max_records (``int``, optional): The maximum total number of records that will be returned. See :any:`MaxRecordsParam` view (``str``, optional): The name or ID of a view.