Skip to content

Commit 6da57e0

Browse files
authored
Add 'read_timeout' option in client.py (#213)
* Added request_timeout property to client and parameter to its constructor. * Fixed client constructor comments * Formatted test_client.py * Request timeout is overwritten when a http_client is not specified.
1 parent faf1da9 commit 6da57e0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

arango/client.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ class ArangoClient:
5353
False: Do not verify TLS certificate.
5454
str: Path to a custom CA bundle file or directory.
5555
:type verify_override: Union[bool, str, None]
56+
:param request_timeout: This is the default request timeout (in seconds)
57+
for http requests issued by the client if the parameter http_client is
58+
not secified. The default value is 60.
59+
None: No timeout.
60+
int: Timeout value in seconds.
61+
:type request_timeout: Any
5662
"""
5763

5864
def __init__(
@@ -64,6 +70,7 @@ def __init__(
6470
serializer: Callable[..., str] = lambda x: dumps(x),
6571
deserializer: Callable[[str], Any] = lambda x: loads(x),
6672
verify_override: Union[bool, str, None] = None,
73+
request_timeout: Any = 60,
6774
) -> None:
6875
if isinstance(hosts, str):
6976
self._hosts = [host.strip("/") for host in hosts.split(",")]
@@ -80,7 +87,13 @@ def __init__(
8087
else:
8188
self._host_resolver = RoundRobinHostResolver(host_count, resolver_max_tries)
8289

90+
# Initializes the http client
8391
self._http = http_client or DefaultHTTPClient()
92+
# Sets the request timeout.
93+
# This call can only happen AFTER initializing the http client.
94+
if http_client is None:
95+
self.request_timeout = request_timeout
96+
8497
self._serializer = serializer
8598
self._deserializer = deserializer
8699
self._sessions = [self._http.create_session(h) for h in self._hosts]
@@ -117,6 +130,20 @@ def version(self) -> str:
117130
version: str = get_distribution("python-arango").version
118131
return version
119132

133+
@property
134+
def request_timeout(self) -> Any:
135+
"""Return the request timeout of the http client.
136+
137+
:return: Request timeout.
138+
:rtype: Any
139+
"""
140+
return self._http.REQUEST_TIMEOUT # type: ignore
141+
142+
# Setter for request_timeout
143+
@request_timeout.setter
144+
def request_timeout(self, value: Any) -> None:
145+
self._http.REQUEST_TIMEOUT = value # type: ignore
146+
120147
def db(
121148
self,
122149
name: str = "_system",

tests/test_client.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ def test_client_attributes():
5353
assert repr(client) == client_repr
5454
assert isinstance(client._host_resolver, RandomHostResolver)
5555

56+
client = ArangoClient(hosts=client_hosts, request_timeout=120)
57+
assert client.request_timeout == client._http.REQUEST_TIMEOUT == 120
58+
5659

5760
def test_client_good_connection(db, username, password):
5861
client = ArangoClient(hosts="http://127.0.0.1:8529")

0 commit comments

Comments
 (0)