Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions upcloud_api/cloud_manager/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer timeout to be None

Copy link
Copy Markdown
Member Author

@scop scop Jan 31, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The point for the default not to be None is that one can then call the method with timeout=None to disable the timeout altogether. (See complete commit message of the first commit.) That's consistent with how one can disable timeouts for the whole manager.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, works for me :D, merging

"""
Perform a request with a given body to a given endpoint in UpCloud's API.

Expand All @@ -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
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When timeout is None, this could be stated as: call_timeout = timeout or 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()
Expand All @@ -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):
"""
Expand Down
4 changes: 2 additions & 2 deletions upcloud_api/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down