Using AnythingDB Queries from a User Function

A powerful way to explore the use of AnythingDB from within the Altair IoT Studio platform is by automating these queries through the use of Functions. To be able to incorporate calls in the functions, use the https protocol using a POST call.

You must have the necessary credentials to make the call following the basic operation of our API.

https://api.swx.altairone.com/spaces/YourSpaceID/query/cursor/

The body of the POST call must include in JSON format the fields defined above, where the query to be made will be included.

The following code snippet includes an example of a Python function to perform a query using the cursor endpoint.

from swx.auth.token import get_token
import requests
import json


API_HOST = 'https://api.swx.altairone.com'


CLIENT_ID = "xxx::xxxxxxxxxxxxxxxxxxxx"
CLIENT_SECRET = "xxxxxxxxxxxxxxxxxxxxxxx"


def handle(req):
  with get_token(CLIENT_ID, CLIENT_SECRET, ["query"]) as token: 
    data ={"query": "FOR c IN categories FILTER c.name ==@category_name FOR t IN things FILTER c._id IN t.categories RETURN t.status.name",
"batchSize":5,
"bindVars":{
"category_name":"CategoryName"
}
}

PATH="/spaces/spacename/query/cursor/"
headers = {"Authorization": "Bearer " + token.access_token}
response = requests.request("POST", API_HOST + PATH , headers=headers,json=data)


return {
"body": response.json()['result'],
"status_code": response.status_code
}

The above example lists the things within the category with the name "CategoryName". For this example, the Client_Id and the Client_Secret correspond to an App previously defined in the Access Control section where the query scope has been established to be used.