TorizonCloud Python API is a wrapper around the official Torizon Cloud API.
The purpose of this API is to make it easier to the user by:
- Creating a login function, so the user don't need to pass the credentials at each API call
- Format the HTTP header, this can be quite challenging
- Access all devices, packages, fleets and lockboxes with the same syntax.
- The official API creates a new endpoint for each newly created device, package, ....
Check the examples/main.py script, it should be self-explanatory.
But you simply need to:
- Install the package
pip install torizon-cloud
- Login using your Torizon Cloud Credentials.
- It expects 2 enviroment variables,
TORIZON_CLOUD_CLIENT
andTORIZON_CLOUD_SECRET
.
- It expects 2 enviroment variables,
- Check the helper function for the API endpoints.
Check the examples folder for some common use cases.
import os
from torizon_cloud import TorizonCloud
client_id = os.getenv("TORIZON_CLOUD_CLIENT")
client_secret = os.getenv("TORIZON_CLOUD_SECRET")
cloud = TorizonCloud()
cloud.login(client_id, client_secret)
cloud.api.getDevices()
cloud.api.getPackages()
cloud.api.getFleets()
cloud.api.getLockboxes()
cloud.api.getLockbox_details()
cloud.api.getDevice_dataMetric_names()
This is going to return info about ['deviceUuid', 'localIpV4', 'hostname', 'macAddress']
cloud.api.getDevicesNetwork()
cloud.api.getDevicesPackagesDeviceuuid(
deviceUuid = '558c5227-62fe-4a83-beea-7153d7ae641d' # you can get this from getDevices
)
cloud.api.postDevices(
deviceName = "Delicious-Hamburger", # user-defined name
deviceId = "1234", # user-defined string
hibernated = True)
This is going to return the device_credentials.zip
for the device to connect to your cloud.
- dump the zip content to
/var/sota/import
in the device - run in the device
sudo systemctl restart aktualizr
compose_file_lock = "examples/camera.lock.yaml"
version = "999"
with open(compose_file_lock, "r") as f:
data = f.read()
cloud.api.postPackages(
name = os.path.basename(compose_file_lock),
version = version,
hardwareId = "docker-compose",
targetFormat = "BINARY",
ContentLength = os.path.getsize(compose_file_lock),
data = data)
new_fleet_uuid = cloud.api.postFleets(
name = "test",
fleetType = "static"
) # returns a string with the fleet uuid
# query a device by name
devices_by_name = cloud.api.getDevices(
nameContains = "Delicious-Hamburger"
)["values"] # returns a list of devices with nameContains
devices_uuid = [x["deviceUuid"] for x in devices_by_name["values"]]
# add the devices to the fleet
devices_uuid_reponse = cloud.api.postFleetsFleetidDevices(
fleetId = new_fleet_uuid,
devices = devices_uuid
)
cloud.api.postUpdates(
packageIds = [f"{os.path.basename(compose_file_lock)}-{version}"],
# devices = ["4af561db-b4a0-4346-949b-c20d5added07"], # the device must have been seen online at least 1 time
# fleets = ["8b96056e-0607-4e48-a098-e8c182647171"], # the device must have been seen online at least 1 time
)
Please be careful, this operation is irreversible.
# get the first 50 devices
devices = cloud.api.getDevices(limit = 50)["values"]
# filter those devices with a simple logic
deviceUuids = [device["deviceUuid"] for device in devices if (device["deviceName"] != "something" or not device["deviceId"].startswith("verdin"))]
# delete all the devices in the filtered list
for deviceUuid in deviceUuids:
cloud.api.deleteDevicesDeviceuuid(deviceUuid = deviceUuid)
Please be careful, this operation is irreversible.
# get the first 50 fleets
fleets = cloud.api.getFleets(limit = 50)["values"]
# filter those fleets with a simple logic
fleetIds = [fleet["id"] for fleet in fleets if (fleet["name"] != "something")]
# delete all the fleets in the list
for fleetId in fleetIds:
cloud.api.deleteFleetsFleetid(fleetId = fleetId)
Please be careful, this operation is irreversible.
# get the first 50 packages using some filters
packages = cloud.api.getPackages(
limit = 50,
packageSource = "targets.json",
nameContains = "matheuscastelo"
)["values"]
packageIds = [package["packageId"] for package in packages]
for packageId in packageIds:
cloud.api.deletePackagesPackageid(packageId = packageId)
Please be careful, this operation is irreversible.
# get the first 50 devices
devices = cloud.api.getDevices(limit = 50)["values"]
# filter those devices with a simple logic
deviceUuids = [device["deviceUuid"] for device in devices if (device["deviceName"] != "something" or not device["deviceId"].startswith("verdin"))]
# delete all the devices in the filtered list
for deviceUuid in deviceUuids:
cloud.api.deleteDevicesDeviceuuid(deviceUuid = deviceUuid)
Please be careful, this operation is irreversible.
# get the first 50 fleets
fleets = cloud.api.getFleets(limit = 50)["values"]
# filter those fleets with a simple logic
fleetIds = [fleet["id"] for fleet in fleets if (fleet["name"] != "something")]
# delete all the fleets in the list
for fleetId in fleetIds:
cloud.api.deleteFleetsFleetid(fleetId = fleetId)
Please be careful, this operation is irreversible.
# get the first 50 packages using some filters
packages = cloud.api.getPackages(
limit = 50,
packageSource = "targets.json",
nameContains = "matheuscastelo"
)["values"]
packageIds = [package["packageId"] for package in packages]
for packageId in packageIds:
cloud.api.deletePackagesPackageid(packageId = packageId)
Please be careful, this operation is irreversible.
# get lockboxes names and filter it
lockboxes = cloud.api.getLockboxes()
lockboxesNames = [name for name in lockboxes if "matheuscastelo" in name]
for lockboxesNams in lockboxesNames:
cloud.api.deleteLockboxesLockbox_name(lockbox_name = lockboxesNams)