diff --git a/metric_visualization/js/api.js b/metric_visualization/js/api.js new file mode 100644 index 0000000..cb3be43 --- /dev/null +++ b/metric_visualization/js/api.js @@ -0,0 +1,37 @@ +/** + * @module api + * @description Kommunikationsmodul für den Abruf von Analyseergebnissen über den /api/analysis Endpunkt. + */ + +/** + * Führt einen GET-Request an /api/analysis aus und gibt die JSON-Daten zurück. + * @async + * @function fetchAnalysisData + * @returns {Promise>} Analyseergebnisse + * @throws {Error} Wenn der Abruf fehlschlägt oder die Antwort kein gültiges JSON ist. + */ +export async function fetchAnalysisData() { + try { + const response = await fetch('/api/analysis', { + method: 'GET', + headers: { + 'Accept': 'application/json' + } + }); + + if (!response.ok) { + throw new Error(`API-Fehler: ${response.status} ${response.statusText}`); + } + + const data = await response.json(); + + if (!Array.isArray(data)) { + throw new Error('Ungültiges Antwortformat: Erwartet wird ein JSON-Array'); + } + + return data; + } catch (error) { + console.error('Fehler beim Abrufen der Analyseergebnisse:', error); + throw error; + } +}