A python library for accessing the API


#1

Hey all,

I’ve started to develop a python library for interacting with the HTTPS API via Python Requests.

At the moment, it connects, and it lists devices, but I intend to add all of the functionality that is documented in the SwaggerUI.

I’m writing it primarily to scratch an itch, however if anyone else would like to contribute you’re more than welcome.

The code is on Github at https://github.com/mockingbirdconsulting/pyloraserver, and all changes need to go through a code review before being merged, so hopefully this will end up being a relatively quality bit of code!

To preempt the question about why I’m not using gRPC for this, requests+TLS is an easier option for me!


#2

If the Swagger UI supports outputting in YAML or another declarative format, there is automated client SDK tooling available (open source or commercial) that generates SDKs for a dozen+ languages. Whether or not those SDKs are idiomatic code is another issue…


#3

Though I like the idea, I agree with Brian: there are available Swagger clients for Python like https://github.com/Yelp/bravado, https://github.com/pyopenapi/pyswagger, etc., that make this kinds of unnecessary.

That said, I’ve developed a lot of unnecessary projects to learn, adjust things to my tiny unmet requirement or just for the sake of it. So I’ll be happy to give a hand whenever I can.


#4

Yeah, so I’ve looked at the auto-gen libraries in the past for similar things, and they basically produce untested code that has really weird class names.

There’s also a the point that they don’t understand how the API actually works - for instance, in order to create a device in LoRaServer, you have to call make two separate calls to the API - one to create the device, one to set the keys - so in a “custom” library, we can have a single function that calls both endpoints.

I’m pretty sure that in future there would also be “extra” things that we might want to do (for example, iterate over the organisations and return the devices grouped by the gateway that last saw them or similar), so a custom library also helps with that.


#5

Hey folks, version 0.1.0 has been released and can be installed following the instructions at https://pypi.org/project/pyloraserver/

The documentation is pretty much non-existent right now, however as an example the following will create a device:

from pyloraserver import loraserver, device
cx = loraserver.Loraserver(
        loraserver_url="https://my.lora.server",
        loraserver_user="my_account",
        loraserver_pass="my_password")
d = device.Devices(loraserver_connection=cx)
d.description = "My LoRaWAN Device"
d.deveui = "deadbeefdeadbeef"
d.name = "ELSYS-ELT2-HP-DEMO"
d.profile_id = "profile-id-string-from-ui" # Documentation on Device profile searching will be useful here!
d.appid = appId # An INT specifiying the app id - again, you can search for this already but it's not documented!
d.nwkKey = "myNetworkKey" # If this is not specified, a random one will be generated for you
res = d.create_and_activate()
print(res)

All and any help on this library would be appreciated, it’s all fully tested (although I’ve not opened up access to the CI server itself, it just reports back to github), so please have a go and let me know what you think!


#6

For those of you playing along at home, I’ve managed to write up some documentation which includes examples.

This is now live at https://pyloraserver.readthedocs.io/ and I’m using this library to add new devices to a LoRaServer.io cluster without any issues so far.

Bug reports/feature requests/contributions are all welcome :slight_smile:


#7

And now it’s “officially” launched: https://www.mockingbirdconsulting.co.uk/blog/2019-04-24-announcing-pyloraserver/