ntphuc149's picture
Update src/services/apiService.js
18cd20d verified
/**
* API Service for Medical Multi-Agent System
* Handles communication with backend Web API
*/
const API_BASE_URL = process.env.REACT_APP_API_URL || "https://0a986f11bced.ngrok-free.app";
class ApiService {
constructor() {
this.baseUrl = API_BASE_URL;
}
/**
* Generic API call wrapper
*/
async apiCall(endpoint, options = {}) {
const url = `${this.baseUrl}${endpoint}`;
const config = {
headers: {
"Content-Type": "application/json",
"ngrok-skip-browser-warning": "true",
...options.headers,
},
...options,
};
try {
const response = await fetch(url, config);
if (!response.ok) {
const errorData = await response
.json()
.catch(() => ({ detail: "Unknown error" }));
throw new Error(
`API Error: ${response.status} - ${
errorData.detail || response.statusText
}`
);
}
return await response.json();
} catch (error) {
console.error("API call failed:", error);
throw error;
}
}
/**
* Health check
*/
async healthCheck() {
return this.apiCall("/api/health");
}
/**
* Start new chat session
*/
async startChat() {
return this.apiCall("/api/chat/start", {
method: "POST",
});
}
/**
* Send message to AI
*/
async sendMessage(message) {
return this.apiCall("/api/chat/send", {
method: "POST",
body: JSON.stringify({ message }),
});
}
/**
* Get current session status and patient record
*/
async getSessionStatus() {
return this.apiCall("/api/chat/status");
}
/**
* Generate doctor summary
*/
async generateSummary() {
return this.apiCall("/api/chat/summary", {
method: "POST",
});
}
/**
* Reset current session
*/
async resetSession() {
return this.apiCall("/api/chat/reset", {
method: "DELETE",
});
}
}
// Export singleton instance
export const apiService = new ApiService();
export default apiService;