photo_server_json_con_aves22/public/js/admin/api.js
2026-04-18 20:14:42 +02:00

87 lines
No EOL
2.2 KiB
JavaScript

// ======================================================
// API CORE REQUEST
// ======================================================
async function apiRequest(method, path, body = null) {
if (!Admin.BASE_URL) {
console.error("BASE_URL non ancora inizializzato");
throw new Error("BASE_URL non inizializzato");
}
const options = {
method,
headers: {
"Authorization": "Bearer " + Admin.token,
"Content-Type": "application/json"
}
};
if (body) {
options.body = JSON.stringify(body);
}
const res = await fetch(`${Admin.BASE_URL}${path}`, options);
if (!res.ok) {
const text = await res.text();
throw new Error(`Errore API ${res.status}: ${text}`);
}
return res.json();
}
// ======================================================
// API GET
// ======================================================
function apiGet(path) {
return apiRequest("GET", path);
}
// ======================================================
// API POST
// ======================================================
function apiPost(path, body = null) {
return apiRequest("POST", path, body);
}
// ======================================================
// API DELETE
// ======================================================
function apiDelete(path) {
return apiRequest("DELETE", path);
}
// ======================================================
// UTILITY: LEGGI DATA/ORA DAL CAMPO SINCE
// ======================================================
function getSinceISO() {
const el = document.getElementById("sinceInput");
if (!el || !el.value) return null;
return new Date(el.value).toISOString();
}
// ======================================================
// GET DB CHANGES (solo modifiche nel DB)
// ======================================================
async function getDBChanges(since, user) {
return apiGet(`/photos/changes?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
}
// ======================================================
// GET HARD DELETED (solo hard delete)
// ======================================================
async function getHardDeleted(since, user) {
return apiGet(`/photos/deleted_hard?since=${encodeURIComponent(since)}&user=${encodeURIComponent(user)}`);
}