Spaces:
Running
Running
Update project_controller.py
Browse files- project_controller.py +96 -5
project_controller.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
from fastapi import APIRouter, Request, HTTPException
|
2 |
from service_config import ServiceConfig
|
3 |
import datetime
|
|
|
4 |
|
5 |
router = APIRouter()
|
6 |
service_config = ServiceConfig()
|
@@ -9,6 +10,10 @@ service_config.load()
|
|
9 |
def get_utc_now():
|
10 |
return datetime.datetime.utcnow().isoformat()
|
11 |
|
|
|
|
|
|
|
|
|
12 |
@router.post("/add")
|
13 |
async def add_project(request: Request):
|
14 |
data = await request.json()
|
@@ -24,7 +29,6 @@ async def add_project(request: Request):
|
|
24 |
"last_updated": get_utc_now()
|
25 |
}
|
26 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
27 |
-
import json
|
28 |
json.dump(service_config, f, indent=2)
|
29 |
return {"message": f"Project {project_name} added"}
|
30 |
|
@@ -41,12 +45,11 @@ async def update_project(request: Request):
|
|
41 |
if project["last_updated"] != client_last_updated:
|
42 |
raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
|
43 |
|
44 |
-
#
|
45 |
-
|
46 |
|
47 |
project["last_updated"] = get_utc_now()
|
48 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
49 |
-
import json
|
50 |
json.dump(service_config, f, indent=2)
|
51 |
return {"message": f"Project {project_name} updated"}
|
52 |
|
@@ -78,7 +81,95 @@ async def publish_project(request: Request):
|
|
78 |
project["last_updated"] = get_utc_now()
|
79 |
|
80 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
81 |
-
import json
|
82 |
json.dump(service_config, f, indent=2)
|
83 |
|
84 |
return {"message": f"Project {project_name} version published"}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from fastapi import APIRouter, Request, HTTPException
|
2 |
from service_config import ServiceConfig
|
3 |
import datetime
|
4 |
+
import json
|
5 |
|
6 |
router = APIRouter()
|
7 |
service_config = ServiceConfig()
|
|
|
10 |
def get_utc_now():
|
11 |
return datetime.datetime.utcnow().isoformat()
|
12 |
|
13 |
+
@router.get("/list")
|
14 |
+
def list_projects():
|
15 |
+
return service_config.projects
|
16 |
+
|
17 |
@router.post("/add")
|
18 |
async def add_project(request: Request):
|
19 |
data = await request.json()
|
|
|
29 |
"last_updated": get_utc_now()
|
30 |
}
|
31 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
|
|
32 |
json.dump(service_config, f, indent=2)
|
33 |
return {"message": f"Project {project_name} added"}
|
34 |
|
|
|
45 |
if project["last_updated"] != client_last_updated:
|
46 |
raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
|
47 |
|
48 |
+
# örnek update: sadece enable/disable flag güncelliyoruz
|
49 |
+
project["enabled"] = data.get("enabled", project["enabled"])
|
50 |
|
51 |
project["last_updated"] = get_utc_now()
|
52 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
|
|
53 |
json.dump(service_config, f, indent=2)
|
54 |
return {"message": f"Project {project_name} updated"}
|
55 |
|
|
|
81 |
project["last_updated"] = get_utc_now()
|
82 |
|
83 |
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
|
|
84 |
json.dump(service_config, f, indent=2)
|
85 |
|
86 |
return {"message": f"Project {project_name} version published"}
|
87 |
+
|
88 |
+
@router.post("/add_intent")
|
89 |
+
async def add_intent(request: Request):
|
90 |
+
data = await request.json()
|
91 |
+
project_name = data.get("project_name")
|
92 |
+
version_number = data.get("version_number")
|
93 |
+
intent = data.get("intent")
|
94 |
+
client_last_updated = data.get("client_last_updated")
|
95 |
+
|
96 |
+
project = service_config.projects.get(project_name)
|
97 |
+
if not project:
|
98 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
99 |
+
|
100 |
+
if project["last_updated"] != client_last_updated:
|
101 |
+
raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
|
102 |
+
|
103 |
+
version = next((v for v in project["versions"] if v["version_number"] == version_number), None)
|
104 |
+
if not version:
|
105 |
+
raise HTTPException(status_code=404, detail="Version not found")
|
106 |
+
|
107 |
+
if any(i["name"] == intent["name"] for i in version.get("intents", [])):
|
108 |
+
raise HTTPException(status_code=400, detail="Intent already exists")
|
109 |
+
|
110 |
+
version.setdefault("intents", []).append(intent)
|
111 |
+
version["last_updated"] = get_utc_now()
|
112 |
+
project["last_updated"] = get_utc_now()
|
113 |
+
|
114 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
115 |
+
json.dump(service_config, f, indent=2)
|
116 |
+
|
117 |
+
return {"message": f"Intent {intent['name']} added to project {project_name}, version {version_number}"}
|
118 |
+
|
119 |
+
@router.post("/delete_intent")
|
120 |
+
async def delete_intent(request: Request):
|
121 |
+
data = await request.json()
|
122 |
+
project_name = data.get("project_name")
|
123 |
+
version_number = data.get("version_number")
|
124 |
+
intent_name = data.get("intent_name")
|
125 |
+
client_last_updated = data.get("client_last_updated")
|
126 |
+
|
127 |
+
project = service_config.projects.get(project_name)
|
128 |
+
if not project:
|
129 |
+
raise HTTPException(status_code=404, detail="Project not found")
|
130 |
+
|
131 |
+
if project["last_updated"] != client_last_updated:
|
132 |
+
raise HTTPException(status_code=409, detail="This record has been updated by another user. Please reload.")
|
133 |
+
|
134 |
+
version = next((v for v in project["versions"] if v["version_number"] == version_number), None)
|
135 |
+
if not version:
|
136 |
+
raise HTTPException(status_code=404, detail="Version not found")
|
137 |
+
|
138 |
+
version["intents"] = [i for i in version.get("intents", []) if i["name"] != intent_name]
|
139 |
+
version["last_updated"] = get_utc_now()
|
140 |
+
project["last_updated"] = get_utc_now()
|
141 |
+
|
142 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
143 |
+
json.dump(service_config, f, indent=2)
|
144 |
+
|
145 |
+
return {"message": f"Intent {intent_name} deleted from project {project_name}, version {version_number}"}
|
146 |
+
|
147 |
+
@router.post("/add_api")
|
148 |
+
async def add_api(request: Request):
|
149 |
+
data = await request.json()
|
150 |
+
api_name = data.get("api_name")
|
151 |
+
api_def = data.get("api_def")
|
152 |
+
|
153 |
+
if api_name in service_config.apis:
|
154 |
+
raise HTTPException(status_code=400, detail="API already exists")
|
155 |
+
|
156 |
+
service_config.apis[api_name] = api_def
|
157 |
+
|
158 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
159 |
+
json.dump(service_config, f, indent=2)
|
160 |
+
|
161 |
+
return {"message": f"API {api_name} added"}
|
162 |
+
|
163 |
+
@router.post("/delete_api")
|
164 |
+
async def delete_api(request: Request):
|
165 |
+
data = await request.json()
|
166 |
+
api_name = data.get("api_name")
|
167 |
+
if api_name not in service_config.apis:
|
168 |
+
raise HTTPException(status_code=404, detail="API not found")
|
169 |
+
|
170 |
+
del service_config.apis[api_name]
|
171 |
+
|
172 |
+
with open(service_config.config_path, "w", encoding="utf-8") as f:
|
173 |
+
json.dump(service_config, f, indent=2)
|
174 |
+
|
175 |
+
return {"message": f"API {api_name} deleted"}
|