From 51ea22663373aa9a641d4cf04c2c8d4184d6d2bc Mon Sep 17 00:00:00 2001 From: Alex Golec Date: Sun, 5 Jun 2022 10:53:21 -0400 Subject: [PATCH] Sets default timeout to 30s and adds timeout management (#315) --- docs/client.rst | 13 +++++++++++++ tda/client/base.py | 14 ++++++++++++++ tests/client_test.py | 13 +++++++++++-- 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/docs/client.rst b/docs/client.rst index fbfe37f3..086f5ec0 100644 --- a/docs/client.rst +++ b/docs/client.rst @@ -145,6 +145,19 @@ own, the constructor looks like this: .. automethod:: tda.client.Client.__init__ + +++++++++++++++++++ +Timeout Management +++++++++++++++++++ + +Timeouts for HTTP calls are managed under the hood by the ``httpx`` library. +``tda-api`` defaults to 30 seconds, which experience has shown should be more +than enough to allow even the slowest API calls to complete. A different timeout +specification can be set using this method: + +.. automethod:: tda.client.Client.set_timeout + + .. _orders-section: ++++++ diff --git a/tda/client/base.py b/tda/client/base.py index 0ffffe37..95076fc6 100644 --- a/tda/client/base.py +++ b/tda/client/base.py @@ -49,6 +49,9 @@ def __init__(self, api_key, session, *, enforce_enums=True, self.token_metadata = token_metadata + # Set the default timeout configuration + self.set_timeout(30.0) + # XXX: This class's tests perform monkey patching to inject synthetic values # of utcnow(). To avoid being confused by this, capture these values here so # we can use them later. @@ -117,6 +120,17 @@ def ensure_updated_refresh_token(self, update_interval_seconds=None): return new_session is not None + def set_timeout(self, timeout): + '''Sets the timeout configuration for this client. Applies to all HTTP + calls. + + :param timeout: ``httpx`` timeout configuration. Passed directly to + underlying ``httpx`` library. See + `here `__ for + examples.''' + self.session.timeout = timeout + ########################################################################## # Orders diff --git a/tests/client_test.py b/tests/client_test.py index 264411dc..7c88ed93 100644 --- a/tests/client_test.py +++ b/tests/client_test.py @@ -76,12 +76,21 @@ def make_url(self, path): watchlistId=WATCHLIST_ID) return 'https://api.tdameritrade.com' + path + + # Generic functionality + + + def test_set_timeout(self): + timeout = 'dummy' + self.client.set_timeout(timeout) + self.assertEqual(timeout, self.client.session.timeout) + + # get_order def test_get_order(self): - - thing = self.client.get_order(ORDER_ID, ACCOUNT_ID) + self.client.get_order(ORDER_ID, ACCOUNT_ID) self.mock_session.get.assert_called_once_with( self.make_url('/v1/accounts/{accountId}/orders/{orderId}'), params={})