-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclitools.py
More file actions
62 lines (53 loc) · 1.69 KB
/
clitools.py
File metadata and controls
62 lines (53 loc) · 1.69 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
import os
import tempfile
import time
import subprocess
import random
from typing import Optional
import fire
def gen_openapi_json(file:str = "openapi.json"):
"""Generate an openapi.yaml file for the current server code."""
import uvicorn
import requests
port = random.randint(9000, 12000)
command = f"uvicorn submit_ce.api.app:app --host 127.0.0.1 --port {port}".split()
process = subprocess.Popen(command)
time.sleep(1.5)
try:
response = requests.get("http://127.0.0.1:%d/openapi.json" % port)
if response.status_code != 200:
raise RuntimeError(f"Could not get openapi.yaml from server {response.status_code}: {response.text}")
with open(file, "w") as f:
f.write(response.text)
print(f"Wrote openapi spec in json format to {file}")
finally:
process.kill()
def gen_client(gen_spec:bool = True):
"""Generate the API client for the current server code.
ARGS:
gen_spec (bool): Whether to generate a spec file or attempt to use an exsiting one.
"""
if gen_spec:
print(f"* Generating to openapi.json for current code")
gen_openapi_json()
command = f"""
docker run --rm
--user {str(os.getuid()) + ":" + str(os.getgid())}
-v ./:/local
openapitools/openapi-generator-cli generate
-i /local/openapi.json
-g python
-o /local/client
--package-name openapi_submit_client
--minimal-update
"""
print(command)
process = subprocess.Popen(command.split())
process.wait()
if __name__ == "__main__":
fire.Fire(
{
"gen_openapi_json":gen_openapi_json,
"gen_client":gen_client,
}
)