balena-io / balena-sdk-python

Balena SDK for Python
Apache License 2.0
67 stars 46 forks source link

Device History model doesn't work #349

Closed fisehara closed 11 months ago

fisehara commented 11 months ago

balena-sdk 14.1.0

balena.models.device.history.get_all_by_device returns empty array for UUID or ID whereas using direct url request with OData compliant query parameters returns data for a givne visible device.

Run script with UUID="<uuid>" APITOKEN="<apiTokenFromDashboard>" python3 test.py code to reproduce, please get an APIKEY and an device UUID that you have access to:

from balena import Balena
import requests
import os

APITOKEN = os.environ.get("APITOKEN")

headers = {"Authorization": "Bearer " + APITOKEN}

deviceUUID = os.environ.get("UUID")

balena = Balena()
balena.auth.login_with_token(APITOKEN)
device = balena.models.device.get(deviceUUID)

print (device)

url_history_uuid = requests.get(
    f"https://api.balena-cloud.com/v6/device_history?$filter=uuid eq '{device['uuid']}'",
    headers=headers,
)
print("\nurl_history_uuid: ", url_history_uuid.text)
sdk_history_uuid = balena.models.device.history.get_all_by_device(uuid_or_id=device['uuid'])
print("\nsdk_history_uuid:", sdk_history_uuid)

url_history_id = requests.get(
    f"https://api.balena-cloud.com/v6/device_history?$filter=tracks__device eq {device['id']}",
    headers=headers,
)

print("\nurl_history_id: ", url_history_id.text)

sdk_history_id = balena.models.device.history.get_all_by_device(uuid_or_id=device['id'])

print("\nsdk_history_id: ", sdk_history_id)
fisehara commented 11 months ago

While preparing a python sdk snippet to fetch device_history data, I discovered that, we have intentionally added range filters on the SDK / client) side to not fetch all data by default. That made me think it's not working. Please excuse the noise.