Skip to content

Commit 05bf747

Browse files
committed
included status in JsonResponse
Signed-off-by: <Keval Mahajan> <[email protected]>
1 parent 688a2da commit 05bf747

File tree

2 files changed

+40
-37
lines changed

2 files changed

+40
-37
lines changed

mcpgateway/admin.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -733,7 +733,7 @@ async def admin_get_gateway(gateway_id: int, db: Session = Depends(get_db), user
733733

734734

735735
@admin_router.post("/gateways")
736-
async def admin_add_gateway(request: Request, db: Session = Depends(get_db), user: str = Depends(require_auth)) -> RedirectResponse:
736+
async def admin_add_gateway(request: Request, db: Session = Depends(get_db), user: str = Depends(require_auth)) -> JSONResponse:
737737
"""Add a gateway via the admin UI.
738738
739739
Expects form fields:
@@ -766,16 +766,20 @@ async def admin_add_gateway(request: Request, db: Session = Depends(get_db), use
766766
root_path = request.scope.get("root_path", "")
767767
try:
768768
await gateway_service.register_gateway(db, gateway)
769-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=303)
769+
return JSONResponse(
770+
content={"message": "Gateway registered successfully!", "success": True},
771+
status_code=200,
772+
)
773+
770774
except Exception as ex:
771775
if isinstance(ex, GatewayConnectionError):
772-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=502)
776+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=502)
773777
if isinstance(ex, ValueError):
774-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=400)
778+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=400)
775779
if isinstance(ex, RuntimeError):
776-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=500)
780+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=500)
781+
return JSONResponse(content={"message": str(ex), "success": False}, status_code=500)
777782

778-
return RedirectResponse(f"{root_path}/admin#gateways", status_code=500)
779783

780784

781785
@admin_router.post("/gateways/{gateway_id}/edit")

mcpgateway/static/admin.js

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -147,43 +147,42 @@ document.addEventListener("DOMContentLoaded", function () {
147147
}
148148
});
149149

150-
document
151-
.getElementById("add-gateway-form")
152-
.addEventListener("submit", (e) => {
153-
e.preventDefault();
150+
document.getElementById("add-gateway-form")
151+
.addEventListener("submit", async (e) => {
152+
e.preventDefault();
154153

155-
const form = e.target;
156-
const formData = new FormData(form);
154+
const form = e.target;
155+
const formData = new FormData(form);
157156

158-
const status = document.getElementById("status-gateways");
159-
const loading = document.getElementById("add-gateway-loading");
157+
const status = document.getElementById("status-gateways");
158+
const loading = document.getElementById("add-gateway-loading");
160159

161-
// Show loading and clear previous status
162-
loading.style.display = "block";
163-
status.textContent = "";
164-
status.classList.remove("error-status");
160+
// Show loading and clear previous status
161+
loading.style.display = "block";
162+
status.textContent = "";
163+
status.classList.remove("error-status");
165164

166-
fetch(`${window.ROOT_PATH}/admin/gateways`, {
167-
method: "POST",
168-
body: formData,
169-
})
170-
.then((response) => {
171-
if (!response.ok) {
172-
status.textContent = "Connection failed!";
165+
try {
166+
const response = await fetch(`${window.ROOT_PATH}/admin/gateways`, {
167+
method: "POST",
168+
body: formData,
169+
});
170+
171+
let result = await response.json();
172+
if (!result.success) {
173+
alert(result.message || "An error occurred");
174+
} else {
175+
window.location.href = `${window.ROOT_PATH}/admin#gateways`; // Redirect on success
176+
}
177+
178+
} catch (error) {
179+
console.error("Error:", error);
180+
status.textContent = error.message || "An error occurred!";
173181
status.classList.add("error-status");
174-
} else {
175-
location.reload(); // Will exit before hiding spinner
182+
} finally {
183+
loading.style.display = "none"; // Hide loading spinner
176184
}
177-
})
178-
.catch((error) => {
179-
console.error("Error:", error);
180-
status.textContent = "An error occurred!";
181-
status.classList.add("error-status");
182-
})
183-
.finally(() => {
184-
loading.style.display = "none"; // Hide loading
185185
});
186-
});
187186

188187

189188
document
@@ -305,7 +304,7 @@ document.addEventListener("DOMContentLoaded", function () {
305304
if (!result.success) {
306305
alert(result.message || "An error occurred");
307306
} else {
308-
window.location.href = "/admin#tools"; // Redirect on success
307+
window.location.href = `${window.ROOT_PATH}/admin#tools`; // Redirect on success
309308
}
310309
} catch (error) {
311310
console.error("Fetch error:", error);

0 commit comments

Comments
 (0)