Skip to content

Commit

Permalink
Allow to specify timeout in client constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
leplatrem committed Sep 20, 2019
1 parent 441a9b2 commit f23ff92
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ This document describes changes between each past release.
10.6.0 (unreleased)
===================

- Nothing changed yet.
**New features**

- Specify requests timeout in client constructor


10.5.0 (2019-09-10)
Expand Down
24 changes: 24 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,30 @@ Failing operations will raise a ``KintoException``, which has ``request`` and ``
print("Not allowed!")
Requests Timeout
----------------

A ``timeout`` value in seconds can be specified in the client constructor:

.. code-block:: python
client = KintoClient(server_url="...", timeout=5)
To distinguish the connect from the read timeout, use a tuple:

.. code-block:: python
client = KintoClient(server_url="...", timeout=(3.05, 27))
For an infinit timeout, use ``None``:

.. code-block:: python
client = KintoClient(server_url="...", timeout=None)
See the `timeout documentation <https://2.python-requests.org//en/master/user/advanced/#timeouts>`_ of the underlying ``requests`` library.


Retry on error
--------------

Expand Down
4 changes: 3 additions & 1 deletion kinto_http/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,14 @@ def __init__(
collection=None,
retry=0,
retry_after=None,
timeout=None,
ignore_batch_4xx=False
):
self.endpoints = Endpoints()

session_kwargs = dict(
server_url=server_url, auth=auth, session=session, retry=retry, retry_after=retry_after
server_url=server_url, auth=auth, session=session, retry=retry, retry_after=retry_after,
timeout=timeout,
)
self.session = create_session(**session_kwargs)
self._bucket_name = bucket
Expand Down
9 changes: 8 additions & 1 deletion kinto_http/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
)


unset = object()


def create_session(server_url=None, auth=None, session=None, **kwargs):
"""Returns a session from the passed arguments.
Expand Down Expand Up @@ -49,12 +52,13 @@ class Session(object):
"""Handles all the interactions with the network.
"""

def __init__(self, server_url, auth=None, retry=0, retry_after=None):
def __init__(self, server_url, auth=None, timeout=unset, retry=0, retry_after=None):
self.backoff = None
self.server_url = server_url
self.auth = auth
self.nb_retry = retry
self.retry_after = retry_after
self.timeout = timeout

def request(self, method, endpoint, data=None, permissions=None, payload=None, **kwargs):
current_time = time.time()
Expand All @@ -68,6 +72,9 @@ def request(self, method, endpoint, data=None, permissions=None, payload=None, *
else:
actual_url = endpoint

if self.timeout != unset:
kwargs.setdefault("timeout", self.timeout)

if self.auth is not None:
kwargs.setdefault("auth", self.auth)

Expand Down
22 changes: 22 additions & 0 deletions kinto_http/tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,28 @@ def test_uses_specified_server_url(self):
session = Session(mock.sentinel.server_url)
self.assertEqual(session.server_url, mock.sentinel.server_url)

def test_timeout_can_be_set_to_none(self):
response = fake_response(200)
self.requests_mock.request.return_value = response
session = Session("https://example.org", timeout=None)
self.assertEqual(session.auth, None)
session.request("get", "/test")
self.requests_mock.request.assert_called_with(
"get", "https://example.org/test", timeout=None,
headers=self.requests_mock.request.headers
)

def test_timeout_can_be_set_to_value(self):
response = fake_response(200)
self.requests_mock.request.return_value = response
session = Session("https://example.org", timeout=4)
self.assertEqual(session.auth, None)
session.request("get", "/test")
self.requests_mock.request.assert_called_with(
"get", "https://example.org/test", timeout=4,
headers=self.requests_mock.request.headers
)

def test_no_auth_is_used_by_default(self):
response = fake_response(200)
self.requests_mock.request.return_value = response
Expand Down

0 comments on commit f23ff92

Please sign in to comment.