Skip to content

Commit

Permalink
Sets default timeout to 30s and adds timeout management (#315)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgolec authored Jun 5, 2022
1 parent b1069e6 commit 51ea226
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/client.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:

++++++
Expand Down
14 changes: 14 additions & 0 deletions tda/client/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 <https://www.python-httpx.org/advanced/
#setting-a-default-timeout-on-a-client>`__ for
examples.'''
self.session.timeout = timeout

##########################################################################
# Orders

Expand Down
13 changes: 11 additions & 2 deletions tests/client_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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={})
Expand Down

0 comments on commit 51ea226

Please sign in to comment.