File size: 2,296 Bytes
803d869
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
const BASE_URL = process.env.REACT_APP_BACKEND_URL || "http://localhost:8080";

const apiCall = async (

  endpoint,

  method = "GET",

  data = null,

  params = null,

) => {
  const token = localStorage.getItem("token");
  const headers = {
    "Content-Type": "application/json",
    ...(token && { Authorization: `Bearer ${token}` }),
  };

  // For GET requests, we should use params, not body
  let url = `${BASE_URL}${endpoint}`;
  if (params || (method === "GET" && data)) {
    const queryParams = new URLSearchParams(params || data).toString();
    url += `?${queryParams}`;
  }

  const options = {
    method,
    headers,
    ...(method !== "GET" && data && { body: JSON.stringify(data) }),
  };
  try {
    const response = await fetch(url, options);

    if (!response.ok) {
      if (response.status === 401) {
        localStorage.removeItem("token");
        window.location.href = "/login";
      }
      const errorText = await response.text();
      throw new Error(
        errorText || `Request failed with status ${response.status}`,
      );
    }

    return response.json();
  } catch (error) {
    console.error(`API Call Error: ${endpoint}`, error);
    throw error;
  }
};

export default {
  register: (userData) => apiCall("/user/register", "POST", userData),
  generateOtp: (email) => apiCall("/user/generateOtp", "POST", { email }),
  login: (email, otp) => apiCall("/user/login", "POST", { email, otp }),
  logout: () => apiCall("/user/logout", "POST"),
  getUserProfile: (username) => apiCall(`/user/profile`, "GET", null, { username }),
  submitReview: (reviewData) => apiCall("/user/review/submit", "POST", reviewData),
  getAllReviews: () => apiCall("/user/review/all", "GET"),
  createModel: () => apiCall("/model/create", "GET"),
  sendMail: (emailData) => apiCall("/model/sendMail", "POST", emailData),
  test: () => apiCall("/model/test", "GET"),
  translate: (data) => apiCall("/model/translate", "POST", data),
  grammarCheck: (data) => apiCall("/model/correct_text", "POST", data),
  summarizeText: (data) => apiCall("/model/summarize", "POST", data),
  startChat: (params) => apiCall("/model/start", "GET", null, params),
  chatWithBot: (data) => apiCall("/model/chat", "POST", data),
};