forked from fusion44/blitz_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_client_libs.py
82 lines (64 loc) · 1.92 KB
/
gen_client_libs.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
import importlib
import json
import os
import shutil
from subprocess import PIPE, Popen
from fastapi.openapi.utils import get_openapi
# npm install @openapitools/openapi-generator-cli -g
# sudo apt install default-jre
# For now assume that the clients repository is a sibling to the current working directory.
out_path = os.path.abspath(os.path.join("../", f"blitz_api_client_libraries"))
mod = importlib.import_module(f"app.main")
app = getattr(mod, "app")
version = "v1"
if version:
for route in app.router.routes:
if version in route.path:
app = route.app
break
def _generate(generator: str):
p = os.path.abspath(os.path.join(out_path, f"clients/{generator}"))
print(f"Removing {p}")
if os.path.exists(p):
shutil.rmtree(p)
print(f"Recreating directory {p}")
os.makedirs(p, exist_ok=True)
print(f"Generating client files {generator}")
process = Popen(
[
"openapi-generator-cli",
"generate",
"-i",
"openapi.json",
"-g",
generator,
"-o",
p,
],
stdout=PIPE,
stderr=PIPE,
)
_, stderr = process.communicate()
if stderr:
print(stderr)
def main():
# Gets the OpenAPI specs.
specs = get_openapi(
title=app.title if app.title else None,
version=app.version if app.version else None,
openapi_version=app.openapi_version if app.openapi_version else None,
description=app.description if app.description else None,
routes=app.routes if app.routes else None,
)
with open(f"openapi.json", "w") as f:
json.dump(specs, f, indent=2)
_generate("dart-dio")
_generate("dart")
_generate("go")
_generate("javascript")
_generate("kotlin")
_generate("python")
_generate("typescript-axios")
_generate("typescript-fetch")
if __name__ == "__main__":
main()