From a5d84429462afc9a067ad4be85d3bc961d357ac3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 30 Jan 2018 12:42:58 +0200 Subject: [PATCH 1/2] 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. --- upcloud_api/cloud_manager/base.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/upcloud_api/cloud_manager/base.py b/upcloud_api/cloud_manager/base.py index d8ae4c7..3fa1e07 100644 --- a/upcloud_api/cloud_manager/base.py +++ b/upcloud_api/cloud_manager/base.py @@ -18,7 +18,7 @@ def __init__(self, token, timeout=None): # noqa self.token = token self.timeout = timeout - def request(self, method, endpoint, body=None): + def request(self, method, endpoint, body=None, timeout=-1): """ Perform a request with a given body to a given endpoint in UpCloud's API. @@ -38,11 +38,13 @@ def request(self, method, endpoint, body=None): else: json_body_or_None = None + call_timeout = timeout if timeout != -1 else self.timeout + APIcall = getattr(requests, method.lower()) res = APIcall('https://api.upcloud.com' + url, data=json_body_or_None, headers=headers, - timeout=self.timeout) + timeout=call_timeout) if res.text: res_json = res.json() @@ -51,17 +53,17 @@ def request(self, method, endpoint, body=None): return self.__error_middleware(res, res_json) - def get_request(self, endpoint): + def get_request(self, endpoint, timeout=-1): """ Perform a GET request to a given endpoint in UpCloud's API. """ - return self.request('GET', endpoint) + return self.request('GET', endpoint, timeout=timeout) - def post_request(self, endpoint, body=None): + def post_request(self, endpoint, body=None, timeout=-1): """ Perform a POST request to a given endpoint in UpCloud's API. """ - return self.request('POST', endpoint, body) + return self.request('POST', endpoint, body, timeout) def __error_middleware(self, res, res_json): """ From a46276b2dce104be340c3879a95c3b47e95d205f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ville=20Skytt=C3=A4?= Date: Tue, 30 Jan 2018 12:43:13 +0200 Subject: [PATCH 2/2] server: Run start() with a generous timeout by default The 10 second CloudManager default is not quite sufficient here. --- upcloud_api/server.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/upcloud_api/server.py b/upcloud_api/server.py index 93df193..17ffbb2 100644 --- a/upcloud_api/server.py +++ b/upcloud_api/server.py @@ -160,14 +160,14 @@ def stop(self): # noqa """ self.shutdown() - def start(self): + def start(self, timeout=120): """ Start the server. Note: slow and blocking request. The API waits for confirmation from UpCloud's IaaS backend before responding. """ path = '/server/{0}/start'.format(self.uuid) - self.cloud_manager.post_request(path) + self.cloud_manager.post_request(path, timeout=timeout) object.__setattr__(self, 'state', 'started') def restart(self, hard=False, timeout=30, force=True):