NewLogging #1

Open
garrett wants to merge 2 commits from NewLogging into main
7 changed files with 73 additions and 8 deletions

View File

@@ -1,10 +1,18 @@
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, `dataController.itemLookup()`);
const productCode = req.query.productCode;
const oauthToken = await fetchEbayReadToken();
console.log(productCode);
let oauthToken;
try{
oauthToken = await fetchEbayReadToken();
}catch(e){
smartLogging(LoggingLevel.Error, `Error Getting eBay Token: ${e}`);
}
smartLogging(LoggingLevel.Logging, `Product Code: ${productCode}`);
try {
const response = await fetch(
`https://api.ebay.com/buy/browse/v1/item_summary/search?gtin=${productCode}`,
@@ -28,7 +36,7 @@ export const itemLookup = async (req, res) => {
res.status(200).send(price);
} catch (error) {
console.error("Error fetching data from eBay Browse API:", error);
smartLogging(LoggingLevel.Error, `Error fetching data from eBay Browse API: ${error}`);
res.status(500).send("Internal Server Error");
}
};

View File

@@ -2,12 +2,14 @@
import buildAddFixedPriceItemRequestXML from "../utils/buildAddFixedPriceItemRequestXML.js";
import fetch from "node-fetch";
import fetchEbayUserToken from "../utils/fetchEbayUserToken.js"; // Adjust the import path according to your project structure
import { LoggingLevel, smartLogging } from "../utils/helper.js";
export const addItem = async (req, res) => {
smartLogging(LoggingLevel.AppTrace, `inventoryController.addItem`);
const itemDetails = req.body; // Assuming item details are sent in the request body
itemDetails.userToken =
"v^1.1#i^1#r^0#I^3#f^0#p^3#t^H4sIAAAAAAAAAOVZf2wbVx2P86Nd1HWAViWsLZrntQMtPd+7O9vnO2ozJ3bidPnhxnaXBKHo3d075zX3q/fu4ngTKI22wIS6CaqhijIUxoSEJjQ2YCCtUHVD2h/AtE6CakKiILSu4w8KRQwmYOLO+VE3U3/EDqol/M/53n1/fb4/770D81s671/MLv5je2Br69I8mG8NBJhtoHNLR88dba07O1pADUFgaX7PfPtC28X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7DrX/7evX3h2T/bvb54ipx54IT+9T7tA/1u5RN/32Kcun7l47s/7WvJvSo+3fX9x29NffeAzrx7ofOT8l86c3Ne9HMv/AqyXZcRHHgAA";
console.log(itemDetails);
smartLogging(LoggingLevel.Logging, `${itemDetails}`);
const xmlRequest = buildAddFixedPriceItemRequestXML(itemDetails);
try {
@@ -35,7 +37,7 @@ export const addItem = async (req, res) => {
res.json({ success: true, data: responseData }); // Send back a success response
} catch (error) {
console.error("Error adding item to eBay:", error);
smartLogging(LoggingLevel.Error,`Error adding item to eBay: ${error});
res.status(500).json({
success: false,
message: "Failed to add item to eBay",

View File

@@ -4,6 +4,7 @@ import cors from "cors";
import dotenv from "dotenv";
import dataRoutes from "./routes/dataRoutes.js";
import inventoryRoutes from "./routes/inventoryRoutes.js";
import { LoggingLevel, setLevel, smartLogging } from "./utils/helper.js";
dotenv.config();
@@ -16,5 +17,7 @@ app.use(express.json());
app.use("/api/data", dataRoutes);
app.use("/api/inventory", inventoryRoutes);
//Set Logging Level
setLevel(LoggingLevel.AppTrace); //This should get passed in from the Docker-Compose.
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => console.log(`Server running on port ${PORT}`));
app.listen(PORT, () => smartLogging(LoggingLevel.Logging, `Server running on port ${PORT}`));

View File

@@ -1,7 +1,9 @@
// Need to figoure out expiration and make sure to cycle this appropriately to avoid unnecessary calls
import fetch from "node-fetch";
import { LoggingLevel, smartLogging } from "./helper.js";
const fetchEbayReadToken = async () => {
smartLogging(LoggingLevel.AppTrace, `fetchEbayReadToken`);
const ebayClientId = process.env.EBAY_CLIENT_ID;
const ebayClientSecret = process.env.EBAY_CLIENT_SECRET;
const credentials = Buffer.from(
@@ -9,6 +11,7 @@ const fetchEbayReadToken = async () => {
).toString("base64");
try {
smartLogging(LoggingLevel.Debug, `Fetching Token from eBay`);
const response = await fetch(
"https://api.ebay.com/identity/v1/oauth2/token",
{
@@ -31,7 +34,7 @@ const fetchEbayReadToken = async () => {
const data = await response.json();
return data.access_token;
} catch (error) {
console.error("Error fetching eBay OAuth token:", error);
smartLogging(LoggingLevel.Error, `Error fetching eBay OAuth token: ${error}`);
throw error; // Throw the error to be handled by the caller
}
};

View File

@@ -1,6 +1,8 @@
import fetch from "node-fetch";
import { LoggingLevel, smartLogging } from "./helper.js";
const fetchEbayUserToken = async (authorizationCode) => {
smartLogging(LoggingLevel.AppTrace, `fetchEbayUserToken`);
const clientId = process.env.EBAY_CLIENT_ID;
const clientSecret = process.env.EBAY_CLIENT_SECRET;
const redirectUri = process.env.EBAY_REDIRECT_URI; // Make sure this matches the URI registered with eBay
@@ -33,7 +35,7 @@ const fetchEbayUserToken = async (authorizationCode) => {
const data = await response.json();
return data.access_token; // This is the User access token
} catch (error) {
console.error("Error fetching eBay user token:", error);
smartLogging(LoggingLevel.Error,`Error fetching eBay user token: ${JSON.stringify(error)}`);
throw error;
}
};

46
api/utils/helper.js Normal file
View File

@@ -0,0 +1,46 @@
export let setlvl = undefined;
export function setLevel(level){
console.log(`Setting logging level to: ${level}`);
setlvl = level;
}
export function smartLogging(level, msg){
if(setlvl == undefined){ //If no level is set treat it as console.log()
console.log(msg);
}else if(level <= setlvl){
switch (level) {
case LoggingLevel.Error:
console.error(msg);
break;
case LoggingLevel.Warning:
console.warn(msg);
break;
case LoggingLevel.Logging:
console.log(msg);
break;
case LoggingLevel.Debug:
console.debug(msg);
case LoggingLevel.AppTrace:
console.debug(msg);
break;
case LoggingLevel.Trace:
console.trace(msg);
break;
default:
console.log(msg);
}
}
}
export const LoggingLevel = {
Error: "10",
Warning: "9",
Logging: "8",
Debug: "7",
AppTrace: "6",
Trace: "5"
}

View File

@@ -2,6 +2,7 @@ version: "3.8"
services:
api:
build: ./api
container_name: eBayAPI
ports:
- "3000:3000"
volumes: