Spaces:
Running
on
CPU Upgrade
Running
on
CPU Upgrade
File size: 5,343 Bytes
f8362f3 d6b6619 31d11b5 d6b6619 f8362f3 d6b6619 |
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 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
// API Configuration and Service
const API_CONFIG = {
// Use the current origin in production
// In development, use localhost:3001 explicitly
BASE_URL:
process.env.NODE_ENV === "development" ||
window.location.hostname === "localhost"
? "http://localhost:3001"
: window.location.origin,
};
// API Service for making HTTP requests
const apiService = {
// Generic API call method with error handling
async call(endpoint, options = {}) {
try {
const url = endpoint.startsWith("http")
? endpoint
: `${API_CONFIG.BASE_URL}${endpoint}`;
const response = await fetch(url, {
...options,
headers: {
"Content-Type": "application/json",
...options.headers,
},
});
// If response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
// Check if response is JSON or text
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return await response.json();
}
return await response.text();
} catch (error) {
console.error("API call failed:", error);
throw error;
}
},
// GET request
async get(endpoint, options = {}) {
return await this.call(endpoint, {
method: "GET",
...options,
});
},
// POST request with JSON data
async post(endpoint, data, options = {}) {
return await this.call(endpoint, {
method: "POST",
body: JSON.stringify(data),
...options,
});
},
// POST request with FormData
async postFormData(endpoint, formData, options = {}) {
try {
const url = endpoint.startsWith("http")
? endpoint
: `${API_CONFIG.BASE_URL}${endpoint}`;
const response = await fetch(url, {
method: "POST",
body: formData,
...options,
headers: {
// Ne pas définir Content-Type pour FormData
...(options.headers || {}),
},
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
// Check if response is JSON or text
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
return await response.json();
}
return await response.text();
} catch (error) {
console.error("API FormData call failed:", error);
throw error;
}
},
// PUT request
async put(endpoint, data, options = {}) {
return await this.call(endpoint, {
method: "PUT",
body: JSON.stringify(data),
...options,
});
},
// DELETE request
async delete(endpoint, options = {}) {
return await this.call(endpoint, {
method: "DELETE",
...options,
});
},
// Upload a file
async uploadFile(file) {
const formData = new FormData();
formData.append("file", file);
// Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
try {
const response = await fetch(`${API_CONFIG.BASE_URL}/upload`, {
method: "POST",
body: formData,
// Ne pas définir le Content-Type pour FormData
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Upload file failed:", error);
throw error;
}
},
// Upload content from a URL
async uploadUrl(url) {
const formData = new FormData();
formData.append("url", url);
// Utilisation directe de fetch pour éviter les problèmes avec les en-têtes de FormData
try {
const response = await fetch(`${API_CONFIG.BASE_URL}/upload-url`, {
method: "POST",
body: formData,
// Ne pas définir le Content-Type pour FormData
});
// Si response is not ok, throw an error
if (!response.ok) {
const errorData = await response.json().catch(() => ({}));
throw new Error(errorData.detail || `API error: ${response.status}`);
}
return await response.json();
} catch (error) {
console.error("Upload URL failed:", error);
throw error;
}
},
// Get static document content
async getDocumentContent(docId, extension) {
return await this.get(`/${docId}.${extension}`);
},
// Download document
downloadDocument(docId, extension, documentName) {
try {
const link = document.createElement("a");
link.href = `/${docId}.${extension}`;
link.setAttribute("download", `${documentName}.${extension}`);
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
return true;
} catch (error) {
console.error("Download document failed:", error);
throw error;
}
},
};
export { API_CONFIG, apiService };
export default API_CONFIG;
|