Hi All,
I'm trying to get all devices in an organization via a Python script and know there is a limit of 1000 devices per page.
The problem is i have 8000 devices so i need to use header links to grabs the next pages.
Being banging my head against the wall for an hour now but cannot seem the figure out how to implement pagination using the following function
def getNetworksDevices(p_orgid):
try:
r = requests.get('https://api.meraki.com/api/v1/organizations/%s/devices' % (p_orgid),headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
while r.status_code == 429:
time.sleep(int(r.headers['Retry-After']))
r = requests.get('https://api.meraki.com/api/v1/organizations/%s/devices' % (p_orgid),headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
if r.status_code == 200:
return(r.json())
if r.status_code == 200:
return (r.json())
except:
print('ERROR 02: Unable to contact Meraki cloud')
print ('API response: {}'.format(r.status_code))
Solved! Go to solution.
Solved it!
i can now get 8000 devices in a single organization using the following Function:
def getNetworksDevices(p_orgid):
results = []
try:
r = requests.get('https://api.meraki.com/api/v1/organizations/%s/devices' % (p_orgid),headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
if r.status_code == 200:
raw = r.json()
for i in raw:
results.append(i)
while 'next' in r.links :
r = requests.get(r.links['next']['url'],headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
#print(r.links)
raw = r.json()
for i in raw:
results.append(i)
#print (len(results))
return (results)
except:
print('ERROR 02: Unable to contact Meraki cloud')
print ('API response: {}'.format(r.status_code))
Solved it!
i can now get 8000 devices in a single organization using the following Function:
def getNetworksDevices(p_orgid):
results = []
try:
r = requests.get('https://api.meraki.com/api/v1/organizations/%s/devices' % (p_orgid),headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
if r.status_code == 200:
raw = r.json()
for i in raw:
results.append(i)
while 'next' in r.links :
r = requests.get(r.links['next']['url'],headers={'X-Cisco-Meraki-API-Key': ARG_APIKEY,'Content-Type': 'application/json'} )
#print(r.links)
raw = r.json()
for i in raw:
results.append(i)
#print (len(results))
return (results)
except:
print('ERROR 02: Unable to contact Meraki cloud')
print ('API response: {}'.format(r.status_code))
Thanks for sharing !!!
I tested in my environment and it works !!!
you should take a look at the meraki python library.
It is easier to use than using standard requests and it will take care about all throttling limits, pagination, ....
i will thank you.