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