Spaces:
Running
Running
Update src/services/apiService.js
Browse files- src/services/apiService.js +101 -101
src/services/apiService.js
CHANGED
|
@@ -1,101 +1,101 @@
|
|
| 1 |
-
/**
|
| 2 |
-
* API Service for Medical Multi-Agent System
|
| 3 |
-
* Handles communication with backend Web API
|
| 4 |
-
*/
|
| 5 |
-
|
| 6 |
-
const API_BASE_URL = process.env.REACT_APP_API_URL || "
|
| 7 |
-
|
| 8 |
-
class ApiService {
|
| 9 |
-
constructor() {
|
| 10 |
-
this.baseUrl = API_BASE_URL;
|
| 11 |
-
}
|
| 12 |
-
|
| 13 |
-
/**
|
| 14 |
-
* Generic API call wrapper
|
| 15 |
-
*/
|
| 16 |
-
async apiCall(endpoint, options = {}) {
|
| 17 |
-
const url = `${this.baseUrl}${endpoint}`;
|
| 18 |
-
const config = {
|
| 19 |
-
headers: {
|
| 20 |
-
"Content-Type": "application/json",
|
| 21 |
-
...options.headers,
|
| 22 |
-
},
|
| 23 |
-
...options,
|
| 24 |
-
};
|
| 25 |
-
|
| 26 |
-
try {
|
| 27 |
-
const response = await fetch(url, config);
|
| 28 |
-
|
| 29 |
-
if (!response.ok) {
|
| 30 |
-
const errorData = await response
|
| 31 |
-
.json()
|
| 32 |
-
.catch(() => ({ detail: "Unknown error" }));
|
| 33 |
-
throw new Error(
|
| 34 |
-
`API Error: ${response.status} - ${
|
| 35 |
-
errorData.detail || response.statusText
|
| 36 |
-
}`
|
| 37 |
-
);
|
| 38 |
-
}
|
| 39 |
-
|
| 40 |
-
return await response.json();
|
| 41 |
-
} catch (error) {
|
| 42 |
-
console.error("API call failed:", error);
|
| 43 |
-
throw error;
|
| 44 |
-
}
|
| 45 |
-
}
|
| 46 |
-
|
| 47 |
-
/**
|
| 48 |
-
* Health check
|
| 49 |
-
*/
|
| 50 |
-
async healthCheck() {
|
| 51 |
-
return this.apiCall("/api/health");
|
| 52 |
-
}
|
| 53 |
-
|
| 54 |
-
/**
|
| 55 |
-
* Start new chat session
|
| 56 |
-
*/
|
| 57 |
-
async startChat() {
|
| 58 |
-
return this.apiCall("/api/chat/start", {
|
| 59 |
-
method: "POST",
|
| 60 |
-
});
|
| 61 |
-
}
|
| 62 |
-
|
| 63 |
-
/**
|
| 64 |
-
* Send message to AI
|
| 65 |
-
*/
|
| 66 |
-
async sendMessage(message) {
|
| 67 |
-
return this.apiCall("/api/chat/send", {
|
| 68 |
-
method: "POST",
|
| 69 |
-
body: JSON.stringify({ message }),
|
| 70 |
-
});
|
| 71 |
-
}
|
| 72 |
-
|
| 73 |
-
/**
|
| 74 |
-
* Get current session status and patient record
|
| 75 |
-
*/
|
| 76 |
-
async getSessionStatus() {
|
| 77 |
-
return this.apiCall("/api/chat/status");
|
| 78 |
-
}
|
| 79 |
-
|
| 80 |
-
/**
|
| 81 |
-
* Generate doctor summary
|
| 82 |
-
*/
|
| 83 |
-
async generateSummary() {
|
| 84 |
-
return this.apiCall("/api/chat/summary", {
|
| 85 |
-
method: "POST",
|
| 86 |
-
});
|
| 87 |
-
}
|
| 88 |
-
|
| 89 |
-
/**
|
| 90 |
-
* Reset current session
|
| 91 |
-
*/
|
| 92 |
-
async resetSession() {
|
| 93 |
-
return this.apiCall("/api/chat/reset", {
|
| 94 |
-
method: "DELETE",
|
| 95 |
-
});
|
| 96 |
-
}
|
| 97 |
-
}
|
| 98 |
-
|
| 99 |
-
// Export singleton instance
|
| 100 |
-
export const apiService = new ApiService();
|
| 101 |
-
export default apiService;
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* API Service for Medical Multi-Agent System
|
| 3 |
+
* Handles communication with backend Web API
|
| 4 |
+
*/
|
| 5 |
+
|
| 6 |
+
const API_BASE_URL = process.env.REACT_APP_API_URL || "https://0a986f11bced.ngrok-free.app";
|
| 7 |
+
|
| 8 |
+
class ApiService {
|
| 9 |
+
constructor() {
|
| 10 |
+
this.baseUrl = API_BASE_URL;
|
| 11 |
+
}
|
| 12 |
+
|
| 13 |
+
/**
|
| 14 |
+
* Generic API call wrapper
|
| 15 |
+
*/
|
| 16 |
+
async apiCall(endpoint, options = {}) {
|
| 17 |
+
const url = `${this.baseUrl}${endpoint}`;
|
| 18 |
+
const config = {
|
| 19 |
+
headers: {
|
| 20 |
+
"Content-Type": "application/json",
|
| 21 |
+
...options.headers,
|
| 22 |
+
},
|
| 23 |
+
...options,
|
| 24 |
+
};
|
| 25 |
+
|
| 26 |
+
try {
|
| 27 |
+
const response = await fetch(url, config);
|
| 28 |
+
|
| 29 |
+
if (!response.ok) {
|
| 30 |
+
const errorData = await response
|
| 31 |
+
.json()
|
| 32 |
+
.catch(() => ({ detail: "Unknown error" }));
|
| 33 |
+
throw new Error(
|
| 34 |
+
`API Error: ${response.status} - ${
|
| 35 |
+
errorData.detail || response.statusText
|
| 36 |
+
}`
|
| 37 |
+
);
|
| 38 |
+
}
|
| 39 |
+
|
| 40 |
+
return await response.json();
|
| 41 |
+
} catch (error) {
|
| 42 |
+
console.error("API call failed:", error);
|
| 43 |
+
throw error;
|
| 44 |
+
}
|
| 45 |
+
}
|
| 46 |
+
|
| 47 |
+
/**
|
| 48 |
+
* Health check
|
| 49 |
+
*/
|
| 50 |
+
async healthCheck() {
|
| 51 |
+
return this.apiCall("/api/health");
|
| 52 |
+
}
|
| 53 |
+
|
| 54 |
+
/**
|
| 55 |
+
* Start new chat session
|
| 56 |
+
*/
|
| 57 |
+
async startChat() {
|
| 58 |
+
return this.apiCall("/api/chat/start", {
|
| 59 |
+
method: "POST",
|
| 60 |
+
});
|
| 61 |
+
}
|
| 62 |
+
|
| 63 |
+
/**
|
| 64 |
+
* Send message to AI
|
| 65 |
+
*/
|
| 66 |
+
async sendMessage(message) {
|
| 67 |
+
return this.apiCall("/api/chat/send", {
|
| 68 |
+
method: "POST",
|
| 69 |
+
body: JSON.stringify({ message }),
|
| 70 |
+
});
|
| 71 |
+
}
|
| 72 |
+
|
| 73 |
+
/**
|
| 74 |
+
* Get current session status and patient record
|
| 75 |
+
*/
|
| 76 |
+
async getSessionStatus() {
|
| 77 |
+
return this.apiCall("/api/chat/status");
|
| 78 |
+
}
|
| 79 |
+
|
| 80 |
+
/**
|
| 81 |
+
* Generate doctor summary
|
| 82 |
+
*/
|
| 83 |
+
async generateSummary() {
|
| 84 |
+
return this.apiCall("/api/chat/summary", {
|
| 85 |
+
method: "POST",
|
| 86 |
+
});
|
| 87 |
+
}
|
| 88 |
+
|
| 89 |
+
/**
|
| 90 |
+
* Reset current session
|
| 91 |
+
*/
|
| 92 |
+
async resetSession() {
|
| 93 |
+
return this.apiCall("/api/chat/reset", {
|
| 94 |
+
method: "DELETE",
|
| 95 |
+
});
|
| 96 |
+
}
|
| 97 |
+
}
|
| 98 |
+
|
| 99 |
+
// Export singleton instance
|
| 100 |
+
export const apiService = new ApiService();
|
| 101 |
+
export default apiService;
|