forked from dev-magdy/Shreyash-specs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fix_pokeapi.py
141 lines (130 loc) · 3.53 KB
/
fix_pokeapi.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
import copy
import json
import yaml
from re import sub
_PaginatedResourceWithNameURL = """{
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PaginatedResourceWithNameURL"
}
}
}
}
}"""
_PaginatedResourceWithURL = """{
"200": {
"description": "successful operation",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/PaginatedResourceWithURL"
}
}
}
}
}"""
_parameters = """[
{
"name": "limit",
"in": "query",
"required": false,
"schema": {
"type": "integer",
"default": 20
}
},
{
"name": "offset",
"in": "query",
"required": false,
"schema": {
"type": "integer",
"default": 0
}
}
]"""
_schemas = """{
"PaginatedResourceWithNameURL": {
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"next": {
"type": "string"
},
"previous": {
"type": "string"
},
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"url": {
"type": "string"
}
}
}
}
}
},
"PaginatedResourceWithURL": {
"type": "object",
"properties": {
"count": {
"type": "integer"
},
"next": {
"type": "string"
},
"previous": {
"type": "string"
},
"results": {
"type": "array",
"items": {
"type": "object",
"properties": {
"url": {
"type": "string"
}
}
}
}
}
}
}"""
def camel_case(s):
return sub(r"(_|-|/)+", " ", s).title().replace(" ", "")
def _getListOperationId(path, method):
return f"{method.lower()}{camel_case(path)}List"
if __name__ == '__main__':
with open("pokeapi.yaml", 'r') as f:
api_spec = yaml.safe_load(f.read())
modified_spec = copy.deepcopy(api_spec)
modified_spec['components']['schemas'].update(json.loads(_schemas))
for path, path_data in api_spec['paths'].items():
for resource_path in ["{id_or_name}/", "{id}/"]:
if resource_path in path:
list_path = path.split(resource_path)[0]
print(f"{list_path}")
list_path_data = copy.deepcopy(path_data)
for method in list_path_data:
list_path_data[method]["operationId"] = _getListOperationId(list_path, method)
list_path_data[method]["summary"] = list_path_data[method]['summary'] + " List"
list_path_data[method]["description"] = "List for all " + list_path_data[method]["description"]
list_path_data[method]["parameters"] = json.loads(_parameters)
if resource_path == "{id_or_name}/":
list_path_data[method]["responses"] = json.loads(_PaginatedResourceWithNameURL)
else:
list_path_data[method]["responses"] = json.loads(_PaginatedResourceWithURL)
modified_spec['paths'][list_path] = list_path_data
with open("modified_pokeapi.yaml", "w") as f:
f.write(yaml.dump(modified_spec, sort_keys=False))