import { addQueryParams, errorHandler, fetchRespHandler, getAuthHeaderOptions } from "../utils/http"; const API_ENDPOINT = process.env.REACT_APP_OTA_SERVICE_URL; const createDeployUpdatesClosure = (suffix) => { return async (data, token) => fetch(`${API_ENDPOINT}/${suffix}`, { method: "POST", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), body: JSON.stringify(data), }) .then(fetchRespHandler) .catch(errorHandler); }; const updatesAPI = { createFleetUpdates: createDeployUpdatesClosure("fleetupdate"), createCarUpdates: createDeployUpdatesClosure("carupdate"), getCarUpdateLog: async (query, token) => { const u = addQueryParams(`${API_ENDPOINT}/carupdateslog`, query); return fetch(u, { method: "GET", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), }) .then(fetchRespHandler) .catch(errorHandler); }, getCarUpdateProgress: async (carupdateids, token) => { const u = `${API_ENDPOINT}/carupdatesstatuses?carupdateids=${carupdateids}`; return fetch(u, { method: "GET", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), }) .then(fetchRespHandler) .catch(errorHandler); }, getCarUpdates: async (search, token) => { const u = addQueryParams(`${API_ENDPOINT}/carupdates`, search); return fetch(u, { method: "GET", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), }) .then(fetchRespHandler) .catch(errorHandler); }, getVINUpdates: async (vin, token) => { const u = addQueryParams(`${API_ENDPOINT}/carupdates`, { vin }); return fetch(u, { method: "GET", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), }) .then(fetchRespHandler) .catch(errorHandler); }, cancelCarUpdate: async (id, token) => { return fetch(`${API_ENDPOINT}/carupdate/${id}/cancel`, { method: "POST", headers: Object.assign( { "Content-Type": "application/json" }, getAuthHeaderOptions(token) ), }) .then(fetchRespHandler) .catch(errorHandler); }, }; export default updatesAPI;