// API client for the Decidron network simulator. // // Defaults to relative URLs so a production build served by the FastAPI // container hits the same origin. For CRA dev (`npm start` on :3000), // set REACT_APP_API_URL=http://localhost:7860 in frontend/.env.development. const API_BASE = process.env.REACT_APP_API_URL !== undefined ? process.env.REACT_APP_API_URL : ''; async function jsonFetch(path, options = {}) { const resp = await fetch(`${API_BASE}${path}`, { headers: { 'Content-Type': 'application/json' }, cache: 'no-store', ...options, }); if (!resp.ok) { const err = await resp.json().catch(() => ({ detail: resp.statusText })); throw new Error(err.detail || `Request failed: ${resp.status}`); } return resp.json(); } export function getNetwork() { return jsonFetch('/api/decidron/network'); } export function getCommands() { return jsonFetch('/api/decidron/commands'); } export function createCommand(command) { return jsonFetch('/api/decidron/commands', { method: 'POST', body: JSON.stringify(command), }); } export function deleteCommand(name) { return jsonFetch(`/api/decidron/commands/${encodeURIComponent(name)}`, { method: 'DELETE', }); } export function submitSensorInput(channel, value) { return jsonFetch('/api/decidron/sensors/input', { method: 'POST', body: JSON.stringify({ channel, value }), }); } export function runSimulation(inputs = null) { return jsonFetch('/api/decidron/run', { method: 'POST', body: JSON.stringify(inputs ? { inputs } : {}), }); } export function getStats() { return jsonFetch('/api/decidron/stats'); } export function getHistory() { return jsonFetch('/api/decidron/history'); } export function resetSimulation() { return jsonFetch('/api/decidron/reset', { method: 'POST' }); }