Skip to content

Commit

Permalink
Merge pull request #14 from UpCloudLtd/0.3.3-devel
Browse files Browse the repository at this point in the history
allow setting timeout (default 10s); bump to 0.3.3
  • Loading branch information
Elias Nygren committed Feb 8, 2016
2 parents 597f1d8 + b038268 commit a7ef273
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 10 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ python setup.py install
(http://stackoverflow.com/questions/29099404/ssl-insecureplatform-error-when-using-requests-package)


**Supported versions as of 0.3.0** (offline tests pass with tox):
**Supported versions as of 0.3.3** (offline tests pass with tox):

* python 2.6
* python 2.7
Expand Down Expand Up @@ -65,7 +65,7 @@ python setup.py install

## Examples

Note that operations are not instant, for example a server is not fully shut down when the API responds.
Note that some operations are not instant, for example a server is not fully shut down when the API responds.
You must take this into account in your automations.

### Defining and creating Servers
Expand Down
17 changes: 15 additions & 2 deletions docs/CloudManager.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
# Account / Authentication
# Cloud Manager

CloudManager handles all HTTP communications with UpCloud and mixes in the behavior of all Manager
classes.

In addition to the credentials, CloudManager can be given a timeout parameter that is
relayed to requests library, see [here](http://docs.python-requests.org/en/master/user/advanced/?highlight=timeout#timeouts).
Default timeout is 10.

```python

# create manager and form a token
manager = CloudManager("api-username", "password")
manager = CloudManager("api-username", "password", timeout=15) # default timeout is 10

```

# Account / Authentication

```python

# test token
manager.authenticate() # alias: get_account()
Expand Down
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

setup(
name='upcloud-api',
version='0.3.2',
version='0.3.3',
description='UpCloud API Client',
author='Elias Nygren',
author_email='elias.nygren@upcloud.com',
maintainer='Elias Nygren',
maintainer_email='elias.nygren@upcloud.com',
url='https://github.com/UpCloudLtd/upcloud-python-api',
packages=['upcloud_api', 'upcloud_api.cloud_manager'],
download='https://github.com/UpCloudLtd/upcloud-python-api/tarball/v0.3.2',
download='https://github.com/UpCloudLtd/upcloud-python-api/tarball/v0.3.3',
license='MIT',
install_requires=[
'future==0.14.3',
Expand Down
2 changes: 1 addition & 1 deletion upcloud_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from future import standard_library
standard_library.install_aliases()

__version__ = "0.3.2"
__version__ = "0.3.3"
__author__ = "Elias Nygren"
__author_email__ = "elias.nygren@upcloud.com"
__license__ = "MIT"
Expand Down
9 changes: 7 additions & 2 deletions upcloud_api/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ class BaseAPI(object):
api = "api.upcloud.com"
api_v = "1.2"

def __init__(self, token):
def __init__(self, token, timeout=None):
self.token = token
self.timeout = timeout

"""
Performs a request with a given body to a given endpoint in UpCloud's API.
Expand All @@ -33,7 +34,11 @@ def request(self, method, endpoint, body=None):
else: json_body_or_None = None

APIcall = getattr(requests, method.lower())
res = APIcall("https://api.upcloud.com" + url, data=json_body_or_None, headers=headers)
res = APIcall("https://api.upcloud.com" + url,
data=json_body_or_None,
headers=headers,
timeout=self.timeout
)

if( res.text ):
res_json = res.json()
Expand Down
10 changes: 9 additions & 1 deletion upcloud_api/cloud_manager/cloud_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,16 @@ class CloudManager(BaseAPI, ServerManager, IPManager, StorageManager, FirewallMa
All other managers are mixed in so code can be organized in corresponding submanager classes.
"""

def __init__(self, username, password):
def __init__(self, username, password, timeout=10):
"""
Initiates CloudManager that handles all HTTP conections with UpCloud's API.
Optionally determine a timeout for API connections (in seconds). A timeout with the value
`None` means that there is no timeout.
"""

self.token = "Basic " + base64.b64encode( (username + ":" + password).encode() ).decode()
self.timeout = timeout


def authenticate(self):
return self.get_account()
Expand Down

0 comments on commit a7ef273

Please sign in to comment.