forked from dev-magdy/Shreyash-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_api operationIds.py
67 lines (56 loc) · 1.88 KB
/
fix_api operationIds.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
import sys
from yaml import load, dump
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
import copy
def get_file(url):
response=requests.get(url)
if response.ok:
contents=response.content
return contents
raise Exception(f"Unable to fetch file using url {url}")
def load_file(filename):
if filename.endswith("yaml") or filename.endswith("yml"):
if filename.startswith("http"):
return load(get_file(filename),Loader=Loader)
return load(open(filename,"r"),Loader=Loader)
if filename.endswith("json"):
if filename.startswith("http"):
return json.loads(get_file(filename),Loader=Loader)
return json.load(open(filename,"r"))
raise Exception("file name does not end with yaml,yml or json")
def camelCase(words):
for i,word in enumerate(words):
if not word:
continue
word = list(word)
word[0] = word[0].upper()
words[i] = "".join(word)
return "".join(words)
def generate(path):
words=[]
for word in path.split("/")[1:]:
word = word.lower()
word = word.replace("{","")
word = word.replace("}","")
if word:
if "-" in word:
word = camelCase(word.split("-"))
if "_" in word:
word = camelCase(word.split("_"))
words.append(word)
return camelCase(words)
filename = sys.argv[1]
obj = load_file(filename)
verbs = ["get","post","put","patch","head"]
for path in obj["paths"]:
path_obj = obj["paths"][path]
if not path_obj:
continue
for verb in verbs:
if verb in path_obj:
path_obj[verb]["operationId"] = verb+generate(path)
name,ext = filename.split(".")
dump(obj,open(name+"_fixed."+ext,"w"),Dumper=Dumper)