structured API with controllersroutes
This commit is contained in:
36
api/controllers/itemLookup.js
Normal file
36
api/controllers/itemLookup.js
Normal file
@@ -0,0 +1,36 @@
|
||||
// controllers/itemLookup.js
|
||||
import fetch from "node-fetch";
|
||||
import fetchEbayToken from "../utils/fetchEbayToken.js"; // Adjust the import path according to your project structure
|
||||
|
||||
const itemLookup = async (req, res) => {
|
||||
const productCode = req.body.productCode;
|
||||
const oauthToken = await fetchEbayToken();
|
||||
console.log(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();
|
||||
data.itemSummaries.forEach((item) => {
|
||||
console.log(item.price, item.condition);
|
||||
});
|
||||
|
||||
res.status(200).send("Data fetched successfully");
|
||||
} catch (error) {
|
||||
console.error("Error fetching data from eBay Browse API:", error);
|
||||
res.status(500).send("Internal Server Error");
|
||||
}
|
||||
};
|
||||
|
||||
export default itemLookup;
|
||||
Reference in New Issue
Block a user