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): """ 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):