Balena API odata and pyodata

Hi, I’m trying to create a simple python wrapper for the balena API using the pyodata library.

and I’m having troubles with figuring out what url to use for the odata part.

So for context I can use the curl method:

curl -X GET "https://api.balena-cloud.com/v6/device" -H "Content-Type: application/json" -H "Authorization: Bearer MYTOKEN"

And I can make a basic call using python requests.

import requests
import json
from requests.structures import CaseInsensitiveDict$
url="https://api.balena-cloud.com/v6/device"
headers = CaseInsensitiveDict()
headers['Accept'] = 'application/json'
headers['Authorization'] = 'Bearer MYTOKEN'
response = requests.get(url, headers=headers)
data = json.loads(response.content)

The response_code is 200 and the content is the valid json containing a list of devices.

Now I’m trying to use the pyodata library to parse your odata standard structured data.

import requests
service_url = "not really sure what this should be????"
session = requests.Session()
session.headers = { 'Authorization': 'Bearer MYTOKEN', 'Accept': 'application/json' }
myservice = pyodata.Client(service_url, session)

Unfortunately the result when I use the same url as above is:

*** pyodata.exceptions.HttpError: Metadata request failed, status code: 400, body:
b'"Malformed url: \'/resin/device/$metadata?$format=json;metadata=full\'"'

I know this is an error from a third party library but hoping you can help me understand what I’m missing. I believe I’m just not using the correct service url - but from not having a really good understanding of how the odata standard works I don’t know what that should be.

Is there some separate url from which I get the metadata needed to format the response into a structured object?
What should the service_url be?

Hi there, I’m getting the same issue. Using the python requests library with that service_url does work, but then using the pyodata library on top of that seems to result in this 400 error.

Can I ask, what is the benefit of making a python wrapper for the api, as opposed to using the balena python sdk (Balena Python SDK - Balena Documentation)?

:grinning: I don’t think there is any benefit. And now that I know about the SDK I will abandon this foolishness!