File size: 7,325 Bytes
db54c41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0a7c6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db54c41
 
 
0a7c6ee
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9f20e46
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// 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();
    }
};