0

I have this script to make an API call using the meraki python module to query a Meraki device.

I'm trying to add some error handling in the code so that if the API call comes back with an error code, it will do something else. I cannot seem to figure out what to do.

Here is my simple code to just query a device:

import meraki
import requests

API_KEY = 'API_KEY'
dashboard = meraki.DashboardAPI(API_KEY)

serial = input("What is the serial number?")
print(f"{serial}")

response = (dashboard.devices.getDevice(serial))

When I run the script it will return either a "200 OK" or "404 Not Found"

Terminal window response:

C:\Scripts\Meraki\dev> python .\getdevice.py
What is the serial number? XXXX-XXXX-XXXX

2023-04-26 18:32:52       meraki:     INFO > GET https://api.meraki.com/api/v1/devices/XXXX-XXXX-XXXX
2023-04-26 18:32:53       meraki:     INFO > devices, getDevice - 200 OK

or

2023-04-26 18:41:09       meraki:     INFO > GET https://api.meraki.com/api/v1/devices/XXXX-XXXX-XXXX
2023-04-26 18:41:10       meraki:    ERROR > devices, getDevice - 404 Not Found, b''

The purpose of this script is to check to see if the serial number has already been assigned to a user / network. If it is available, I'll get a "404 Not Found" and if it has already been assigned to someone, I'll get a "404 Not Found".

How can I detect and handle the 404 case?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Jonathan
  • 3
  • 1
  • What is the output of `print(repr(response))`? – mkrieger1 Apr 27 '23 at 00:15
  • It returns properties of the device. 'network id N_12345645682 serial # XXXX-XXXX-XXXX model # MX64 url is https://n22.meraki.com/ latitude longitude serial XXXX-XXXX-XXXX mac wan1Ip None wan2Ip None tags [] name MX64 model MX64 firmware Not running configured version floorPlanId None ' – Jonathan Apr 27 '23 at 13:47

2 Answers2

0

I'm assuming you are using the meraki package with the source code at https://github.com/meraki/dashboard-api-python/tree/main/meraki.

According to that, if a 404 response occurs, getDevice should raise an APIError exception. It contains details about the error in the status, reason, and message attributes (see https://github.com/meraki/dashboard-api-python/blob/main/meraki/exceptions.py) – your log output shows the result of APIError.__repr__ which includes these attributes.

You can handle it by using a try/except statement:

from meraki import APIError

# ...

try:
    response = dashboard.devices.getDevice(serial)
except APIError as e:
    print(e.status)
    print(e.reason)
    print(e.message)

If you need to handle the 404 case specifically, you can use an if statement:

    if e.status == 404:
        # ...
mkrieger1
  • 19,194
  • 5
  • 54
  • 65
0

Not wishing to be awkward, but you are attempting to do this the worst possible way.

Try: inventory = dashboard.organizations.getOrganizationInventoryDevices( my_org.get(), total_pages="all" )

The above pulls your entire organisation inventory down and you can search for the serial number within the list you have downloaded.

You can have that API call filter out fields you don't want but it had issues in the early days (it only returned the first 1000 items) therefore I just wrote my own culling routines

The 404 you are receiving is the API telling you you have encountered an error (and what it is) you really shouldn't rely upon an error to derive information.

BTW be considerate (and sensible) by paying attention to the (regular) 10 API calls per second limit (and others with API access are doing the same) you should seldom encounter API failures.

I find myself using try to catch strange return values due to bizarrely configured devices.

Also, you do not need to import requests if your only http/https communications are with the Meraki endpoint.

API V1 will handle failed responses and retrys fairly well behaved manner

cta102
  • 1
  • 1
  • I appreciate the feedback. I am very new with Python and cobbling things together. We have several hundred networks / devices, so I was looking at getting a single device. I don't know enough yet to figure out how to grab everything and only look for the specific device that I need. I'm up for a new approach. Appreciate the heads up on importing "requests", I stole that from another script I found. – Jonathan Jun 29 '23 at 02:38