Skip to main content

Python client for the AmigoCloud REST API

Project description

Python client for the AmigoCloud REST API.

Installation

Install via pip:

pip install amigocloud

Dependencies

  • requests: Handles the HTTP requests to the AmigoCloud REST API.

  • socketIO_client: Handles the AmigoCloud websocket connection.

This dependencies will be automatically installed.

Usage

Authentication

This library uses API token to authenticate you. To generate or access your API tokens, go to API tokens.

from amigocloud import AmigoCloud
amigocloud = AmigoCloud(token='R:dlNDEiOWciP3y26kG2cHklYpr2HIPK40HD32r1')

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. If the project URL doesn’t match its project, AmigoCloudError will be thrown.

from amigocloud import AmigoCloud
amigocloud = AmigoCloud(token='C:Ndl3xGWeasYt9rqyuVsByf5HPMAGyte10y1Mub',
                        project_url='users/123/projects/1234')

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.

Requests

Once you’re logged in you can start making requests to the server. You can use full urls or relative API urls:

# All three will do the same request:
amigocloud.get('me')
amigocloud.get('/me')
amigocloud.get('https://www.amigocloud.com/api/v1/me')

For convenience, when using project tokens, urls are relative to the project’s url (unless it starts with /):

# All three will do the same request:
amigocloud.get('datasets')
amigocloud.get('/users/123/projects/1234/datasets')
amigocloud.get('https://www.amigocloud.com/api/v1/users/123/projects/1234/datasets')

Creating a new AmigoCloud project from Python is as simple as:

data = {'name': 'New Project', 'description': 'Created from Python'}
amigocloud.post('me/projects', data)

All responses are parsed as JSON and return a Python object (usually a dict). This data can be later used in another request:

me = amigocloud.get('me')
visible_projects = amigocloud.get(me['visible_projects'])

print 'My projects:'
for project in visible_projects['results']:
    print '*', project['name']

You can get the raw response if you want by using the raw parameter:

me = amigocloud.get('me')
images = amigocloud.get(me['images'])

with open('thumbnail.png', 'wb') as thumbnail:
    image_data = amigocloud.get(images['thumbnail'], raw=True)
    thumbnail.write(image_data)

Websocket connection

The websocket connection is started when the AmigoCloud object is instantiated, and it is closed when the object is destroyed. You always need to use a user token for websockets.

Make sure to read our help page about our websocket events before continue reading.

To start listening to websocket events related to your user (multicast events), do (you must be logged in to start listening to your events):

amigocloud.listen_user_events()

Once you’re listening to your events, you can start adding callbacks to them. A callback is a function that will be called everytime the event is received. These functions should have only one parameter, that would be a python dict.

def project_created(data):
    print 'User id=%(user_id)s created project id=%(project_id)s' % data
amigocloud.add_callback('project:creation_succeeded', project_created)

Realtime events are broadcast events related to realtime dataset. To start listening to them, do:

amigocloud.listen_dataset_events(owner_id, project_id, dataset_id)

Then add a callback for them:

def realtime(data):
    print 'Realtime dataset id=%(dataset_id)s' % data
    for obj in data['data']:
        print "Object '%(object_id)s' is now at (%(latitude)s, %(longitude)s)" % obj
amigocloud.add_callback('realtime', realtime)

Finally, start running the websocket client:

ac.start_listening()

This method receives an optional parameter seconds. If seconds is None (default value), the client will listen forever. You might want to run this method in a new thread.

Exceptions

An AmigoCloudError exception will be raised if anything fails during the request:

try:
    amigocloud.post('me/projects')
except AmigoCloudError as err:
    print 'Something failed!'
    print 'Status code was', err.response.status_code
    print 'Message from server was', err.text

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

amigocloud-1.2.1.linux-x86_64.tar.gz (10.0 kB view details)

Uploaded Source

Built Distributions

amigocloud-1.2.1-py3-none-any.whl (9.5 kB view details)

Uploaded Python 3

amigocloud-1.2.1-py2-none-any.whl (9.5 kB view details)

Uploaded Python 2

File details

Details for the file amigocloud-1.2.1.linux-x86_64.tar.gz.

File metadata

File hashes

Hashes for amigocloud-1.2.1.linux-x86_64.tar.gz
Algorithm Hash digest
SHA256 c4a03a275e58fe87d7e019d8b7c65e9bdfc4778857cfaf2188bbfcd908a09423
MD5 dceaf3ac5084a2f17a3876babb1af83a
BLAKE2b-256 f88b2e6108a142fc6a4069634805aae8fcd1a4c6f737b76e6f2751b6d838863e

See more details on using hashes here.

File details

Details for the file amigocloud-1.2.1-py3-none-any.whl.

File metadata

File hashes

Hashes for amigocloud-1.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 84c84b3ab2f2988727ec95728fc06ce39396c62074d159d0de7b6a9edc8c9b31
MD5 152565853cd75608d8a70395d2cf41cf
BLAKE2b-256 5d60e9d218952bae4e8f4ac978f49d625b3f80de61a62d207a9a4e70dccf2117

See more details on using hashes here.

File details

Details for the file amigocloud-1.2.1-py2-none-any.whl.

File metadata

File hashes

Hashes for amigocloud-1.2.1-py2-none-any.whl
Algorithm Hash digest
SHA256 e5cf4f6a2163e624073c42a76747bd82228f5b6224c246ec4e3cf509d0d9d9be
MD5 0d803f15273280415a3c4974b7dec132
BLAKE2b-256 c408af11a1345689e663d542231a8cea2c4106ffee2238c35e86c383f1368b36

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page