get_organization_devices API what values for starting_after and ending_before to get 2nd page

SOLVED
Farrukh
Getting noticed

get_organization_devices API what values for starting_after and ending_before to get 2nd page

from meraki_sdk.meraki_sdk_client import MerakiSdkClient
from meraki_sdk.exceptions.api_exception import APIException
 
x_cisco_meraki_api_key = 'X-Cisco-Meraki-API-Key'
 
client = MerakiSdkClient(x_cisco_meraki_api_key)
 
 devices_controller = client.devices
collect = {}
organization_id = 'organizationId4'
collect['organization_id'] = organization_id
 
per_page = 1000
collect['per_page'] = per_page
 
starting_after = '?'
collect['starting_after'] = starting_after
 
ending_before = '?'
collect['ending_before'] = ending_before
 
try:
    result = devices_controller.get_organization_devices(collect)
except APIException as e: 
    print(e)
1 ACCEPTED SOLUTION
CBurkhead
Building a reputation

Here is the Python function for doing this.

 

# This function takes the API URL and query string. It returns all of the JSON data from the API request that has
# been broken up into pages of data.
def GetPages(url,query):
   data = []
   done = False
   while done == False:
      try:
         ret = requests.request('GET', url, headers=g_header, params=query)
      except requests.exceptions.ConnectionError:
         print('A connection error has occurred while getting the page information.\n')
         return None
# Append the new data to the existing.
      data += ErrorCheck(ret)
# Get the page URLs from the HTTP header of the API call and split it into the URLs.
      pages = ret.headers['Link'].split(',')
# See if there is a page for the 'next' page of data.
      page = next((i for i in pages if 'rel=next' in i), None)
      if page != None:
# Isolate the token and then add it to the new query.
         token = page.split('startingAfter=')[1]
         token = token.split('&')[0]
         query['startingAfter'] = token
      else:
         done = True
   return data

 

The call to this would be something like:

 

clientlist = GetPages(url,querystring)

 

Where url is the full URL to the endpoint and querystring would be the parameters you want, like {'timespan':'30','perPage':'1000'}

View solution in original post

7 REPLIES 7
BrechtSchamp
Kind of a big deal

The first call results in a first batch of results and will also give you the values for starting_after and ending_before for your next call.

 

Thanks for the reply are those values are going to be in the header and is it possible to access the header using result in python API

Yes. Well, in a way.

 

In the headers there will be a link field, as shown below:

image.png

 

In this example the "first" link brings you back to the beginning with startingAfter=a000000.

The "next" link brings you to the next set of - in this case 3 - results with startingAfter=k70307.

CBurkhead
Building a reputation

Here is the Python function for doing this.

 

# This function takes the API URL and query string. It returns all of the JSON data from the API request that has
# been broken up into pages of data.
def GetPages(url,query):
   data = []
   done = False
   while done == False:
      try:
         ret = requests.request('GET', url, headers=g_header, params=query)
      except requests.exceptions.ConnectionError:
         print('A connection error has occurred while getting the page information.\n')
         return None
# Append the new data to the existing.
      data += ErrorCheck(ret)
# Get the page URLs from the HTTP header of the API call and split it into the URLs.
      pages = ret.headers['Link'].split(',')
# See if there is a page for the 'next' page of data.
      page = next((i for i in pages if 'rel=next' in i), None)
      if page != None:
# Isolate the token and then add it to the new query.
         token = page.split('startingAfter=')[1]
         token = token.split('&')[0]
         query['startingAfter'] = token
      else:
         done = True
   return data

 

The call to this would be something like:

 

clientlist = GetPages(url,querystring)

 

Where url is the full URL to the endpoint and querystring would be the parameters you want, like {'timespan':'30','perPage':'1000'}

CBurkhead
Building a reputation

A couple of notes, the ErrorCheck function that gets called is my internal function that converts the returned data to JSON and verifies there are no errors. It returns the data from the endpoint or a value of None.

 

Also, I have had to modify this function a couple of times depending on the data that the endpoint is returning. It was originally made for getting client data, which was just lists of data. You might have to alter how the data is appended if you are getting a dictionary with lists in it or some other format.

 

It does work as written for networks/{networkID}/clients.

Thanks that great. Let me try and see if it works 

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.