Get a Device
This example demonstrates how to retrieve a device from the ActionStreamer API using the official Python library.
Prerequisites
- Python 3.7+
- The
actionstreamer
Python package installed (e.g., viapip install actionstreamer
) - Access and secret keys saved to the local file system
File Setup
Save your access key and secret key in text files:
AccessKey.txt
SecretKey.txt
These should be placed in a known folder on the file system, such as:
C:\temp\config\
(Windows)/tmp/config/
(Linux/macOS)
Example Code
import actionstreamer.Model
import actionstreamer.WebService
import actionstreamer.CommonFunctions
# Determine config file path based on OS
if actionstreamer.Config.is_windows():
config_folder_path = 'C:\\temp\\config\\'
else:
config_folder_path = '/tmp/config/'
# Load credentials
access_key = actionstreamer.Config.get_config_value(config_folder_path, "AccessKey")
secret_key = actionstreamer.Config.get_config_value(config_folder_path, "SecretKey")
# Define API base URL
web_service_base_url = 'https://api.actionstreamer.com/'
# Create the web service config
ws_config = actionstreamer.Config.WebServiceConfig(access_key, secret_key, web_service_base_url, 10)
# Device identifier
device_name = 'TestDevice'
# Retrieve device information
ws_result = actionstreamer.WebService.API.get_device(ws_config, device_name)
# Handle response
if ws_result.http_response_code == 200:
device = actionstreamer.Model.Device(**ws_result.json_data)
print('DeviceID: ' + str(device.key))
print('Serial number: ' + device.serialNumber)
else:
print(f"Failed to retrieve device. HTTP Status Code: {ws_result.http_response_code}")