File size: 4,142 Bytes
e2eff86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// apiService.js - A service to handle API calls to the backend

// Get the backend URL from the global configuration
// This is set up in src/clientModules/initAppConfig.js
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();

// Default headers for API requests
const getDefaultHeaders = () => ({
  'Content-Type': 'application/json',
});

// Get authorization header if token exists
const getAuthHeader = () => {
  const token = localStorage.getItem('access_token');
  if (token) {
    return { 'Authorization': `Bearer ${token}` };
  }
  return {};
};

// Function to merge headers
const getHeaders = (additionalHeaders = {}) => ({
  ...getDefaultHeaders(),
  ...getAuthHeader(),
  ...additionalHeaders,
});

// API service object
const apiService = {
  // Authentication endpoints
  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 () => {
      // In a real implementation, this would invalidate the token on the server
      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 endpoints
  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 content endpoints
  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;