ITERATOR SUPPORT
I would also like to highlight the new possibility to use iterators in all list methods of the api.
Just add the new parameter to the Async/DashboardAPI object
use_iterator_for_get_pages = True
so in total it might look like this:
meraki.DashboardAPI(
api_key,
base_url="https://api.meraki.com/api/v1",
log_file_prefix=__file__[:-3],
print_console=True,
use_iterator_for_get_pages = True
)
So whats the big deal with it?
Currently when you are calling e.g. meraki.organizations.getOrganizationNetworks it is split into pages. The API will download each additional page and create a complete list of all entries in each page. As soon as the api has finished all of these calls it will return the whole list.
This creates a few problems:
- Depending on the number of pages it might take several seconds/minutes to download everything. Your script is waiting for the completion of this task to continue. So it might look like it is doing nothing for a while.
Thats especially true, when you are hitting the api limit. - Depending on the endpoint and the pages it is using more and more RAM
- If you are searching for a specific entry, then you wouldn't be able to abort the method as you will only see the result at the end.
The iterator version on the other hand will download exactly one page at the time and yield every entry of the page.
As soon as you hit the last entry, then it will download the next page and so on.
- You are getting the result of the page as soon as it is downloaded. You won't just have one big delay. Instead you are getting more and much smaller delays between the pages.
- Since there is a bigger time between each page call, ther is a lower risk of hitting the api limit.
- Much less RAM usage as there is just only one page in the RAM
- If you are searching for a specific entry, you could just break/return/... from the iteration loop and it won't download any subsequent pages.
The Async implementation is a little bit special here. It will reduce the delay between the pages as it is downloading the next page while you are still iterating over the current page.
Your TODOs:
As most scripts are just doing a loop over the result of the api call, there won't be any changes needed for the normal api calls.
for x in meraki.organizations.getOrganizationNetworks(organizationId=ORGANIZATION_ID, perPage=perPage, total_pages=-1):
print(f"{x['id']} - {x['name']}")
This will work with use_iterator_for_get_pages = True and False.
getNetworkEvents
For getNetworkEvents there is a difference. The legacy api call would create a dictionary with pageStart/End and an "events" key which holds all events.
The iterator is getting rid of pageStart/End and will just return each event directly.
so the following version has to be changed from this:
result = meraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless")
for x in result["events"]:
print(f"{x['occurredAt']}")
to this:
for x in meraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless"):
print(f"{x['occurredAt']}")
Your AsyncIO TODOs:
To get this with asyncio to work you will need to do a little bit more.
Instead of "await"ing the method you have to use "async for".
e.g. for getOrganizationNetworks:
for x in await aiomeraki.organizations.getOrganizationNetworks(organizationId=ORGANIZATION_ID, perPage=perPage, total_pages=-1):
print(f"{x['id']} - {x['name']}")
becomes this:
async for x in aiomeraki.organizations.getOrganizationNetworks(organizationId=ORGANIZATION_ID, perPage=perPage,total_pages=-1):
print(f"{x['id']} - {x['name']}")
getNetworkEvents
the backend changes are the same for the normal version, so you will also just receive the events itself here.
result = await aiomeraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless")
for x in result["events"]:
print(f"{x['occurredAt']}")
has to replaced with:
async for x in aiomeraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless"):
print(f"{x['occurredAt']}")
Full Examples
normal Version
AsyncIO Version