Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,16 @@ client = seatsio.Client(seatsio.Region.EU(), secret_key="my-workspace-secret-key
```

Passing in 0 disables exponential backoff completely. In that case, the client will never retry a failed request.

## Timeouts

By default, the client waits up to 10 seconds to connect to the server, and up to 30 seconds for the server to respond.

To change this, pass a `timeout` when creating the `Client`. It accepts a single number of seconds, or a `(connect, read)` tuple:

```python
import seatsio
client = seatsio.Client(seatsio.Region.EU(), secret_key="my-workspace-secret-key", timeout=(10, 60))
```

Requests that time out are not retried.
4 changes: 2 additions & 2 deletions seatsio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@


class Client:
def __init__(self, region, secret_key, workspace_key=None, max_retries=5):
def __init__(self, region, secret_key, workspace_key=None, max_retries=5, timeout=(10, 30)):
base_url = region.url
self.base_url = base_url
self.http_client = HttpClient(base_url, secret_key, workspace_key, max_retries)
self.http_client = HttpClient(base_url, secret_key, workspace_key, max_retries, timeout)
self.charts = ChartsClient(self.http_client)
self.events = EventsClient(self.http_client)
self.seasons = SeasonsClient(self.http_client, self)
Expand Down
2 changes: 1 addition & 1 deletion seatsio/httpClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def handle_error(request, response):


class HttpClient:
def __init__(self, base_url, secret_key, workspace_key, max_retries, timeout=10):
def __init__(self, base_url, secret_key, workspace_key, max_retries, timeout=(10, 30)):
self.base_url = base_url
self.secret_key = secret_key
self.workspace_key = workspace_key
Expand Down
36 changes: 36 additions & 0 deletions tests/testTimeout.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import unittest
from unittest.mock import patch, MagicMock

import seatsio
from seatsio.httpClient import HttpClient
from tests.util.asserts import assert_that


def ok_response():
response = MagicMock()
response.status_code = 200
response.json.return_value = {}
return response


class TimeoutTest(unittest.TestCase):

def test_default_timeout(self):
with patch("seatsio.httpClient.requests.get", return_value=ok_response()) as get:
HttpClient("http://localhost", "aSecretKey", None, 5).url("/ping").get()
assert_that(get.call_args.kwargs["timeout"]).is_equal_to((10, 30))

def test_client_default_timeout(self):
with patch("seatsio.httpClient.requests.get", return_value=ok_response()) as get:
seatsio.Client(seatsio.Region.EU(), "aSecretKey").http_client.url("/ping").get()
assert_that(get.call_args.kwargs["timeout"]).is_equal_to((10, 30))

def test_client_custom_timeout(self):
with patch("seatsio.httpClient.requests.get", return_value=ok_response()) as get:
seatsio.Client(seatsio.Region.EU(), "aSecretKey", timeout=(5, 60)).http_client.url("/ping").get()
assert_that(get.call_args.kwargs["timeout"]).is_equal_to((5, 60))

def test_client_custom_timeout_applies_to_post(self):
with patch("seatsio.httpClient.requests.post", return_value=ok_response()) as post:
seatsio.Client(seatsio.Region.EU(), "aSecretKey", timeout=45).http_client.url("/ping").post()
assert_that(post.call_args.kwargs["timeout"]).is_equal_to(45)
Loading