// Use relative URL for production (HF Spaces serves frontend + backend from same origin) // Use localhost for development when VITE_API_URL is not set and we're on localhost const getApiUrl = () => { if (import.meta.env.VITE_API_URL) { return import.meta.env.VITE_API_URL; } // In production (HF Spaces), use relative URL // In development (localhost), use localhost:8000 if (typeof window !== 'undefined' && window.location.hostname === 'localhost') { return 'http://localhost:8000/api'; } return '/api'; // Relative URL for production }; const API_URL = getApiUrl(); // API pour les simulations existantes (thermique) export const api = { createSimulation: async (name, parameters) => { const response = await fetch(`${API_URL}/simulations/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, parameters }) }); return response.json(); }, getSimulations: async () => { const response = await fetch(`${API_URL}/simulations/`); return response.json(); }, getSimulation: async (id) => { const response = await fetch(`${API_URL}/simulations/${id}/`); return response.json(); }, getResultImage: (simulation) => { // In production, use current origin; in dev, use localhost:8000 const baseUrl = import.meta.env.VITE_API_URL || (window.location.hostname === 'localhost' ? 'http://localhost:8000' : window.location.origin); return `${baseUrl}${simulation.result_image_path}`; } }; // API pour les simulations Poisson export const poissonApi = { createSimulation: async (params) => { const response = await fetch(`${API_URL}/poisson/simulations/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Erreur lors de la création'); } return response.json(); }, getSimulations: async () => { const response = await fetch(`${API_URL}/poisson/simulations/`); if (!response.ok) throw new Error('Erreur lors du chargement'); return response.json(); }, getSimulation: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/`); if (!response.ok) throw new Error('Simulation non trouvée'); return response.json(); }, deleteSimulation: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/`, { method: 'DELETE' }); if (!response.ok) throw new Error('Erreur lors de la suppression'); return true; }, previewMesh: async (params) => { const response = await fetch(`${API_URL}/poisson/simulations/preview_mesh/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (!response.ok) throw new Error('Erreur lors de la génération du maillage'); return response.json(); }, getMesh: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/mesh/`); if (!response.ok) throw new Error('Maillage non disponible'); return response.json(); }, getSolution: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/solution/`); if (!response.ok) throw new Error('Solution non disponible'); return response.json(); }, getAnimation: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/animation/`); if (!response.ok) throw new Error('Animation non disponible'); return response.json(); }, rerunSimulation: async (id) => { const response = await fetch(`${API_URL}/poisson/simulations/${id}/rerun/`, { method: 'POST' }); if (!response.ok) throw new Error('Erreur lors du relancement'); return response.json(); } }; // API pour Dang Van export const dangvanApi = { calculate: async (params) => { const response = await fetch(`${API_URL}/dangvan/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (!response.ok) throw new Error('Erreur lors du calcul'); return response.json(); } }; // API pour les simulations de diffusion thermique export const thermiqueApi = { createSimulation: async (params) => { const response = await fetch(`${API_URL}/thermique/simulations/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (!response.ok) { const error = await response.json(); throw new Error(error.detail || 'Erreur lors de la création'); } return response.json(); }, getSimulations: async () => { const response = await fetch(`${API_URL}/thermique/simulations/`); if (!response.ok) throw new Error('Erreur lors du chargement'); return response.json(); }, getSimulation: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/`); if (!response.ok) throw new Error('Simulation non trouvée'); return response.json(); }, deleteSimulation: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/`, { method: 'DELETE' }); if (!response.ok) throw new Error('Erreur lors de la suppression'); return true; }, previewMesh: async (params) => { const response = await fetch(`${API_URL}/thermique/simulations/preview_mesh/`, { method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(params) }); if (!response.ok) throw new Error('Erreur lors de la génération du maillage'); return response.json(); }, getMesh: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/mesh/`); if (!response.ok) throw new Error('Maillage non disponible'); return response.json(); }, getSolution: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/solution/`); if (!response.ok) throw new Error('Solution non disponible'); return response.json(); }, getAnimation: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/animation/`); if (!response.ok) throw new Error('Animation non disponible'); return response.json(); }, rerunSimulation: async (id) => { const response = await fetch(`${API_URL}/thermique/simulations/${id}/rerun/`, { method: 'POST' }); if (!response.ok) throw new Error('Erreur lors du relancement'); return response.json(); } };