ocr / src /services /api.js
ariansyahdedy's picture
test with yaml
7f37689
import axios from 'axios';
import axiosInstance from './axiosInstance';
// Ensure you have VITE_API_URL set in your .env file
const API_URL = import.meta.env.VITE_API_URL;
export const register = async (first_name, surname, email, phone, country, address, password) => {
const data = new URLSearchParams();
data.append('first_name', first_name);
data.append('surname', surname);
data.append('email', email);
data.append('phone', phone);
data.append('country', country);
data.append('address', address);
data.append('password', password);
try {
console.log('data', data);
const response = await axios.post(`${API_URL}/api/v1/user/`, data,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
});
return response.data;
} catch (error) {
console.error('Registration failed:', error);
throw error;
}
};
export const login = async (email, password) => {
const data = new URLSearchParams();
data.append('username', email);
data.append('password', password);
try {
const response = await axios.post(`${API_URL}/token`, data,{
headers: {
'Content-Type': 'application/x-www-form-urlencoded',
'accept': 'application/json',
},
});
return response;
} catch (error) {
console.error('Login failed:', error);
throw error;
}
};
export const logoutUser = () => {
localStorage.removeItem('token');
};
export const getTemplates = async (templateName) => {
try {
const response = await axiosInstance.get(`${API_URL}/api/v1/ocrtemplate/`, { params: { template_name: templateName } });
return response.data;
} catch (error) {
console.error('Error fetching templates:', error);
throw error;
}
};
export const createTemplate = async (templateName, fields, userId) => {
const data = {
template_name: templateName,
fields: fields,
user_id: userId,
};
try {
const response = await axiosInstance.post(`${API_URL}/api/v1/ocrtemplate/`, data);
return response.data;
} catch (error) {
console.error('Error creating template:', error);
throw error;
}
};
export const createOrUpdateTemplate = async (templateName, fields, userId, updated) => {
const data = {
template_name: templateName,
fields: fields,
user_id: userId,
};
try {
let response;
console.log(updated)
if (updated) {
// Update existing template
response = await axiosInstance.put(`${API_URL}/api/v1/ocrtemplate/templates/`, data);
} else {
// Create new template
response = await axiosInstance.post(`${API_URL}/api/v1/ocrtemplate/`, data);
}
return response.data;
} catch (error) {
console.error('Error creating/updating template:', error);
throw error;
}
};
export const deleteTemplate = async (templateName) => {
try {
const response = await axiosInstance.delete(`${API_URL}/api/v1/ocrtemplate/${templateName}`);
return response.data;
} catch (error) {
console.error('Error deleting template:', error);
throw error;
}
};
export const ocrProcess = async (formData) => {
try {
for (let [key, value] of formData.entries()) {
console.log(`${key}: ${value.name || value}`);
}
const response = await axiosInstance.post(`${API_URL}/api/v1/ocr/process`, formData);
// Check for HTTP errors
if (response.status !== 200) {
throw new Error(`Error: ${response.statusText}`);
}
const data = response.data; // axios automatically parses JSON
console.log("Success:", data);
return data; // Return the parsed data
} catch (error) {
console.error("Error:", error);
alert("There was an error processing the files.");
throw error; // Rethrow the error for further handling if needed
}
};
export const textToSpeech = async (text, languageCode, ssmlGender, name, pitch, speakingRate, volumeGainDb) => {
const data = {
text,
language_code: languageCode,
ssml_gender: ssmlGender,
name,
pitch,
speaking_rate: speakingRate,
volume_gain_db: volumeGainDb
};
// Get the token from your storage solution (e.g., localStorage)
const token = localStorage.getItem('token');
const headers = {
'xi-api-key': 'u2', // Replace with the actual API key for the user
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
};
try {
console.log(data)
// const response = await axios.post(`${API_URL}/api/v1/text-to-speech/`, data, {headers, responseType: 'blob'});
const response = await axios.post(`${API_URL}/api/v1/text-to-speech/generate`, data, {headers});
// Create a URL for the blob and return it
// const audioUrl = window.URL.createObjectURL(new Blob([response.data]));
const audioUrl = response.data.data.key;
console.log(audioUrl )
return audioUrl
} catch (error) {
if (error.response) {
// The request was made and the server responded with a status code
// that falls out of the range of 2xx
console.error('Error response data:', error.response.data);
console.error('Error response status:', error.response.status);
console.error('Error response headers:', error.response.headers);
} else if (error.request) {
// The request was made but no response was received
console.error('Error request data:', error.request);
} else {
// Something happened in setting up the request that triggered an Error
console.error('Error message:', error.message);
}
console.error('Error config:', error.config);
throw error;
}
};