forked from meraki/dashboard-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaio_get_pages_iterator.py
More file actions
126 lines (98 loc) · 4.42 KB
/
Copy pathaio_get_pages_iterator.py
File metadata and controls
126 lines (98 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import csv
from datetime import datetime, timedelta
import os
import asyncio
import argparse
import ipaddress
from typing import Dict,List
import sys
import time
import meraki.aio
# Either input your API key below, or set an environment variable
# for example, in Terminal on macOS: export MERAKI_DASHBOARD_API_KEY=66839003d2861bc302b292eb66d3b247709f2d0d
api_key = ""
ORGANIZATION_ID = ""
NETWORK_ID = ""
def timeit(func):
async def process(func, *args, **params):
if asyncio.iscoroutinefunction(func):
print('this function is a coroutine: {}'.format(func.__name__))
return await func(*args, **params)
else:
print('this is not a coroutine')
return func(*args, **params)
async def helper(*args, **params):
print('{}.time'.format(func.__name__))
start = time.time()
result = await process(func, *args, **params)
# Test normal function route...
# result = await process(lambda *a, **p: print(*a, **p), *args, **params)
print('>>>', time.time() - start)
return result
return helper
@timeit
async def getNetworksLegacy(aiomeraki: meraki.aio.AsyncDashboardAPI, perPage=5):
count = 0
for x in await aiomeraki.organizations.getOrganizationNetworks(organizationId=ORGANIZATION_ID, perPage=perPage, total_pages=-1):
print(f"{x['id']} - {x['name']}")
count = count + 1
print(f"Found {count} networks")
@timeit
async def getNetworksIterator(aiomeraki: meraki.aio.AsyncDashboardAPI, perPage=5):
count = 0
async for x in aiomeraki.organizations.getOrganizationNetworks(organizationId=ORGANIZATION_ID, perPage=perPage,total_pages=-1):
print(f"{x['id']} - {x['name']}")
count = count + 1
print(f"Found {count} networks")
@timeit
async def getNetworkEventsLegacy(aiomeraki: meraki.aio.AsyncDashboardAPI, perPage=5):
count = 0
result = await aiomeraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless")
for x in result["events"]:
print(f"{x['occurredAt']}")
count = count + 1
print(f"Found {count} events")
@timeit
async def getNetworkEventsIterator(aiomeraki: meraki.aio.AsyncDashboardAPI, perPage=5):
count = 0
async for x in aiomeraki.networks.getNetworkEvents(networkId=NETWORK_ID, perPage=perPage,total_pages=50,productType="wireless"):
print(f"{x['occurredAt']}")
count = count + 1
print(f"Found {count} events")
async def main():
parser = argparse.ArgumentParser(description='Example for demonstrating the use_iterator_for_get_pages parameter')
# Instantiate a Meraki dashboard API session
# NOTE: you have to use "async with" so that the session will be closed correctly at the end of the usage
async with meraki.aio.AsyncDashboardAPI(
api_key,
base_url="https://api.meraki.com/api/v1",
log_file_prefix=__file__[:-3],
print_console=True,
use_iterator_for_get_pages = True
) as aiomeraki_iterator:
async with meraki.aio.AsyncDashboardAPI(
api_key,
base_url="https://api.meraki.com/api/v1",
log_file_prefix=__file__[:-3],
print_console=False,
use_iterator_for_get_pages = False
) as aiomeraki_legacy:
pass
print("Test legacy")
await getNetworksLegacy(aiomeraki_legacy)
await asyncio.sleep(2) #just wait two seconds between the tests
print("Test iterator")
await getNetworksIterator(aiomeraki_iterator)
print("-----------------------------------------------------------------------")
print("-----------------------------------------------------------------------")
print("-----------------------------------------------------------------------")
print("-----------------------------------------------------------------------")
print("Test legacy")
await getNetworkEventsLegacy(aiomeraki_legacy)
await asyncio.sleep(2) #just wait two seconds between the tests
print("Test iterator")
await getNetworkEventsIterator(aiomeraki_iterator)
print("Script complete!")
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())