|
|
|
|
|
|
|
|
|
|
|
|
|
|
const getApiBaseUrl = () => { |
|
|
if (typeof window !== 'undefined' && window.APP_CONFIG) { |
|
|
return window.APP_CONFIG.BACKEND_URL || 'http://localhost:8000'; |
|
|
} |
|
|
return 'http://localhost:8000'; |
|
|
}; |
|
|
|
|
|
const API_BASE_URL = getApiBaseUrl(); |
|
|
|
|
|
|
|
|
const getDefaultHeaders = () => ({ |
|
|
'Content-Type': 'application/json', |
|
|
}); |
|
|
|
|
|
|
|
|
const getAuthHeader = () => { |
|
|
const token = localStorage.getItem('access_token'); |
|
|
if (token) { |
|
|
return { 'Authorization': `Bearer ${token}` }; |
|
|
} |
|
|
return {}; |
|
|
}; |
|
|
|
|
|
|
|
|
const getHeaders = (additionalHeaders = {}) => ({ |
|
|
...getDefaultHeaders(), |
|
|
...getAuthHeader(), |
|
|
...additionalHeaders, |
|
|
}); |
|
|
|
|
|
|
|
|
const apiService = { |
|
|
|
|
|
auth: { |
|
|
register: async (userData) => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/register`, { |
|
|
method: 'POST', |
|
|
headers: getHeaders(), |
|
|
body: JSON.stringify(userData) |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Registration failed'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
}, |
|
|
|
|
|
login: async (credentials) => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/login`, { |
|
|
method: 'POST', |
|
|
headers: getHeaders(), |
|
|
body: JSON.stringify(credentials) |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Login failed'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
}, |
|
|
|
|
|
logout: async () => { |
|
|
|
|
|
localStorage.removeItem('access_token'); |
|
|
return { message: 'Successfully logged out' }; |
|
|
}, |
|
|
|
|
|
getProfile: async () => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/profile`, { |
|
|
method: 'GET', |
|
|
headers: getHeaders() |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Failed to get profile'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
}, |
|
|
|
|
|
updateProfile: async (profileData) => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/auth/profile`, { |
|
|
method: 'PUT', |
|
|
headers: getHeaders(), |
|
|
body: JSON.stringify(profileData) |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Failed to update profile'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
chatbot: { |
|
|
query: async (queryData) => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/chatbot/query`, { |
|
|
method: 'POST', |
|
|
headers: getHeaders(), |
|
|
body: JSON.stringify(queryData) |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Failed to get response from chatbot'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
}, |
|
|
|
|
|
getConversations: async () => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/chatbot/conversations`, { |
|
|
method: 'GET', |
|
|
headers: getHeaders() |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Failed to get conversations'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
} |
|
|
}, |
|
|
|
|
|
|
|
|
book: { |
|
|
search: async (query) => { |
|
|
const response = await fetch(`${API_BASE_URL}/api/book/search?q=${encodeURIComponent(query)}`, { |
|
|
method: 'GET', |
|
|
headers: getHeaders() |
|
|
}); |
|
|
|
|
|
if (!response.ok) { |
|
|
const error = await response.json(); |
|
|
throw new Error(error.detail || 'Failed to search book content'); |
|
|
} |
|
|
|
|
|
return response.json(); |
|
|
} |
|
|
} |
|
|
}; |
|
|
|
|
|
export default apiService; |