forked from meraki/dashboard-api-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_snippets.py
326 lines (288 loc) · 11.7 KB
/
generate_snippets.py
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import os
import sys
import requests
from jinja2 import Template
CALL_TEMPLATE = Template(
"""import meraki
# Defining your API key as a variable in source code is discouraged.
# This API key is for a read-only docs-specific environment.
# In your own code, use an environment variable as shown under the Usage section
# @ https://github.com/meraki/dashboard-api-python/
API_KEY = '75dd5334bef4d2bc96f26138c163c0a3fa0b5ca6'
dashboard = meraki.DashboardAPI(API_KEY)
{{ parameter_assignments }}
response = dashboard.{{ section }}.{{ operation }}({{ parameters }})
print(response)
"""
)
REVERSE_PAGINATION = ["getNetworkEvents", "getOrganizationConfigurationChanges"]
# Helper function to convert camel case parameter name to snake case
def snakify(param):
ret = ""
for s in param:
if s.islower():
ret += s
elif s == "_":
ret += "_"
else:
ret += "_" + s.lower()
return ret
# Helper function to return pagination parameters depending on endpoint
def generate_pagination_parameters(operation):
ret = {
"total_pages": {
"type": "integer or string",
"description": 'total number of pages to retrieve, -1 or "all" for all pages',
},
"direction": {
"type": "string",
"description": 'direction to paginate, either "next" or "prev" (default) page'
if operation in REVERSE_PAGINATION
else 'direction to paginate, either "next" (default) or "prev" page',
},
}
return ret
# Helper function to return parameters within OAS spec, optionally based on list of input filters
def parse_params(operation, parameters, param_filters=[]):
if parameters is None:
return {}
# Create dict with information on endpoint's parameters
params = {}
for p in parameters:
name = p["name"]
if "schema" in p:
keys = p["schema"]["properties"]
for k in keys:
if "required" in p["schema"] and k in p["schema"]["required"]:
params[k] = {"required": True}
else:
params[k] = {"required": False}
params[k]["in"] = p["in"]
params[k]["type"] = keys[k]["type"]
params[k]["description"] = keys[k]["description"]
if "enum" in keys[k]:
params[k]["enum"] = keys[k]["enum"]
if "example" in p["schema"] and k in p["schema"]["example"]:
params[k]["example"] = p["schema"]["example"][k]
elif "required" in p and p["required"]:
params[name] = {"required": True}
params[name]["in"] = p["in"]
params[name]["type"] = p["type"]
if "description" in p:
params[name]["description"] = p["description"]
else:
params[name]["description"] = "(required)"
if "enum" in p:
params[name]["enum"] = p["enum"]
else:
params[name] = {"required": False}
params[name]["in"] = p["in"]
params[name]["type"] = p["type"]
params[name]["description"] = p["description"]
if "enum" in p:
params[name]["enum"] = p["enum"]
# Add custom library parameters to handle pagination
if "perPage" in params:
params.update(generate_pagination_parameters(operation))
# Return parameters based on matching input filters
if not param_filters:
return params
else:
ret = {}
if "required" in param_filters:
ret.update(
{k: v for k, v in params.items() if "required" in v and v["required"]}
)
if "pagination" in param_filters:
ret.update(
generate_pagination_parameters(operation) if "perPage" in params else {}
)
if "optional" in param_filters:
ret.update(
{
k: v
for k, v in params.items()
if "required" in v and not v["required"]
}
)
if "path" in param_filters:
ret.update(
{k: v for k, v in params.items() if "in" in v and v["in"] == "path"}
)
if "query" in param_filters:
ret.update(
{k: v for k, v in params.items() if "in" in v and v["in"] == "query"}
)
if "body" in param_filters:
ret.update(
{k: v for k, v in params.items() if "in" in v and v["in"] == "body"}
)
if "array" in param_filters:
ret.update(
{k: v for k, v in params.items() if "in" in v and v["type"] == "array"}
)
if "enum" in param_filters:
ret.update({k: v for k, v in params.items() if "enum" in v})
return ret
# Generate text for parameter assignments
def process_assignments(parameters):
text = "\n"
for k, v in parameters.items():
param_name = snakify(k)
if param_name == "id":
param_name = "id_"
if v == "list":
text += f"{param_name} = []\n"
elif v == "float":
text += f"{param_name} = 0.0\n"
elif v == "int":
text += f"{param_name} = 0\n"
elif v == "bool":
text += f"{param_name} = False\n"
elif v == "dict":
text += f"{param_name} = {{}}\n"
elif v == "str":
text += f"{param_name} = ''\n"
else:
if type(v) == str:
value = f"'{v}'"
else:
value = v
text += f"{param_name} = {value}\n"
return text
def main():
# Get latest OpenAPI specification
spec = requests.get("https://api.meraki.com/api/v1/openapiSpec").json()
# Supported scopes list will include organizations, networks, devices, and all product types.
supported_scopes = [
"administered",
"organizations",
"networks",
"devices",
"appliance",
"camera",
"cellularGateway",
"insight",
"sm",
"switch",
"wireless",
"sensor",
"licensing",
"secureConnect",
"wirelessController",
]
# legacy scopes = ['organizations', 'networks', 'devices',
# 'appliance', 'camera', 'cellularGateway', 'insight', 'sm', 'switch', 'wireless']
tags = spec["tags"]
paths = spec["paths"]
# Scopes used when generating the library will depend on the provided version of the API spec.
scopes = {tag["name"]: {} for tag in tags if tag["name"] in supported_scopes}
# Organize data
operations = []
for path, methods in paths.items():
for method in methods:
endpoint = paths[path][method]
tags = endpoint["tags"]
operation = endpoint["operationId"]
operations.append(operation)
scope = tags[0]
if path not in scopes[scope]:
scopes[scope][path] = {method: endpoint}
else:
scopes[scope][path][method] = endpoint
# Generate API libraries
for scope in scopes:
print(f"...generating {scope}")
section = scopes[scope]
for path, methods in section.items():
for method, endpoint in methods.items():
# Get metadata
tags = endpoint["tags"]
operation = endpoint["operationId"]
description = endpoint["summary"]
parameters = (
endpoint["parameters"] if "parameters" in endpoint else None
)
responses = endpoint[
"responses"
] # not actually used here for library generation
required = {}
optional = {}
if parameters:
if "perPage" in parse_params(operation, parameters):
pagination = True
else:
pagination = False
for p, values in parse_params(
operation, parameters, "required"
).items():
if "example" in values:
required[p] = values["example"]
elif p == "organizationId":
required[p] = "549236"
elif p == "networkId":
# DevNet Sandbox ALWAYS ON network @ https://n149.meraki.com/o/-t35Mb/manage/organization/overview
required[p] = "L_646829496481105433"
elif p == "serial":
required[p] = "Q2QN-9J8L-SLPD"
elif values["type"] == "array":
required[p] = "list"
elif values["type"] == "number":
required[p] = "float"
elif values["type"] == "integer":
required[p] = "int"
elif values["type"] == "boolean":
required[p] = "bool"
elif values["type"] == "object":
required[p] = "dict"
elif values["type"] == "string":
required[p] = "str"
else:
sys.exit(p, values)
if pagination:
if operation not in REVERSE_PAGINATION:
optional["total_pages"] = "all"
else:
optional["total_pages"] = 3
for p, values in parse_params(
operation, parameters, "optional"
).items():
if "example" in values:
optional[p] = values["example"]
if operation == "createNetworkGroupPolicy":
print(required)
print(optional)
if "code_snippets" not in os.listdir():
os.mkdir("code_snippets")
with open(f"code_snippets/{operation}.py", "w", encoding="utf-8") as fp:
if required.items():
parameters_text = "\n "
for k, v in required.items():
param_name = snakify(k)
if param_name == "id":
param_name = "id_"
parameters_text += f"{param_name}, "
for k, v in optional.items():
if k == "total_pages" and v == "all":
parameters_text += f"total_pages='all'"
elif k == "total_pages" and v == 1:
parameters_text += f"total_pages=1"
elif type(v) == str:
parameters_text += f"\n {k}='{v}', "
else:
parameters_text += f"\n {k}={v}, "
if parameters_text[-2:] == ", ":
parameters_text = parameters_text[:-2]
parameters_text += "\n"
else:
parameters_text = ""
fp.write(
CALL_TEMPLATE.render(
parameter_assignments=process_assignments(required),
section=scope,
operation=operation,
parameters=parameters_text,
)
)
if __name__ == "__main__":
main()