"""HTTP routes for the network module — mounted onto the main app via modules/registry.py.""" from __future__ import annotations import asyncio as _asyncio from typing import Any, Dict from fastapi import APIRouter, Body, HTTPException from . import backend as net MANIFEST = {"key": "network", "label": "Network", "icon": "📡"} router = APIRouter(prefix="/network", tags=["network"]) @router.get("/status") async def network_status(): return {"hostname": net.hostname(), "connection": net.primary_connection(), "vpn": net.vpn_status()} @router.post("/vpn/toggle") async def network_vpn_toggle(payload: Dict[str, Any] = Body(...)): try: return await _asyncio.to_thread(net.vpn_toggle, bool(payload.get("enable"))) except RuntimeError as e: raise HTTPException(status_code=400, detail=str(e)) except Exception as e: raise HTTPException(status_code=502, detail=f"nmcli error: {e}") @router.get("/targets") async def network_targets(): return {"targets": net.list_targets()} @router.post("/targets") async def network_target_create(payload: Dict[str, Any] = Body(...)): host = (payload.get("host") or "").strip() if not host: raise HTTPException(status_code=400, detail="'host' is required") return net.add_target((payload.get("label") or "").strip() or host, host) @router.delete("/targets/{target_id}") async def network_target_delete(target_id: str): net.delete_target(target_id) return {"status": "deleted"} @router.get("/ping") async def network_ping_all(): return {"targets": await _asyncio.to_thread(net.ping_targets)}