Darak / front /api.js
Antigravity AI
Fix hardcoded localhost URLs to use relative paths for Hugging Face deployment
3f5ff7f
Raw
History Blame Contribute Delete
4.89 kB
// api.js
const API_BASE_URL = '';
export async function fetchRecommendations(query) {
try {
const response = await fetch(`${API_BASE_URL}/api/recommend`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(query)
});
if (!response.ok) throw new Error(`API error: ${response.status}`);
const data = await response.json();
return data.recommendations || [];
} catch (error) {
console.error("Failed to fetch recommendations:", error);
return [];
}
}
export async function ingestProperty(property) {
try {
const response = await fetch(`${API_BASE_URL}/api/ingest`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(property)
});
return await response.json();
} catch (error) {
console.error("Failed to ingest property:", error);
return null;
}
}
export async function logInteraction(propertyId, interactionType) {
try {
const token = localStorage.getItem('darak_token') || 'guest';
const response = await fetch(`${API_BASE_URL}/api/interact`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
user_id: token,
property_id: propertyId,
interaction_type: interactionType
})
});
return await response.json();
} catch (error) {
console.error("Failed to log interaction:", error);
return null;
}
}
export async function fetchAllProperties(limit = 50, offset = 0) {
try {
const response = await fetch(`${API_BASE_URL}/api/properties?limit=${limit}&offset=${offset}`);
if (!response.ok) throw new Error(`API error: ${response.status}`);
const data = await response.json();
return data.properties || [];
} catch (error) {
console.error("Failed to fetch all properties:", error);
return [];
}
}
export async function semanticSearch(query) {
try {
const safeQuery = String(query).trim().slice(0, 200);
if (!safeQuery) return [];
const response = await fetch(`${API_BASE_URL}/api/search?q=${encodeURIComponent(safeQuery)}`);
if (!response.ok) throw new Error(`API error: ${response.status}`);
const data = await response.json();
return data.properties || [];
} catch (error) {
console.error("Failed to search properties:", error);
return [];
}
}
export async function triggerScraping() {
const ALLOWED_SCRAPE_URL = `${API_BASE_URL}/api/scrape`;
try {
const response = await fetch(ALLOWED_SCRAPE_URL, { method: 'POST' });
if (!response.ok) throw new Error(`API error: ${response.status}`);
return await response.json();
} catch (error) {
console.error("Failed to trigger scraping:", error);
return { status: "error", message: error.message };
}
}
export async function fetchChatResponse(message, fileAttachment = null) {
try {
const token = localStorage.getItem('darak_token') || 'guest';
const payload = { message: message, user_id: token };
if (fileAttachment) {
payload.file_data = fileAttachment.base64;
payload.file_name = fileAttachment.name;
payload.file_type = fileAttachment.type;
}
const response = await fetch(`${API_BASE_URL}/api/chat`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(payload)
});
const data = await response.json();
return data.reply || "عذراً، لم أتمكن من الرد.";
} catch (error) {
console.error("Failed to fetch chat response:", error);
return "عذراً، حدث خطأ في الاتصال. يرجى المحاولة لاحقاً.";
}
}
export async function loginUser(username, password) {
const response = await fetch(`${API_BASE_URL}/api/auth/login`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
if (!response.ok) throw new Error("فشل تسجيل الدخول. يرجى التحقق من بياناتك.");
return await response.json();
}
export async function registerUser(username, password) {
const response = await fetch(`${API_BASE_URL}/api/auth/register`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ username, password })
});
if (!response.ok) throw new Error("اسم المستخدم موجود بالفعل. اختر اسماً آخر.");
return await response.json();
}