Spaces:
Building
Building
Update api_controller.py
Browse files- api_controller.py +20 -26
api_controller.py
CHANGED
@@ -1,21 +1,15 @@
|
|
1 |
-
from fastapi import APIRouter, Request,
|
2 |
-
from
|
3 |
import json
|
4 |
-
import datetime
|
5 |
|
6 |
router = APIRouter()
|
7 |
-
service_config = ServiceConfig()
|
8 |
-
service_config.load()
|
9 |
-
|
10 |
-
def get_utc_now():
|
11 |
-
return datetime.datetime.utcnow().isoformat()
|
12 |
|
13 |
@router.get("/list")
|
14 |
-
def list_apis():
|
15 |
-
return
|
16 |
|
17 |
@router.post("/add")
|
18 |
-
async def add_api(request: Request):
|
19 |
data = await request.json()
|
20 |
api_name = data.get("api_name")
|
21 |
api_data = data.get("api_data")
|
@@ -23,18 +17,18 @@ async def add_api(request: Request):
|
|
23 |
if not api_name or not api_data:
|
24 |
raise HTTPException(status_code=400, detail="api_name and api_data are required")
|
25 |
|
26 |
-
if api_name in
|
27 |
raise HTTPException(status_code=400, detail="API with this name already exists")
|
28 |
|
29 |
-
|
30 |
|
31 |
-
with open(
|
32 |
-
json.dump(
|
33 |
|
34 |
return {"message": f"API {api_name} added"}
|
35 |
|
36 |
@router.post("/update")
|
37 |
-
async def update_api(request: Request):
|
38 |
data = await request.json()
|
39 |
api_name = data.get("api_name")
|
40 |
api_data = data.get("api_data")
|
@@ -42,30 +36,30 @@ async def update_api(request: Request):
|
|
42 |
if not api_name or not api_data:
|
43 |
raise HTTPException(status_code=400, detail="api_name and api_data are required")
|
44 |
|
45 |
-
if api_name not in
|
46 |
raise HTTPException(status_code=404, detail="API not found")
|
47 |
|
48 |
-
|
49 |
|
50 |
-
with open(
|
51 |
-
json.dump(
|
52 |
|
53 |
return {"message": f"API {api_name} updated"}
|
54 |
|
55 |
@router.post("/delete")
|
56 |
-
async def delete_api(request: Request):
|
57 |
data = await request.json()
|
58 |
api_name = data.get("api_name")
|
59 |
|
60 |
if not api_name:
|
61 |
raise HTTPException(status_code=400, detail="api_name is required")
|
62 |
|
63 |
-
if api_name not in
|
64 |
raise HTTPException(status_code=404, detail="API not found")
|
65 |
|
66 |
# Check if API is used in any intent
|
67 |
used_in = []
|
68 |
-
for project_name, project in
|
69 |
for version in project.get("versions", []):
|
70 |
for intent in version.get("intents", []):
|
71 |
if intent.get("action") == api_name:
|
@@ -77,9 +71,9 @@ async def delete_api(request: Request):
|
|
77 |
detail=f"API '{api_name}' is used in intents: {', '.join(used_in)}. Cannot delete."
|
78 |
)
|
79 |
|
80 |
-
del
|
81 |
|
82 |
-
with open(
|
83 |
-
json.dump(
|
84 |
|
85 |
return {"message": f"API {api_name} deleted"}
|
|
|
1 |
+
from fastapi import APIRouter, HTTPException, Request, Depends
|
2 |
+
from app import get_config, ServiceConfig
|
3 |
import json
|
|
|
4 |
|
5 |
router = APIRouter()
|
|
|
|
|
|
|
|
|
|
|
6 |
|
7 |
@router.get("/list")
|
8 |
+
def list_apis(config: ServiceConfig = Depends(get_config)):
|
9 |
+
return config.apis
|
10 |
|
11 |
@router.post("/add")
|
12 |
+
async def add_api(request: Request, config: ServiceConfig = Depends(get_config)):
|
13 |
data = await request.json()
|
14 |
api_name = data.get("api_name")
|
15 |
api_data = data.get("api_data")
|
|
|
17 |
if not api_name or not api_data:
|
18 |
raise HTTPException(status_code=400, detail="api_name and api_data are required")
|
19 |
|
20 |
+
if api_name in config.apis:
|
21 |
raise HTTPException(status_code=400, detail="API with this name already exists")
|
22 |
|
23 |
+
config.apis[api_name] = api_data
|
24 |
|
25 |
+
with open(config.config_path, "w", encoding="utf-8") as f:
|
26 |
+
json.dump(config, f, indent=2)
|
27 |
|
28 |
return {"message": f"API {api_name} added"}
|
29 |
|
30 |
@router.post("/update")
|
31 |
+
async def update_api(request: Request, config: ServiceConfig = Depends(get_config)):
|
32 |
data = await request.json()
|
33 |
api_name = data.get("api_name")
|
34 |
api_data = data.get("api_data")
|
|
|
36 |
if not api_name or not api_data:
|
37 |
raise HTTPException(status_code=400, detail="api_name and api_data are required")
|
38 |
|
39 |
+
if api_name not in config.apis:
|
40 |
raise HTTPException(status_code=404, detail="API not found")
|
41 |
|
42 |
+
config.apis[api_name] = api_data
|
43 |
|
44 |
+
with open(config.config_path, "w", encoding="utf-8") as f:
|
45 |
+
json.dump(config, f, indent=2)
|
46 |
|
47 |
return {"message": f"API {api_name} updated"}
|
48 |
|
49 |
@router.post("/delete")
|
50 |
+
async def delete_api(request: Request, config: ServiceConfig = Depends(get_config)):
|
51 |
data = await request.json()
|
52 |
api_name = data.get("api_name")
|
53 |
|
54 |
if not api_name:
|
55 |
raise HTTPException(status_code=400, detail="api_name is required")
|
56 |
|
57 |
+
if api_name not in config.apis:
|
58 |
raise HTTPException(status_code=404, detail="API not found")
|
59 |
|
60 |
# Check if API is used in any intent
|
61 |
used_in = []
|
62 |
+
for project_name, project in config.projects.items():
|
63 |
for version in project.get("versions", []):
|
64 |
for intent in version.get("intents", []):
|
65 |
if intent.get("action") == api_name:
|
|
|
71 |
detail=f"API '{api_name}' is used in intents: {', '.join(used_in)}. Cannot delete."
|
72 |
)
|
73 |
|
74 |
+
del config.apis[api_name]
|
75 |
|
76 |
+
with open(config.config_path, "w", encoding="utf-8") as f:
|
77 |
+
json.dump(config, f, indent=2)
|
78 |
|
79 |
return {"message": f"API {api_name} deleted"}
|