This sample code results in frequent 429 errors. If you have more than 200 networks, it is pretty much guaranteed to fail.
#!/usr/bin/env python3
#
import os,argparse,asyncio,meraki.aio,datetime
# Load global and local Meraki settings such as MERAKI_DASHBOARD_API_KEY
from dotenv import load_dotenv
load_dotenv()
load_dotenv(dotenv_path=os.path.join(os.path.expanduser("~"),".meraki.env"))
# This function retrieves the orgId
async def getOrgId(dashboard,orgName):
orgId=None
# Search for the org
for org in await dashboard.organizations.getOrganizations():
if org['name'] == orgName:
orgId=org['id']
break;
if orgId == None:
print("Invalid organization name supplied: "+orgName)
exit(-1)
return(orgId)
async def test429(dashboard,orgName):
# Find out the organisation ID
orgId=await getOrgId(dashboard,orgName)
networks=await dashboard.organizations.getOrganizationNetworks(orgId)
clientTasks=[dashboard.networks.getNetworkAlertsSettings(net['id']) for net in networks]
for task in asyncio.as_completed(clientTasks):
await task
async def main():
# Meraki parameters
orgName=None
text="""
429 async error generator
"""
parser = argparse.ArgumentParser(description = text)
parser.add_argument("-o", "--orgName", help="Meraki org name")
args=parser.parse_args()
# Grab any arguments defined in the .env file
orgName=os.getenv("orgName")
# Apply any overrides from the command line
if args.orgName: orgName=args.orgName
# Check that we got all the parameters we need
if not os.getenv("MERAKI_DASHBOARD_API_KEY"):
print("MERAKI_DASHBOARD_API_KEY must be defined in .meraki.env in your home directory or in .env in the current directory")
exit(-1)
if not orgName:
print("orgName must be defined on the command line, in .meraki.env in your home directory or in .env in the current directory")
exit(-1)
async with meraki.aio.AsyncDashboardAPI(
output_log=False,
print_console=False
) as dashboard:
await test429(dashboard,orgName)
if __name__ == "__main__":
asyncio.run(main())
Include the throttler class and change the "with" command to the below - and it works with zero errors or warnings.
import os,argparse,asyncio,meraki.aio,datetime,throttler
async with meraki.aio.AsyncDashboardAPI(
output_log=False,
print_console=False
) as dashboard:
dashboard._session._concurrent_requests_semaphore = throttler.Throttler(rate_limit=10, period=1.0)
await test429(dashboard,orgName)