-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastapi2postman.py
72 lines (61 loc) · 1.93 KB
/
fastapi2postman.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
import argparse
import json
import os
import sys
from fastapi.routing import APIRoute
def register_routes(app):
routes = []
for route in app.routes:
if isinstance(route, APIRoute):
routes.append(
{
"name": route.name,
"method": ",".join(route.methods),
"url": str(route.path),
"path": str(route.path).replace("{", ":").replace("}", ""),
}
)
return routes
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--output", type=str, help="Output file", default="output.json")
parser.add_argument(
"--name", type=str, help="Collection name", default="FastAPI API"
)
parser.add_argument("--host", type=str, help="API host", default="localhost")
parser.add_argument("--port", type=int, help="API port", default=8000)
parser.add_argument(
"--app",
type=str,
help="Path to FastAPI application instance",
default="app.py",
required=True,
)
args = parser.parse_args()
app_path = args.app
app_dir, app_file = os.path.split(app_path)
sys.path.append(app_dir)
app_name, _ = os.path.splitext(app_file)
app_module = __import__(app_name)
app = getattr(app_module, "app")
routes = register_routes(app)
collection = {
"info": {
"name": args.name,
"schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json",
},
"item": [],
}
for route in routes:
item = {
"name": route["name"],
"request": {
"url": f'http://{args.host}:{args.port}{route["url"]}',
"method": route["method"],
},
}
collection["item"].append(item)
with open(args.output, "w") as f:
json.dump(collection, f, indent=4)
if __name__ == "__main__":
main()