Add data_visualization/js/api.js
This commit is contained in:
parent
fc41dd5a56
commit
b99d16330e
1 changed files with 43 additions and 0 deletions
43
data_visualization/js/api.js
Normal file
43
data_visualization/js/api.js
Normal file
|
|
@ -0,0 +1,43 @@
|
|||
/**
|
||||
* @module api
|
||||
* Stellt die API-Kommunikation für die Visualisierung bereit.
|
||||
* Verantwortlich für den Abruf der Messdaten von /data.
|
||||
*/
|
||||
|
||||
/**
|
||||
* Sendet eine GET-Anfrage an die /data-Route und gibt die Messdaten als JSON zurück.
|
||||
*
|
||||
* @async
|
||||
* @function fetchData
|
||||
* @param {Object} [filters={}] - Optionale Filter-Parameter (z. B. { from, to, exposure })
|
||||
* @returns {Promise<Array<Object>>} Array von Messobjekten [{timestamp, temperature, humidity, iso, exposure_time, brightness}]
|
||||
* @throws {Error} Wenn die Anfrage fehlschlägt oder ungültige Daten erhält
|
||||
*/
|
||||
export async function fetchData(filters = {}) {
|
||||
try {
|
||||
const queryParams = new URLSearchParams(filters);
|
||||
const url = queryParams.toString() ? `/data?${queryParams}` : '/data';
|
||||
|
||||
const response = await fetch(url, {
|
||||
method: 'GET',
|
||||
headers: {
|
||||
'Accept': 'application/json'
|
||||
}
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`Fehler beim Laden der Messdaten: ${response.status}`);
|
||||
}
|
||||
|
||||
const data = await response.json();
|
||||
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error('Unerwartetes Datenformat der API-Antwort');
|
||||
}
|
||||
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('fetchData():', error);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue