Spaces:
Runtime error
Runtime error
File size: 5,268 Bytes
0c6bee4 37a6ee1 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 | const BASE_URL = import.meta.env.VITE_API_URL || "http://localhost:8000";
/**
* Helper to process fetch responses, parsing JSON and raising errors with detailed messages if available.
*/
async function handleResponse(response) {
if (!response.ok) {
let errMsg = `Request failed with status ${response.status}`;
try {
const data = await response.json();
if (data && data.detail) {
if (typeof data.detail === "string") {
errMsg = data.detail;
} else if (Array.isArray(data.detail)) {
// Parse FastAPI validation errors
errMsg = data.detail.map(err => `${err.loc.join(".")}: ${err.msg}`).join(", ");
}
}
} catch (e) {
// Fallback to text if parsing fails
try {
const text = await response.text();
if (text) errMsg = text;
} catch (innerErr) {}
}
throw new Error(errMsg);
}
// Return parsed data and preserve headers if needed
const jsonData = await response.json();
// If we have custom headers like X-Process-Time, we can attach it to the returned data structure
if (response.headers.has("X-Process-Time")) {
jsonData._processTime = response.headers.get("X-Process-Time");
}
return jsonData;
}
export async function getDatasets() {
console.log("[API] Fetching datasets...");
const response = await fetch(`${BASE_URL}/datasets`);
return handleResponse(response);
}
export async function getSchema(dbName) {
console.log(`[API] Fetching schema for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/datasets/${encodeURIComponent(dbName)}/schema`);
return handleResponse(response);
}
export async function getPreview(dbName) {
console.log(`[API] Fetching preview for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/datasets/${encodeURIComponent(dbName)}/preview`);
return handleResponse(response);
}
export async function uploadCSV(file) {
console.log(`[API] Uploading CSV file: ${file.name}...`);
const formData = new FormData();
formData.append("file", file);
const response = await fetch(`${BASE_URL}/upload`, {
method: "POST",
body: formData
// Note: Do not set Content-Type header manually. The browser will auto-set it with boundary.
});
return handleResponse(response);
}
export async function deleteDataset(dbName) {
console.log(`[API] Deleting database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/datasets/${encodeURIComponent(dbName)}`, {
method: "DELETE"
});
return handleResponse(response);
}
export async function queryDataset(question, dbName) {
console.log(`[API] Querying database: ${dbName} with question: '${question}'...`);
const response = await fetch(`${BASE_URL}/query`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ question, db_name: dbName })
});
return handleResponse(response);
}
export async function getSuggestions(dbName) {
console.log(`[API] Fetching suggestions for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/datasets/${encodeURIComponent(dbName)}/suggestions`);
return handleResponse(response);
}
export async function getQueryHistory(dbName) {
console.log(`[API] Fetching history for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/query/history?db_name=${encodeURIComponent(dbName)}`);
return handleResponse(response);
}
export async function getSchemaDatasets() {
console.log("[API] Fetching schema datasets...");
const response = await fetch(`${BASE_URL}/schema-datasets`);
return handleResponse(response);
}
export async function getSchemaInfo(dbName) {
console.log(`[API] Fetching schema details for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/schema-datasets/${encodeURIComponent(dbName)}/schema`);
return handleResponse(response);
}
export async function getSchemaPreview(dbName) {
console.log(`[API] Fetching schema preview for database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/schema-datasets/${encodeURIComponent(dbName)}/preview`);
return handleResponse(response);
}
export async function uploadSchemaDB(sqlFile, erdImage = null) {
console.log(`[API] Uploading schema files...`);
const formData = new FormData();
if (sqlFile) {
formData.append("schema_file", sqlFile);
}
if (erdImage) {
formData.append("erd_image", erdImage);
}
const response = await fetch(`${BASE_URL}/upload/schema`, {
method: "POST",
body: formData
});
return handleResponse(response);
}
export async function deleteSchemaDataset(dbName) {
console.log(`[API] Deleting schema database: ${dbName}...`);
const response = await fetch(`${BASE_URL}/schema-datasets/${encodeURIComponent(dbName)}`, {
method: "DELETE"
});
return handleResponse(response);
}
export async function querySchema(question, dbName) {
console.log(`[API] Querying schema database: ${dbName} with question: '${question}'...`);
const response = await fetch(`${BASE_URL}/schema-query`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify({ question, db_name: dbName })
});
return handleResponse(response);
}
|