Skip to content

Commit a026860

Browse files
minor refactor generators
Extracting organize_spec() method to ensure consistency between snippets and library generation.
1 parent 2acd4a8 commit a026860

6 files changed

Lines changed: 1413 additions & 559 deletions

File tree

generator/common.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
def organize_spec(paths, scopes):
2+
operations = list() # list of operation IDs
3+
for path, methods in paths.items():
4+
# method is the HTTP action, e.g., get, put, etc.
5+
for method in methods:
6+
# endpoint is the method for that specific path
7+
endpoint = paths[path][method]
8+
9+
# the endpoint has tags
10+
tags = endpoint["tags"]
11+
12+
# the endpoint has an operationId
13+
operation = endpoint["operationId"]
14+
15+
# add the operation ID to the list
16+
operations.append(operation)
17+
18+
# The endpoint has a scope defined by the first tag
19+
# There are a handful of operations that are currently mistagged
20+
# This helps ensure they are scoped to the correct module
21+
if len(tags) > 2:
22+
match tags[2]:
23+
case 'spaces':
24+
scope = 'spaces'
25+
case _:
26+
scope = tags[0]
27+
else:
28+
scope = tags[0]
29+
30+
# Needs documentation
31+
if path not in scopes[scope]:
32+
scopes[scope][path] = {method: endpoint}
33+
# Needs documentation
34+
else:
35+
scopes[scope][path][method] = endpoint
36+
return operations, scopes

generator/generate_library.py

Lines changed: 3 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import jinja2
77
import requests
88

9+
import common as common
10+
911
READ_ME = """
1012
=== PREREQUISITES ===
1113
Include the jinja2 files in same directory as this script, and install Python requests
@@ -308,40 +310,7 @@ def generate_library(spec: dict, version_number: str, api_version_number: str, i
308310
fp.write(contents)
309311

310312
# Organize data from OpenAPI specification
311-
operations = list() # list of operation IDs
312-
for path, methods in paths.items():
313-
# method is the HTTP action, e.g. get, put, etc.
314-
for method in methods:
315-
# endpoint is the method for that specific path
316-
endpoint = paths[path][method]
317-
318-
# the endpoint has tags
319-
tags = endpoint["tags"]
320-
321-
# the endpoint has an operationId
322-
operation = endpoint["operationId"]
323-
324-
# add the operation ID to the list
325-
operations.append(operation)
326-
327-
# the endpoint has a scope defined by the first tag
328-
# There are a handful of operations that are currently mistagged
329-
# This helps ensure they are scoped to the correct module
330-
if len(tags) > 2:
331-
match tags[2]:
332-
case 'spaces':
333-
scope = 'spaces'
334-
case _:
335-
scope = tags[0]
336-
else:
337-
scope = tags[0]
338-
339-
# Needs documentation
340-
if path not in scopes[scope]:
341-
scopes[scope][path] = {method: endpoint}
342-
# Needs documentation
343-
else:
344-
scopes[scope][path][method] = endpoint
313+
operations, scopes = common.organize_spec(paths, scopes)
345314

346315
# Inform the user how many operations were found
347316
print(f"Total of {len(operations)} endpoints found from OpenAPI spec...")

generator/generate_snippets.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import requests
55
from jinja2 import Template
6+
import common as common
67

78
CALL_TEMPLATE = Template(
89
"""import meraki
@@ -12,7 +13,7 @@
1213
# In your own code, use an environment variable as shown under the Usage section
1314
# @ https://github.com/meraki/dashboard-api-python/
1415
15-
API_KEY = '75dd5334bef4d2bc96f26138c163c0a3fa0b5ca6'
16+
API_KEY = 'your-key-here'
1617
1718
dashboard = meraki.DashboardAPI(API_KEY)
1819
{{ parameter_assignments }}
@@ -194,6 +195,8 @@ def main():
194195
"licensing",
195196
"secureConnect",
196197
"wirelessController",
198+
"campusGateway",
199+
"spaces"
197200
]
198201
# legacy scopes = ['organizations', 'networks', 'devices',
199202
# 'appliance', 'camera', 'cellularGateway', 'insight', 'sm', 'switch', 'wireless']
@@ -202,19 +205,8 @@ def main():
202205
# Scopes used when generating the library will depend on the provided version of the API spec.
203206
scopes = {tag["name"]: {} for tag in tags if tag["name"] in supported_scopes}
204207

205-
# Organize data
206-
operations = []
207-
for path, methods in paths.items():
208-
for method in methods:
209-
endpoint = paths[path][method]
210-
tags = endpoint["tags"]
211-
operation = endpoint["operationId"]
212-
operations.append(operation)
213-
scope = tags[0]
214-
if path not in scopes[scope]:
215-
scopes[scope][path] = {method: endpoint}
216-
else:
217-
scopes[scope][path][method] = endpoint
208+
# Organize data from OpenAPI specification
209+
operations, scopes = common.organize_spec(paths, scopes)
218210

219211
# Generate API libraries
220212
for scope in scopes:
@@ -299,9 +291,9 @@ def main():
299291
parameters_text += f"{param_name}, "
300292
for k, v in optional.items():
301293
if k == "total_pages" and v == "all":
302-
parameters_text += f"total_pages='all'"
294+
parameters_text += "total_pages='all'"
303295
elif k == "total_pages" and v == 1:
304-
parameters_text += f"total_pages=1"
296+
parameters_text += "total_pages=1"
305297
elif type(v) == str:
306298
parameters_text += f"\n {k}='{v}', "
307299
else:

0 commit comments

Comments
 (0)