Sindbad~EG File Manager
const fetch = require("node-fetch");
async function getBearerToken() {
const response = await fetch(`${process.env.CAR_API_BASE_URL}/auth/login`, {
method: "POST",
headers: {
accept: "text/plain",
"Content-Type": "application/json",
},
body: JSON.stringify({
api_token: process.env.CAR_API_TOKEN,
api_secret: process.env.CAR_API_SECRET,
}),
});
return response.text();
}
const getAllYears = async (req, res) => {
const url = `${process.env.CAR_API_BASE_URL}/years`;
const options = {
method: "GET",
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const yearsList = await response.json();
return res.json({ status: 200, message: "success", data: yearsList });
} catch (error) {
console.error(error);
}
};
const getMakesByYear = async (req, res) => {
const bearer_token = await getBearerToken();
const { year } = req.body;
const url = `${process.env.CAR_API_BASE_URL}/makes?year=${year}`;
const options = {
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${bearer_token}`,
},
};
try {
const response = await fetch(url, options);
if (!response.ok) {
console.log(response);
return response.json({
status: 401,
message: "Something went wrong...",
response,
});
}
const body = await response.json();
// return res.json(body.data);
const filteredMakes = body.data.map((item) => item.name);
return res.json({ status: 200, message: "success", data: filteredMakes });
} catch (error) {
console.error(error);
}
};
const getModelsByMake = async (req, res) => {
const bearer_token = await getBearerToken();
const { year, make } = req.body;
const url = `${process.env.CAR_API_BASE_URL}/models?make=${make}&year=${year}`;
const options = {
method: "GET",
headers: {
Accept: "application/json",
Authorization: `Bearer ${bearer_token}`,
},
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const body = await response.json();
const filteredModels = body.data.map((item) => item.name);
return res.json({
status: 200,
message: "success",
data: filteredModels,
});
} catch (error) {
console.error(error);
}
};
const vinDecoder = async (req, res) => {
const bearer_token = await getBearerToken();
const { vin } = req.body;
const url = `${process.env.CAR_API_BASE_URL}/vin/${vin}`;
const options = {
method: "GET",
headers: {
Accept: "text/plain",
"Content-Type": "application/json",
Authorization: `Bearer ${bearer_token}`,
},
};
try {
const response = await fetch(url, options);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const body = await response.json();
return res.json({
status: 200,
message: "success",
data: body,
});
} catch (error) {
console.error(error);
}
};
module.exports = {
getMakesByYear,
getModelsByMake,
getAllYears,
vinDecoder,
};
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists