Skip to main content

A Python client for the Altair IoT Studio API

Project description

Altair IoT Studio API Client

A Python client for the Altair® IoT Studio™ API

pip command Supported Versions Documentation Status

Introduction

This library allows you to interact with the Altair® IoT Studio™ API using Python. The current implementation has support for the following APIs:

Install

From PyPI:

pip install iots

This library officially supports Python 3.8+.

The API class

All the requests are made using an instance of the API class.

from iots import API

api = API()

By default, the API class will use the host https://api.swx.altairone.com. You can also specify a different host:

from iots import API

api = API(host="https://api.my-iot-studio.com")

Authentication

There are multiple ways to deal with authentication:

  • Setting an already-exchanged access token:

    api = API().set_token("my-access-token")
    
  • Using an OAuth2 client credentials with manual token revocation:

    my_client_id = "my-client-id"
    my_client_secret = "my-client-secret"
    my_scopes = ["category", "thing"]
    api = API().set_credentials(my_client_id, my_client_secret, my_scopes)
    
    # ...
    
    api.revoke_token()
    
  • Using an OAuth2 client credentials with automatic token revocation:

    with API().set_credentials(my_client_id, my_client_secret, my_scopes) as api:
        # ...
        # The token will be revoked when the 'with' block ends
        # or if the code returns or raises an exception
    

Tokens are automatically refreshed using OAuth2 client credentials, so you don't need to care about manually refreshing them.

Using the API

The API class uses a nested syntax to allow accessing the API resources, setting the request information with the same structure order that the one used by the API endpoints. Some examples:

# Get an instance of a Space that will be used to access resources later.
# Creating this instance will NOT make any request to the API.
space = api.spaces("my-iot-project")

# Get all the Categories in the Space
categories = space.categories().get()

# Get a specific Thing
thing = space.things("01GQ2E9M2Y45BX9EW0F2BM032Q").get()

# Get all the Things inside a Category
things = space.categories("Sensors").things().get()

# Get all the Things with some query parameters
things = space.things().get(params={"property:temperature": "gt:20"})

# Get all the Property values of a Thing
thing_properties = space.things("01GQ2E9M2Y45BX9EW0F2BM032Q").properties().get()
# ... and access to the 'temperature' Property
temperature = thing_properties['temperature']

# Get a specific Property value
thing_property = space.things("01GQ2E9M2Y45BX9EW0F2BM032Q").properties("temperature").get()
temperature = thing_properties['temperature']

# Update a Property value
thing_property = space.things("01GQ2E9M2Y45BX9EW0F2BM032Q").properties("temperature").update(17.3)

# Create a new Action value
action = space.things("01GQ2E9M2Y45BX9EW0F2BM032Q").actions("updateFirmware").create({"updateFirmware": {"input": "v2.0.0"}})

The models used by the API for request and response data can be found in the iots.models.models module.

💡 Note: The API resources use type hints that should help to understand how to use the API and the data models to define input data or access response data. It should also help your IDE with code completion and displaying documentation.

Query parameters

To add any query parameter to a request, use the param argument with a dictionary of parameters:

# Return up to 100 Things that have a "temperature" Property with value >= 20
things = space.things().get(params={
  'property:temperature': 'gte:20',
  'limit': 100,
})

Pagination

Some resource listing operations support pagination. You can iterate the response instances to retrieve all the results. If additional API calls are needed to fetch the remaining results, they will be made behind the scenes.

# Get all the Things in a Space
things = space.things().get()

for t in things:
    print(t.uid)

Get raw HTTP response

Making an API request returns an instance of an object that represents the response content. However, you can also access the original response using the http_response() method.

things = api.spaces("my-iot-project").things().get()
# Get the raw response as an instance of requests.Response
raw_response = things.http_response()

status_code = raw_response.status_code
content = raw_response.content
body = raw_response.json()
# ...

This method is also available in the raised exceptions, provided that a response has been returned from the server.

from iots.models.exceptions import ResponseError

try:
    things = api.spaces("my-iot-project").things().get()
except ResponseError as e:
    raw_response = e.http_response()

TLS certificate verification

If you need to skip the TLS certificate verification, you can use the verify argument when creating the API instance:

api = API(verify=False)

🔮 Future features

  • Add more API resource components.
  • Support for asynchronous requests.

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

iots-0.5.0.tar.gz (42.3 kB view details)

Uploaded Source

Built Distribution

iots-0.5.0-py3-none-any.whl (51.0 kB view details)

Uploaded Python 3

File details

Details for the file iots-0.5.0.tar.gz.

File metadata

  • Download URL: iots-0.5.0.tar.gz
  • Upload date:
  • Size: 42.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.7.16 Linux/6.8.0-52-generic

File hashes

Hashes for iots-0.5.0.tar.gz
Algorithm Hash digest
SHA256 7e3f4cbcecf260786383736121e25249780902f37b21b4fcb4ca6b70c4465b8e
MD5 eb0e68e247db6c59478b73fe957c6a5b
BLAKE2b-256 24f5614ca014a44238b84fb06b5b3e1fa494b11daf2b5c75993afcc1e9e4c8f0

See more details on using hashes here.

File details

Details for the file iots-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: iots-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 51.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.7.16 Linux/6.8.0-52-generic

File hashes

Hashes for iots-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 aaf8e1bedf1ead0f9f8debbe3cc59ee728f5c632bf731046ccedc8395c2527b7
MD5 7759c050a682ac82dd6bd0ec0d71305b
BLAKE2b-256 210eefd237f9f4d1264105f83e502aca1cba553a149a8dff5a26b82cb6d7857c

See more details on using hashes here.

Supported by

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