PYTHON, JSON AND API’S – PART 2

In part one we figured out how to authenticate to the API. We can now start digging in to getting some data. A lot of API’s are documented right along with the API. Vertiv however decided to document their API via PDF document. It is pretty clear the commands and the results, just kind of a pain it’s not online with the API. That being said let’s start building out some of the URL’s we need to pull data. First we look to get the root API URL. More times than not it’s something like http://192.168.10.15/api/v1. In this case it’s no different. Our root (I call it base) URL is

base_url = 'http://10.1.2.38/api/v1'

Next we can check the documentation to see where the info we want is located. I like to grab something easy right off the bat to make sure everything is working as expected. In reviewing the API docs, I see we can grab some system information.

The URL to grab this information is at the 2.2.1 header, /system/info. For simplicity let’s assign a variable to that portion of the URL.

sysinfo = '/system/info'

Our next step is to combine the two URL variables so we have a full URL to call.

full_url = base_url + sysinfo

print(full_url)

http://10.1.2.38/api/v1/system/info

That’s exactly what we are looking for. We now have our authentication token and a URL to call. Let’s try it and see what we get.

response = requests.get(final_url, headers=header_token)
json_data = json.loads(response.text)
print(json_data)

{'serialNumber': '053009xxxx', 'type': 'ACS8016 with dual power supply', 'bootcode': '1.18', 'firmware': '2.0.3.3542+685+35+11', 'firmwareDate': 'Dec 6 2017 - 18:06:39', 'bootedFrom': 'hardware', 'powerSupply1': 'on', 'powerSupply2': 'on', 'cpu': 'ARMv7 Processor rev 0 (v7l)', 'cores': 2}

It works! Next up, grabbing the info we want and putting it in the format we want.