Add data_visualization/js/api.js
This commit is contained in:
parent
619fc9737c
commit
160c6db0dc
1 changed files with 67 additions and 0 deletions
67
data_visualization/js/api.js
Normal file
67
data_visualization/js/api.js
Normal file
|
|
@ -0,0 +1,67 @@
|
|||
/**
|
||||
* js/api.js
|
||||
* Data Layer für Donau2Space Experimente
|
||||
* Kommunikation mit den API-Routen /data und /images
|
||||
*/
|
||||
|
||||
/**
|
||||
* Hilfsfunktion zum Erstellen der URL mit optionalen Query-Parametern
|
||||
* @param {string} baseUrl - Basisroute der API (z.B. '/data')
|
||||
* @param {Object} [params] - Optionale Parameter für Querystring
|
||||
* @returns {string} Komplette URL mit Querystring
|
||||
*/
|
||||
function buildUrl(baseUrl, params = {}) {
|
||||
const url = new URL(baseUrl, window.location.origin);
|
||||
Object.entries(params).forEach(([key, value]) => {
|
||||
if (value !== undefined && value !== null && value !== '') {
|
||||
url.searchParams.append(key, value);
|
||||
}
|
||||
});
|
||||
return url.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt Umweltdaten von der API /data.
|
||||
* @param {Object} [options] - Optional: Filter (time_range, sensor_id)
|
||||
* @returns {Promise<Array<{timestamp: string, temperature: number, wind_speed: number, humidity: number}>>>}
|
||||
*/
|
||||
export async function getData(options = {}) {
|
||||
const endpoint = buildUrl('/data', options);
|
||||
try {
|
||||
const response = await fetch(endpoint, { method: 'GET' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Fehler beim Laden von /data: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error('Antwortformat von /data ist ungültig. Erwartet: Array.');
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('getData() Fehler:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Holt Bild-URLs von der API /images.
|
||||
* @param {Object} [options] - Optional: Filter (start_time, end_time)
|
||||
* @returns {Promise<Array<{url: string, capture_time: string, analysis_result: string}>>>}
|
||||
*/
|
||||
export async function getImages(options = {}) {
|
||||
const endpoint = buildUrl('/images', options);
|
||||
try {
|
||||
const response = await fetch(endpoint, { method: 'GET' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Fehler beim Laden von /images: ${response.status}`);
|
||||
}
|
||||
const data = await response.json();
|
||||
if (!Array.isArray(data)) {
|
||||
throw new Error('Antwortformat von /images ist ungültig. Erwartet: Array.');
|
||||
}
|
||||
return data;
|
||||
} catch (error) {
|
||||
console.error('getImages() Fehler:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue