Files
ebay_api/api/controllers/dataController.js

43 lines
1.4 KiB
JavaScript

import fetch from "node-fetch";
import fetchEbayReadToken from "../utils/fetchEbayReadToken.js"; // Adjust the import path according to your project structure
import { LoggingLevel, smartLogging } from "../utils/helper.js";
export const itemLookup = async (req, res) => {
smartLogging(LoggingLevel.Debug, ``)
const productCode = req.query.productCode;
let oauthToken;
try{
oauthToken = await fetchEbayReadToken();
}catch(e){
smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`);
}
// console.log(productCode);
smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`);
try {
const response = await fetch(
`https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`,
{
method: "GET",
headers: {
Authorization: `Bearer ${oauthToken}`,
"Content-Type": "application/json",
},
}
);
console.log(response);
if (!response.ok)
throw new Error("Failed to fetch data from eBay Browse API");
const data = await response.json();
const price = [];
data.itemSummaries.forEach((item) => {
price.push(item.price, item.condition);
});
res.status(200).send(price);
} catch (error) {
smartLogging(LoggingLevel.Error, `Error fetching data from eBay Browse API: ${error}`);
res.status(500).send("Internal Server Error");
}
};