-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_api_docs.py
More file actions
96 lines (65 loc) · 1.77 KB
/
gen_api_docs.py
File metadata and controls
96 lines (65 loc) · 1.77 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
import json
def max_size(objs, key):
min_width = len(key)
col_max = max([len(obj[key]) for obj in objs])
return max(min_width, col_max)
def pad(str, width, char=" "):
return str.ljust(width, char)
def table_row(cols, col_sizes):
padded_cols = [pad(col, col_sizes[k]) for k, col in enumerate(cols)]
return "|" + "|".join(padded_cols) + "|"
def table_div(col_sizes):
padded_cols = [pad("", col_sizes[k], char="-")
for k in range(len(col_sizes))]
return table_row(padded_cols, col_sizes)
def table_for(table):
if len(table) == 0:
return "None"
headers = table[0].keys()
col_sizes = [max_size(table, header) for header in headers]
strs = []
strs.append(table_row(headers, col_sizes))
strs.append(table_div(col_sizes))
for row in table:
cols = [row[header] for header in headers]
strs.append(table_row(cols, col_sizes))
return "\n".join(strs)
def gen_endpoint(doc):
print(f"""
<details>
<summary>
<code><b>{doc["method"]}</b> {doc["path"]}</code>
<p>{doc["description"]}</p>
</summary>
### Parameters
{table_for(doc["parameters"])}
### Responses
{table_for(doc["responses"])}
### Example
```bash
{doc["example"]}
```
</details>
""")
def gen_schema(schema):
print(f"""
<details>
<summary>
<code>{schema['name']}</code>
<p>{schema['description']}</p>
</summary>
##### Example
```json
{schema['json']}
```
</details>
""")
def main():
with open("api-docs.json") as f:
docs = json.load(f)
print("## API Documentation")
[gen_endpoint(endpoint) for endpoint in docs["endpoints"]]
print("### API Schema")
[gen_schema(schema) for schema in docs["schema"]]
if __name__ == "__main__":
main()