forked from unit-finance/openapi-unit-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
111 lines (83 loc) · 2.93 KB
/
generate.py
File metadata and controls
111 lines (83 loc) · 2.93 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
import os
import json
python_bearer_auth = {
"type": "apiKey",
"in": "header",
"name": "Authorization"
}
default_bearer_auth = {
"type": "http",
"scheme": "bearer",
"bearerFormat": "JWT"
}
def change_bearer(bearer=python_bearer_auth):
with open('openapi.json', encoding='utf8') as file:
j = json.load(file)
j["components"]["securitySchemes"]["bearerAuth"] = bearer
with open('openapi.json', 'w', encoding='utf8') as file:
json.dump(j, file, indent=4, ensure_ascii=False)
# if flag is not python
change_bearer()
path_of_the_directory = './schemas/'
for filename in os.listdir(path_of_the_directory):
f_path = os.path.join(path_of_the_directory, filename)
if os.path.isfile(f_path):
f = open(f_path, 'r', encoding="utf8")
json_object = json.load(f)
f.close
json_object_as_string = str(json_object)
json_object_as_string = json_object_as_string.replace('./schemas/', '')
json_object_as_string = json_object_as_string.replace('./', '')
json_object_as_string = json_object_as_string.replace('"$ref": "../', '"$ref": "')
if json_object_as_string == str(json_object):
continue
data = eval(json_object_as_string)
# Re-open file here
f2 = open(f_path, 'w', encoding="utf8")
json.dump(data, f2, indent=4)
# for line in lineList:
# line = line.replace('./schemas/', '')
# f2.write(line)
f2.close()
try:
path_of_the_directory = "./unit/swagger_client"
for root, dirs, files in os.walk(path_of_the_directory, topdown=False):
for name in files:
os.remove(os.path.join(root, name))
for name in dirs:
os.rmdir(os.path.join(root, name))
except FileNotFoundError:
print("ignore")
file_name = "swagger-codegen-cli-3.0.47.jar"
if not os.path.exists(file_name):
print(f"Error: Unable to access jarfile {file_name}")
change_bearer(default_bearer_auth)
exit(1)
os.system(f"java -jar {file_name} generate -i openapi.json -l python -o unit")
path_of_the_directory = './unit/swagger_client/models/'
if not os.path.exists(path_of_the_directory):
change_bearer(default_bearer_auth)
exit(1)
for filename in os.listdir(path_of_the_directory):
f_path = os.path.join(path_of_the_directory, filename)
if os.path.isfile(f_path):
f = open(f_path, 'r')
lineList = f.readlines()
f.close
# Re-open file here
f2 = open(f_path, 'w')
for line in lineList:
line = line.replace('#/components/schemas/', '')
f2.write(line)
f2.close()
f_path = "unit/swagger_client/api/create_application_api.py"
f = open(f_path, 'r')
lineList = f.readlines()
f.close
# Re-open file here
f2 = open(f_path, 'w')
for line in lineList:
line = line.replace('_create_application', 'create_application')
f2.write(line)
f2.close()
change_bearer(default_bearer_auth)