File size: 9,790 Bytes
bc2f725 |
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 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 |
/**
* API Client - Centralized API Communication
* Crypto Monitor HF - Enterprise Edition
*/
class APIClient {
constructor(baseURL = '') {
this.baseURL = baseURL;
this.defaultHeaders = {
'Content-Type': 'application/json',
};
}
/**
* Generic fetch wrapper with error handling
*/
async request(endpoint, options = {}) {
const url = `${this.baseURL}${endpoint}`;
const config = {
headers: { ...this.defaultHeaders, ...options.headers },
...options,
};
try {
const response = await fetch(url, config);
if (!response.ok) {
throw new Error(`HTTP ${response.status}: ${response.statusText}`);
}
// Handle different content types
const contentType = response.headers.get('content-type');
if (contentType && contentType.includes('application/json')) {
return await response.json();
} else if (contentType && contentType.includes('text')) {
return await response.text();
}
return response;
} catch (error) {
console.error(`[APIClient] Error fetching ${endpoint}:`, error);
throw error;
}
}
/**
* GET request
*/
async get(endpoint) {
return this.request(endpoint, { method: 'GET' });
}
/**
* POST request
*/
async post(endpoint, data) {
return this.request(endpoint, {
method: 'POST',
body: JSON.stringify(data),
});
}
/**
* PUT request
*/
async put(endpoint, data) {
return this.request(endpoint, {
method: 'PUT',
body: JSON.stringify(data),
});
}
/**
* DELETE request
*/
async delete(endpoint) {
return this.request(endpoint, { method: 'DELETE' });
}
// ===== Core API Methods =====
/**
* Get system health
*/
async getHealth() {
return this.get('/api/health');
}
/**
* Get system status
*/
async getStatus() {
return this.get('/api/status');
}
/**
* Get system stats
*/
async getStats() {
return this.get('/api/stats');
}
/**
* Get system info
*/
async getInfo() {
return this.get('/api/info');
}
// ===== Market Data =====
/**
* Get market overview
*/
async getMarket() {
return this.get('/api/market');
}
/**
* Get trending coins
*/
async getTrending() {
return this.get('/api/trending');
}
/**
* Get sentiment analysis
*/
async getSentiment() {
return this.get('/api/sentiment');
}
/**
* Get DeFi protocols
*/
async getDefi() {
return this.get('/api/defi');
}
// ===== Providers API =====
/**
* Get all providers
*/
async getProviders() {
return this.get('/api/providers');
}
/**
* Get specific provider
*/
async getProvider(providerId) {
return this.get(`/api/providers/${providerId}`);
}
/**
* Get providers by category
*/
async getProvidersByCategory(category) {
return this.get(`/api/providers/category/${category}`);
}
/**
* Health check for provider
*/
async checkProviderHealth(providerId) {
return this.post(`/api/providers/${providerId}/health-check`);
}
/**
* Add custom provider
*/
async addProvider(providerData) {
return this.post('/api/providers', providerData);
}
/**
* Remove provider
*/
async removeProvider(providerId) {
return this.delete(`/api/providers/${providerId}`);
}
// ===== Pools API =====
/**
* Get all pools
*/
async getPools() {
return this.get('/api/pools');
}
/**
* Get specific pool
*/
async getPool(poolId) {
return this.get(`/api/pools/${poolId}`);
}
/**
* Create new pool
*/
async createPool(poolData) {
return this.post('/api/pools', poolData);
}
/**
* Delete pool
*/
async deletePool(poolId) {
return this.delete(`/api/pools/${poolId}`);
}
/**
* Add member to pool
*/
async addPoolMember(poolId, providerId) {
return this.post(`/api/pools/${poolId}/members`, { provider_id: providerId });
}
/**
* Remove member from pool
*/
async removePoolMember(poolId, providerId) {
return this.delete(`/api/pools/${poolId}/members/${providerId}`);
}
/**
* Rotate pool
*/
async rotatePool(poolId) {
return this.post(`/api/pools/${poolId}/rotate`);
}
/**
* Get pool history
*/
async getPoolHistory() {
return this.get('/api/pools/history');
}
// ===== Logs API =====
/**
* Get logs
*/
async getLogs(params = {}) {
const query = new URLSearchParams(params).toString();
return this.get(`/api/logs${query ? '?' + query : ''}`);
}
/**
* Get recent logs
*/
async getRecentLogs() {
return this.get('/api/logs/recent');
}
/**
* Get error logs
*/
async getErrorLogs() {
return this.get('/api/logs/errors');
}
/**
* Get log stats
*/
async getLogStats() {
return this.get('/api/logs/stats');
}
/**
* Export logs as JSON
*/
async exportLogsJSON() {
return this.get('/api/logs/export/json');
}
/**
* Export logs as CSV
*/
async exportLogsCSV() {
return this.get('/api/logs/export/csv');
}
/**
* Clear logs
*/
async clearLogs() {
return this.delete('/api/logs');
}
// ===== Resources API =====
/**
* Get resources
*/
async getResources() {
return this.get('/api/resources');
}
/**
* Get resources by category
*/
async getResourcesByCategory(category) {
return this.get(`/api/resources/category/${category}`);
}
/**
* Import resources from JSON
*/
async importResourcesJSON(data) {
return this.post('/api/resources/import/json', data);
}
/**
* Export resources as JSON
*/
async exportResourcesJSON() {
return this.get('/api/resources/export/json');
}
/**
* Export resources as CSV
*/
async exportResourcesCSV() {
return this.get('/api/resources/export/csv');
}
/**
* Backup resources
*/
async backupResources() {
return this.post('/api/resources/backup');
}
/**
* Add resource provider
*/
async addResourceProvider(providerData) {
return this.post('/api/resources/provider', providerData);
}
/**
* Delete resource provider
*/
async deleteResourceProvider(providerId) {
return this.delete(`/api/resources/provider/${providerId}`);
}
/**
* Get discovery status
*/
async getDiscoveryStatus() {
return this.get('/api/resources/discovery/status');
}
/**
* Run discovery
*/
async runDiscovery() {
return this.post('/api/resources/discovery/run');
}
// ===== HuggingFace API =====
/**
* Get HuggingFace health
*/
async getHFHealth() {
return this.get('/api/hf/health');
}
/**
* Run HuggingFace sentiment analysis
*/
async runHFSentiment(data) {
return this.post('/api/hf/run-sentiment', data);
}
// ===== Reports API =====
/**
* Get discovery report
*/
async getDiscoveryReport() {
return this.get('/api/reports/discovery');
}
/**
* Get models report
*/
async getModelsReport() {
return this.get('/api/reports/models');
}
// ===== Diagnostics API =====
/**
* Run diagnostics
*/
async runDiagnostics() {
return this.post('/api/diagnostics/run');
}
/**
* Get last diagnostics
*/
async getLastDiagnostics() {
return this.get('/api/diagnostics/last');
}
// ===== Sessions API =====
/**
* Get active sessions
*/
async getSessions() {
return this.get('/api/sessions');
}
/**
* Get session stats
*/
async getSessionStats() {
return this.get('/api/sessions/stats');
}
/**
* Broadcast message
*/
async broadcast(message) {
return this.post('/api/broadcast', { message });
}
// ===== Feature Flags API =====
/**
* Get all feature flags
*/
async getFeatureFlags() {
return this.get('/api/feature-flags');
}
/**
* Get single feature flag
*/
async getFeatureFlag(flagName) {
return this.get(`/api/feature-flags/${flagName}`);
}
/**
* Update feature flags
*/
async updateFeatureFlags(flags) {
return this.put('/api/feature-flags', { flags });
}
/**
* Update single feature flag
*/
async updateFeatureFlag(flagName, value) {
return this.put(`/api/feature-flags/${flagName}`, { flag_name: flagName, value });
}
/**
* Reset feature flags to defaults
*/
async resetFeatureFlags() {
return this.post('/api/feature-flags/reset');
}
// ===== Proxy API =====
/**
* Get proxy status
*/
async getProxyStatus() {
return this.get('/api/proxy-status');
}
}
// Create global instance
window.apiClient = new APIClient();
console.log('[APIClient] Initialized');
|