Skip to content

Commit 882536d

Browse files
author
jcmonroy
authored
Merge pull request #4 from amigocloud/project_tokens
Project Tokens Support
2 parents 2066b70 + 695a590 commit 882536d

4 files changed

Lines changed: 58 additions & 17 deletions

File tree

README.rst

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,15 @@ This library uses API token to authenticate you. To generate or access your API
3434
from amigocloud import AmigoCloud
3535
amigocloud = AmigoCloud(token='R:dlNDEiOWciP3y26kG2cHklYpr2HIPK40HD32r1')
3636
37+
You could also use a project token. Remember that project tokens can only be used to query endpoints relative to the project it belongs to.
38+
If the project URL doesn't match its project, `AmigoCloudError` will be thrown.
39+
40+
.. code:: python
41+
42+
from amigocloud import AmigoCloud
43+
amigocloud = AmigoCloud(token='C:Ndl3xGWeasYt9rqyuVsByf5HPMAGyte10y1Mub',
44+
project_url='users/123/projects/1234')
45+
3746
3847
You can use a READ token if you only want to do requests that won't alter data. Otherwise, you'll need to use more permissive tokens.
3948

@@ -50,6 +59,15 @@ can use full urls or relative API urls:
5059
amigocloud.get('/me')
5160
amigocloud.get('https://www.amigocloud.com/api/v1/me')
5261
62+
For convenience, when using project tokens, urls are relative to the project's url (unless it starts with `/`):
63+
64+
.. code:: python
65+
66+
# All three will do the same request:
67+
amigocloud.get('datasets')
68+
amigocloud.get('/users/123/projects/1234/datasets')
69+
amigocloud.get('https://www.amigocloud.com/api/v1/users/123/projects/1234/datasets')
70+
5371
Creating a new AmigoCloud project from Python is as simple as:
5472

5573
.. code:: python
@@ -84,7 +102,8 @@ Websocket connection
84102
~~~~~~~~~~~~~~~~~~~~
85103

86104
The websocket connection is started when the AmigoCloud object is
87-
instantiated, and it is closed when the object is destroyed.
105+
instantiated, and it is closed when the object is destroyed. You always
106+
need to use a user token for websockets.
88107

89108
Make sure to read `our help page about our websocket events <http://help.amigocloud.com/hc/en-us/articles/204246154>`__ before continue reading.
90109

VERSION.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
1.1
1+
1.2

amigocloud/amigocloud.py

Lines changed: 36 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,23 +41,39 @@ class AmigoCloud(object):
4141
https://www.amigocloud.com/accounts/tokens
4242
"""
4343

44-
def __init__(self, token=None, base_url=BASE_URL, use_websockets=True,
45-
websocket_port=None):
44+
error_msg = {
45+
'logged_in_websockets': ('You must be logged in with a user token '
46+
'to start receiving websocket events.')
47+
}
4648

49+
def __init__(self, token=None, project_url=None, base_url=BASE_URL,
50+
use_websockets=True, websocket_port=None):
51+
"""
52+
:param str token: AmigoCloud API Token
53+
:param str project_url: Specify it if you are using a project token
54+
:param str base_url: points to https://www.amigocloud.com by default
55+
:param bool use_websockets: True by default. Parameter will be ignored
56+
when using Project Tokens
57+
:param int websocket_port: Standard websocket port by default
58+
"""
4759
# Urls
4860
if base_url.endswith('/'):
4961
self.base_url = base_url[:-1]
5062
else:
5163
self.base_url = base_url
5264
self.api_url = self.base_url + '/api/v1'
5365

66+
self._token = None
67+
self._user_id = None
68+
self._project_id = None
69+
self._project_url = None
70+
5471
# Auth
55-
self.logout()
5672
if token:
57-
self.authenticate(token)
73+
self.authenticate(token, project_url)
5874

5975
# Websockets
60-
if use_websockets:
76+
if use_websockets and not project_url:
6177
self.socketio = SocketIO(self.base_url, websocket_port)
6278
self.amigosocket = self.socketio.define(BaseNamespace,
6379
'/amigosocket')
@@ -73,22 +89,30 @@ def build_url(self, url):
7389
# User wants to use the api_url
7490
if url.startswith('/'):
7591
return self.api_url + url
76-
return '%s/%s' % (self.api_url, url)
92+
return os.path.join(self._project_url or self.api_url, url)
7793

7894
def check_for_errors(self, response):
7995
try:
8096
response.raise_for_status()
8197
except requests.exceptions.HTTPError as exc:
82-
raise AmigoCloudError(exc.message, exc.response)
98+
raise AmigoCloudError(str(exc), exc.response)
8399

84-
def authenticate(self, token):
100+
def authenticate(self, token, project_url=None):
85101
self._token = token
86-
response = self.get('/me')
87-
self._user_id = response['id']
102+
self._project_url = (self.build_url(project_url) if project_url
103+
else None)
104+
if not self._project_url:
105+
response = self.get('/me')
106+
self._user_id = response['id']
107+
else:
108+
response = self.get('')
109+
self._project_id = response['id']
88110

89111
def logout(self):
90112
self._token = None
91113
self._user_id = None
114+
self._project_id = None
115+
self._project_url = None
92116

93117
def get(self, url, params=None, raw=False, stream=False, **request_kwargs):
94118
"""
@@ -308,8 +332,7 @@ def listen_user_events(self):
308332
"""
309333

310334
if not self._user_id:
311-
msg = 'You must be logged in to start receiving websocket events.'
312-
raise AmigoCloudError(msg)
335+
raise AmigoCloudError(self.error_msg['logged_in_websockets'])
313336

314337
response = self.get('/me/start_websocket_session')
315338
websocket_session = response['websocket_session']
@@ -323,8 +346,7 @@ def listen_dataset_events(self, owner_id, project_id, dataset_id):
323346
"""
324347

325348
if not self._user_id:
326-
msg = 'You must be logged in to start receiving websocket events.'
327-
raise AmigoCloudError(msg)
349+
raise AmigoCloudError(self.error_msg['logged_in_websockets'])
328350

329351
url = '/users/%s/projects/%s/datasets/%s/start_websocket_session'
330352
response = self.get(url % (owner_id, project_id, dataset_id))

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
with open('VERSION.txt', 'r') as v:
99
version = v.read().strip()
1010

11-
with open('REQUIREMENTS.txt', 'r') as r:
11+
with open('requirements.txt', 'r') as r:
1212
requires = r.read().split()
1313

1414
with open('README.rst', 'r') as r:

0 commit comments

Comments
 (0)