10-27-2020 12:01 PM
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.
10-28-2020 07:40 AM
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))
10-28-2020 07:40 AM
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))
08-30-2021 09:39 AM
Thanks for sharing !!!
I tested in my environment and it works !!!
10-28-2020 08:24 AM
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, ....
10-28-2020 10:06 AM
i will thank you.
Discover and save your favorite ideas. Come back to expert answers, step-by-step guides, recent topics, and more.
New here? Get started with these tips. How to use Community New member guide