Python pagination get 8000 devices

SOLVED
Johan_Oosterwaa
Getting noticed

Python pagination get 8000 devices

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))

 

 

 

1 ACCEPTED SOLUTION
Johan_Oosterwaa
Getting noticed

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))

 

 

View solution in original post

4 REPLIES 4
Johan_Oosterwaa
Getting noticed

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))

 

 

View solution in original post

Thanks for sharing !!!

I tested in my environment and it works !!!

Greenberet
A model citizen

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.

 

Get notified when there are additional replies to this discussion.
Welcome to the Meraki Community!
To start contributing, simply sign in with your Cisco account. If you don't yet have a Cisco account, you can sign up.