Skip to content

Commit 687061c

Browse files
bors[bot]sanders41
andauthored
Merge #232
232: Added an option to specify a timeout duration r=bidoubiwa a=sanders41 Closes #231 I did not implement this with the Session as suggested in the issue simply because in the requests documentation there doesn't seem to be a way to set a default timeout for the session. It is possible with httpx to do this so maybe I'm missing something in requests. If anyone knows how and would rather use a session I can update it. As an added bonus this does speed up the tests significantly be setting a timeout of 1 second on the communication error test. Co-authored-by: Paul Sanders <[email protected]>
2 parents 5e8cd05 + 143cac3 commit 687061c

File tree

6 files changed

+69
-7
lines changed

6 files changed

+69
-7
lines changed

meilisearch/_httprequests.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import requests
2-
from meilisearch.errors import MeiliSearchApiError, MeiliSearchCommunicationError
2+
from meilisearch.errors import (
3+
MeiliSearchApiError,
4+
MeiliSearchCommunicationError,
5+
MeiliSearchTimeoutError,
6+
)
37

48
class HttpRequests:
59

@@ -17,10 +21,22 @@ def send_request(self, http_method, path, body=None):
1721
try:
1822
request_path = self.config.url + '/' + path
1923
if body is None:
20-
request = http_method(request_path, headers=self.headers)
24+
request = http_method(
25+
request_path,
26+
timeout=self.config.timeout,
27+
headers=self.headers,
28+
)
2129
else:
22-
request = http_method(request_path, headers=self.headers, json=body)
30+
request = http_method(
31+
request_path,
32+
timeout=self.config.timeout,
33+
headers=self.headers,
34+
json=body,
35+
)
2336
return self.__validate(request)
37+
38+
except requests.exceptions.Timeout as err:
39+
raise MeiliSearchTimeoutError(err) from err
2440
except requests.exceptions.ConnectionError as err:
2541
raise MeiliSearchCommunicationError(err) from err
2642

meilisearch/client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class Client():
1414
config = None
1515
http = None
1616

17-
def __init__(self, url, apiKey=None):
17+
def __init__(self, url, apiKey=None, timeout=None):
1818
"""
1919
Parameters
2020
----------
@@ -23,7 +23,8 @@ def __init__(self, url, apiKey=None):
2323
apiKey : str
2424
The optional API key for MeiliSearch
2525
"""
26-
self.config = Config(url, apiKey)
26+
self.config = Config(url, apiKey, timeout=timeout)
27+
2728
self.http = HttpRequests(self.config)
2829

2930
def create_index(self, uid, options=None):

meilisearch/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class Paths():
2323
attributes_for_faceting = 'attributes-for-faceting'
2424
dumps = 'dumps'
2525

26-
def __init__(self, url, api_key=None):
26+
def __init__(self, url, api_key=None, timeout=None):
2727
"""
2828
Parameters
2929
----------
@@ -35,4 +35,5 @@ def __init__(self, url, api_key=None):
3535

3636
self.url = url
3737
self.api_key = api_key
38+
self.timeout = timeout
3839
self.paths = self.Paths()

meilisearch/tests/client/test_client.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,26 @@
33
import meilisearch
44
from meilisearch.tests import BASE_URL, MASTER_KEY
55

6+
67
def test_get_client():
78
"""Tests getting a client instance."""
89
client = meilisearch.Client(BASE_URL, MASTER_KEY)
910
assert client.config
1011
response = client.health()
1112
assert response.status_code >= 200 and response.status_code < 400
13+
14+
15+
def test_client_timeout_set():
16+
timeout = 5
17+
client = meilisearch.Client(BASE_URL, MASTER_KEY, timeout=timeout)
18+
response = client.health()
19+
assert client.config.timeout == timeout
20+
assert response.status_code >= 200 and response.status_code < 400
21+
22+
23+
def test_client_timeout_not_set():
24+
default_timeout = None
25+
client = meilisearch.Client(BASE_URL, MASTER_KEY)
26+
response = client.health()
27+
assert client.config.timeout == default_timeout
28+
assert response.status_code >= 200 and response.status_code < 400
Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
# pylint: disable=invalid-name
22

3+
from unittest.mock import patch
34
import pytest
5+
import requests
46
import meilisearch
57
from meilisearch.errors import MeiliSearchCommunicationError
68
from meilisearch.tests import MASTER_KEY
79

810

9-
def test_meilisearch_communication_error_host():
11+
@patch("requests.post")
12+
def test_meilisearch_communication_error_host(mock_post):
13+
mock_post.side_effect = requests.exceptions.ConnectionError()
1014
client = meilisearch.Client("http://wrongurl:1234", MASTER_KEY)
1115
with pytest.raises(MeiliSearchCommunicationError):
1216
client.create_index("some_index")
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import pytest
2+
import meilisearch
3+
from meilisearch.errors import MeiliSearchTimeoutError
4+
from meilisearch.tests import BASE_URL, MASTER_KEY
5+
6+
7+
@pytest.mark.usefixtures("indexes_sample")
8+
def test_client_timeout_error(small_movies):
9+
client = meilisearch.Client(BASE_URL, MASTER_KEY, timeout=1e-99)
10+
11+
with pytest.raises(MeiliSearchTimeoutError):
12+
index = client.index("indexUID")
13+
index.add_documents(small_movies)
14+
15+
16+
def test_client_timeout_set():
17+
timeout = 1
18+
client = meilisearch.Client("http://wrongurl:1234", MASTER_KEY, timeout=timeout)
19+
20+
with pytest.raises(Exception):
21+
client.health()
22+
23+
assert client.config.timeout == timeout

0 commit comments

Comments
 (0)