File name
Commit message
Commit date
File name
Commit message
Commit date
File name
Commit message
Commit date
import axios from 'axios';
import AsyncStorage from '@react-native-async-storage/async-storage';
// API 인스턴스 생성
const apiInstance = axios.create({
//baseURL: 'http://takensofttesting.iptime.org:27461',
baseURL: 'http://takensoftai.iptime.org:27461',
headers: {
'Content-Type': 'application/json',
},
});
// 토큰 가져오기 함수
const getAccessToken = async (navigation) => {
try {
const tokenString = await AsyncStorage.getItem('token');
if (tokenString) {
const tokenObject = JSON.parse(tokenString);
const accessToken = tokenObject?.accessToken;
if (!accessToken) {
await AsyncStorage.removeItem('token');
navigation.navigate('Login');
return null;
}
return accessToken;
} else {
console.log("토큰이 스토리지에 없습니다.");
navigation.navigate('Login');
return null;
}
} catch (error) {
console.error('토큰 처리 오류:', error.message);
navigation.navigate('Login');
return null;
}
};
const ApiUtils = {
register: async (userData) => {
try {
const response = await apiInstance.post('/auth/register', userData);
return response.data;
} catch (error) {
console.error('Register error:', error.response || error);
throw error.response?.data || error;
}
},
login: async (credentials) => {
try {
const response = await apiInstance.post('/auth/login', credentials);
return response.data;
} catch (error) {
console.error('Login error:', error.response || error);
throw error.response?.data || error;
}
},
saveTripLog: async (data, navigation) => {
try {
// getAccessToken 함수 호출
const accessToken = await getAccessToken(navigation);
// accessToken이 없으면 중단
if (!accessToken) return;
// 데이터 전송
const response = await apiInstance.post('/action/gps_update', data, {
headers: {
'Content-Type': 'application/json',
Authorization: accessToken, // 토큰을 헤더에 포함
},
});
return response.data;
} catch (error) {
console.error('save trip log error:', error.response?.status, error.response?.data || error.message);
throw error.response?.data || error;
}
},
saveResult: async (result, navigation) => {
try {
// getAccessToken 함수 호출
const accessToken = await getAccessToken(navigation);
// accessToken이 없으면 중단
if (!accessToken) return;
const response = await apiInstance.post('/action/trip_and_score_update', result, {
headers: {
'Content-Type': 'application/json',
Authorization: accessToken,
},
});
return response.data;
} catch (error) {
console.error('result save error:', error.response?.status, error.response?.data || error.message);
throw error.response?.data || error;
}
},
getHistory: async (result, navigation) => {
try {
// getAccessToken 함수 호출
const accessToken = await getAccessToken(navigation);
// accessToken이 없으면 중단
if (!accessToken) return;
const response = await apiInstance.post('/action/get_history', result, {
headers: {
'Content-Type': 'application/json',
Authorization: accessToken,
},
});
console.log(response.data.data)
return response.data;
} catch (error) {
console.error('getHistory error:', error.response?.status, error.response?.data || error.message);
throw error.response?.data || error;
}
},
// 외부 API
uploadPhoto: async (uri) => {
const formData = new FormData();
formData.append('file', {
uri,
type: 'image/jpeg',
name: 'photo.jpg',
});
try {
const response = await apiInstance.post('http://121.181.165.130:8077/predict', formData, {
headers: {
'Content-Type': 'multipart/form-data',
},
});
return response.data;
} catch (error) {
console.error('Upload photo error:', error.response || error);
throw error.response?.data || error;
}
},
sendTripLog: async (data) => {
console.log("보낸값",data)
try {
const response = await axios.post('http://121.181.165.130:8077/calculate_safety_score', data, {
headers: {
'Content-Type': 'application/json',
},
});
console.log("결과값",response.data)
return response.data;
} catch (error) {
console.error('Send trip log error:', error.response?.status, error.response?.data || error.message);
throw error.response?.data || error;
}
},
};
export default ApiUtils;