Skip to content

Commit a5d8442

Browse files
committed
base api: Make per request timeouts overridable
Leave as default (-1) to fall back to timeout set in base api instance. Set to None to disable timing out altogether.
1 parent 0182d41 commit a5d8442

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

upcloud_api/cloud_manager/base.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def __init__(self, token, timeout=None): # noqa
1818
self.token = token
1919
self.timeout = timeout
2020

21-
def request(self, method, endpoint, body=None):
21+
def request(self, method, endpoint, body=None, timeout=-1):
2222
"""
2323
Perform a request with a given body to a given endpoint in UpCloud's API.
2424
@@ -38,11 +38,13 @@ def request(self, method, endpoint, body=None):
3838
else:
3939
json_body_or_None = None
4040

41+
call_timeout = timeout if timeout != -1 else self.timeout
42+
4143
APIcall = getattr(requests, method.lower())
4244
res = APIcall('https://api.upcloud.com' + url,
4345
data=json_body_or_None,
4446
headers=headers,
45-
timeout=self.timeout)
47+
timeout=call_timeout)
4648

4749
if res.text:
4850
res_json = res.json()
@@ -51,17 +53,17 @@ def request(self, method, endpoint, body=None):
5153

5254
return self.__error_middleware(res, res_json)
5355

54-
def get_request(self, endpoint):
56+
def get_request(self, endpoint, timeout=-1):
5557
"""
5658
Perform a GET request to a given endpoint in UpCloud's API.
5759
"""
58-
return self.request('GET', endpoint)
60+
return self.request('GET', endpoint, timeout=timeout)
5961

60-
def post_request(self, endpoint, body=None):
62+
def post_request(self, endpoint, body=None, timeout=-1):
6163
"""
6264
Perform a POST request to a given endpoint in UpCloud's API.
6365
"""
64-
return self.request('POST', endpoint, body)
66+
return self.request('POST', endpoint, body, timeout)
6567

6668
def __error_middleware(self, res, res_json):
6769
"""

0 commit comments

Comments
 (0)