HR-Collector/Extension/API.js
2024-12-31 16:35:06 +01:00

164 lines
4.4 KiB
JavaScript

const url = 'http://127.0.0.1:5180/api/';
async function pingServer() {
try {
const response = await fetch('http://127.0.0.1:5180/api/ping');
return response.ok; // Gibt true zurück, wenn der Server erreichbar ist
} catch (error) {
console.warn('Server ist nicht erreichbar:', error);
return false; // Gibt false zurück, wenn der Server nicht erreichbar ist
}
}
async function deleteHorseAPIAsync(id) {
try {
const apiUrl = `${url}deleteHorse/${id}`;
const response = await fetch(apiUrl, {
method: 'DELETE', // HTTP DELETE-Methode verwenden
headers: {
'Content-Type': 'application/json' // Falls nötig, Header setzen
}
});
if (!response.ok) {
console.warn(`Failed to delete horse with id ${id}: ${response.statusText}`);
}
console.log(response);
return response; // Erfolgreiche Antwort zurückgeben
} catch (error) {
console.warn(`Error deleting horse with id ${id}:`, error);
}
return;
}
async function setBaseDataHorseAPI(id, basedata) {
try {
console.log("Setting horse data");
console.log("ID:", id);
console.log("Basedata:", basedata);
const apiUrl = url+`updateHorse/${id}/BasicData`;
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(basedata),
headers: {
'Content-Type': 'application/json'
}
});
console.log("API Response:", response);
} catch (error) {
console.error("Error setting horse data: " + error.message);
}
}
async function getColorsAPIAsync(id)
{
let colors;
try {
const apiUrl = url+`getHorse/${id}/Colors`;
await fetch(apiUrl)
.then(response => response.json())
.then(data => {
colors = data;
});
} catch (error) {
console.log(error);
}
return colors;
}
async function getHorseNotesAPIAsync(id)
{
let notes;
try {
const apiUrl = url+`getHorse/${id}/Notes`;
await fetch(apiUrl)
.then(response => response.text())
.then(data => {
notes = data;
});
} catch (error) {
console.log(error);
}
return notes;
}
async function getHorseLoadStateAPIAsync(id)
{
let horseData;
try {
const apiUrl = url+`getHorse/${id}/LoadState`;
await fetch(apiUrl)
.then(response => response.json())
.then(data => {
horseData = data;
});
} catch (error) {
console.log(error);
}
return horseData;
}
async function setHorseSummaryAPIAsync(id, summaryData)
{
const apiUrl = url+`updateHorse/${id}/Summary`;
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(summaryData),
headers: {
'Content-Type': 'application/json'
}
});
return response;
}
async function setHorseTrainingAPIAsync(id, training)
{
const apiUrl = url+`updateHorse/${id}/Training`;
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify({ Training:training }),
headers: {
'Content-Type': 'application/json'
}
});
return response;
}
async function setHorseGeneticsAPIAsync(id, genetics)
{
const apiUrl = url+`updateHorse/${id}/Genetics`;
console.log("Accessing API at:", apiUrl);
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(genetics),
headers: {
'Content-Type': 'application/json'
}
});
return response;
}
async function setHorseHealthAPIAsync(id, health)
{
const apiUrl = url+`updateHorse/${id}/Health`;
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify({ Health: health }),
headers: {
'Content-Type': 'application/json'
}
});
return response;
}
// Neue Funktion für Achievements
async function setHorseAchievementsAPIAsync(id, achievements)
{
const apiUrl = url+`updateHorse/${id}/Achievements`;
const response = await fetch(apiUrl, {
method: 'POST',
body: JSON.stringify(achievements),
headers: {
'Content-Type': 'application/json'
}
});
return response;
}