ciyidogan commited on
Commit
948547c
·
verified ·
1 Parent(s): 8679eba

Create api_controller.py

Browse files
Files changed (1) hide show
  1. api_controller.py +85 -0
api_controller.py ADDED
@@ -0,0 +1,85 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from fastapi import APIRouter, Request, HTTPException
2
+ from service_config import ServiceConfig
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 service_config.apis
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")
22
+
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 service_config.apis:
27
+ raise HTTPException(status_code=400, detail="API with this name already exists")
28
+
29
+ service_config.apis[api_name] = api_data
30
+
31
+ with open(service_config.config_path, "w", encoding="utf-8") as f:
32
+ json.dump(service_config, f, indent=2)
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")
41
+
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 service_config.apis:
46
+ raise HTTPException(status_code=404, detail="API not found")
47
+
48
+ service_config.apis[api_name] = api_data
49
+
50
+ with open(service_config.config_path, "w", encoding="utf-8") as f:
51
+ json.dump(service_config, f, indent=2)
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 service_config.apis:
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 service_config.projects.items():
69
+ for version in project.get("versions", []):
70
+ for intent in version.get("intents", []):
71
+ if intent.get("action") == api_name:
72
+ used_in.append(f"{project_name} → v{version.get('version_number')} → {intent.get('name')}")
73
+
74
+ if used_in:
75
+ raise HTTPException(
76
+ status_code=400,
77
+ detail=f"API '{api_name}' is used in intents: {', '.join(used_in)}. Cannot delete."
78
+ )
79
+
80
+ del service_config.apis[api_name]
81
+
82
+ with open(service_config.config_path, "w", encoding="utf-8") as f:
83
+ json.dump(service_config, f, indent=2)
84
+
85
+ return {"message": f"API {api_name} deleted"}