Created
August 12, 2024 18:51
-
-
Save matthewoestreich/1a4846e90cf0bf2f003b1ef540e6f071 to your computer and use it in GitHub Desktop.
Foo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const https = require("https"); | |
const querystring = require("querystring"); | |
const fs = require("fs"); | |
const path = require("path"); | |
(async () => { | |
// Root of json file should be an array.. | |
// [] | |
const LOG_FILE_PATH = path.resolve(__dirname, "./log.json"); | |
const endpoint = "http://localhost:3000/post"; | |
const num = "foo-num"; | |
const TOKEN = "foo-token"; | |
const APP_ID = "foo-app-id"; | |
const sessionID = "foo-sessionID"; | |
const requestBody = { | |
rcx_session: sessionID, | |
command: JSON.stringify({ | |
call: { | |
numbers: num, | |
type: 1, | |
}, | |
}), | |
}; | |
const httpsAgent = new https.Agent({ | |
rejectUnauthorized: true, | |
}); | |
const finalBody = querystring.stringify({ | |
token: TOKEN, | |
app: APP_ID, | |
...requestBody, | |
}); | |
const result = await fetch(endpoint, { | |
method: "POST", | |
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | |
body: finalBody, | |
agent: httpsAgent, | |
}); | |
/** | |
* LOGGING | |
* | |
* Idk what all you want to log.... | |
*/ | |
// | |
// Collect data to log (you may need to change some of this....) | |
// | |
// Data we will log to LOG_FILE_PATH | |
const dataToLog = { | |
logTimeUTC: new Date().toUTCString(), | |
request: { | |
method: "POST", | |
headers: { "Content-Type": "application/x-www-form-urlencoded" }, | |
token: TOKEN, | |
app: APP_ID, | |
requestBody, | |
agent: httpsAgent, | |
}, | |
response: { | |
status: result.status, | |
headers: {}, | |
body: "", | |
url: result.url, | |
type: result.type, | |
} | |
}; | |
// Get headers to log (idk) | |
for (const [key, val] of result.headers.entries()) { | |
dataToLog.response.headers[key] = val; | |
} | |
// Get body to log (idk) | |
dataToLog.response.body = await result.text(); // <-- like idk what your response looks like.... | |
// | |
// Misc logging/file related funcs. | |
// | |
// Create a file if it doesn't exist. | |
async function createLogFileIfNotExists(path) { | |
const logFileExists = await fs.existsSync(path); | |
if (!logFileExists) { | |
await fs.writeFileSync(path, JSON.stringify([])); | |
} | |
} | |
// Get existing file data. | |
async function getExistingLogDataAsJSON(path) { | |
let existingLogs = await fs.readFileSync(path, "utf-8"); | |
if (!existingLogs) { | |
// File exists just no data | |
existingLogs = "[]"; // make it a string so when we call JSON.parse below it ddoesn't error. | |
} | |
return JSON.parse(existingLogs); | |
} | |
// Update file data and write to disk. | |
async function addDataToLogsAndWriteToDisk(path, existingLogs, dataToLog) { | |
existingLogs.push(dataToLog); | |
const jsonString = JSON.stringify(existingLogs, null, 2); // `null, 2` params aren't needed, they just format the file so it's easier to read. | |
await fs.writeFileSync(path, jsonString); | |
} | |
// | |
// | |
// Actually read/write to log file.. | |
// | |
// | |
try { | |
await createLogFileIfNotExists(LOG_FILE_PATH); | |
const logs = await getExistingLogDataAsJSON(LOG_FILE_PATH); | |
await addDataToLogsAndWriteToDisk(LOG_FILE_PATH, logs, dataToLog); | |
} catch (err) { | |
console.log("Error reading/writing log file!", { error: err, request: dataToLog }); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment