Created
March 21, 2024 21:45
-
-
Save psenger/4d203657b66d450ccaa86407ff71c1a8 to your computer and use it in GitHub Desktop.
Compare Travel Insurance
This file contains 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 http = require('http') | |
const readline = require('readline') | |
const url = require('url') | |
function httpRequest(options, data) { | |
return new Promise((resolve, reject) => { | |
const parsedUrl = url.parse(options.hostname) | |
const protocol = parsedUrl.protocol | |
const request = (protocol && protocol.startsWith('https')) ? https.request : http.request | |
options.hostname = parsedUrl.hostname // Update hostname value to exclude 'http://' or 'https://' | |
options.port = parsedUrl.port ? parsedUrl.port : options.port | |
console.log(`Host = "${options.hostname}"`) | |
console.log(`Port = "${options.port}"`) | |
const req = request(options, res => { | |
let result = '' | |
res.on('data', d => { | |
result += d | |
}) | |
res.on('end', () => { | |
let body = undefined | |
if (result) { | |
try { | |
body = JSON.parse(result) | |
} catch (e) { | |
body = result | |
} | |
} | |
resolve({ | |
headers: res.headers, | |
statusCode: res.statusCode, | |
body, | |
}) | |
}) | |
}) | |
req.on('error', err => { | |
reject(err) | |
}) | |
if (data) { | |
req.write(data) | |
} | |
req.end() | |
}) | |
} | |
const askQuestion = (rl,query) => new Promise((resolve) => rl.question(query, resolve)) | |
const promptUserForHostname = async ( passthrough ) => { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
while (true) { // Infinite loop, will exit via return statements | |
console.log('Please select a Host:\n1. https://ctiau.engagecfortstage.com\n2. http://localhost:8080') | |
const answer = await askQuestion(rl,'Enter option number: ') | |
switch (answer.trim()) { | |
case '1': | |
console.log('Option 1 selected') | |
rl.close(); // Close before returning | |
return { | |
...passthrough, | |
hostname: 'https://ctiau.engagecfortstage.com' | |
} | |
case '2': | |
console.log('Option 2 selected') | |
rl.close(); // Close before returning | |
return { | |
...passthrough, | |
hostname: 'http://localhost:8080' | |
} | |
default: | |
console.log('Invalid option selected. Please try again.') | |
break // Loop will continue | |
} | |
} | |
} | |
const promptUserForToken = async ( passthrough ) => { | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}) | |
while (true) { // Infinite loop, will exit via return statements | |
const answer = await askQuestion(rl,'Enter the token: ') | |
if ( answer.trim() !== '' ) { | |
return { | |
...passthrough, | |
token: answer.trim(), | |
} | |
} | |
console.log('Invalid. Please try again.') | |
} | |
} | |
const parseURL = (hostname) => { | |
return url.parse(hostname) | |
} | |
module.exports = { | |
httpRequest, | |
promptUserForHostname, | |
parseURL, | |
promptUserForToken | |
} |
This file contains 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
#!/usr/bin/env node | |
const { httpRequest, promptUserForHostname, parseURL } = require('./common-utils') | |
/** | |
* THis code doesnt work for two reasons: | |
* 1.) we are not supporting polling on the server | |
* 2.) the href links are mapped to the wrong server. | |
* | |
* @param _hostname | |
* @param method | |
* @param maxRetries | |
* @param baseDelay | |
* @returns {Promise<unknown>} | |
*/ | |
const pollTheServer = async (_hostname, method, maxRetries = 10, baseDelay = 500) => { | |
const parsedUrl = parseURL(_hostname ) | |
const options = { | |
hostname: `${parsedUrl.protocol}//${parsedUrl.hostname}${ parsedUrl.port ? `:${parsedUrl.port}` : '' }`, | |
path: parsedUrl.path, | |
method, | |
headers: { | |
'Content-Type': 'application/json', | |
} | |
} | |
for (let i = 0; i < maxRetries; i++) { | |
const results = await httpRequest(options) | |
const {statusCode} = results | |
switch (statusCode) { | |
case 200: | |
return results // Request successful, exiting function | |
case 404: | |
case 410: | |
throw new Error(`Request failed with status code: ${statusCode}`) // Throw error on fail codes | |
case 202: | |
console.log('appears the server is still working.') | |
// Delay then continue the loop | |
const delay = baseDelay * Math.pow(2, i) // Exponential backoff | |
await new Promise(resolve => setTimeout(resolve, delay)) | |
break | |
default: | |
throw new Error(`Unexpected status code: ${statusCode}`) | |
} | |
} | |
throw new Error(`Request failed after max retries ${maxRetries}`) | |
} | |
const createPolicyQuote = async (hostname, parameter) => { | |
const data = JSON.stringify(parameter) | |
const options = { | |
hostname, | |
path: '/api/v1/policyQuote', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length | |
} | |
} | |
return httpRequest(options, data) | |
} | |
const getPolicyQuote = async (hostname, path, method) => { | |
const options = { | |
hostname, | |
path, | |
method, | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
} | |
return httpRequest(options) | |
} | |
function formatDate(date) { | |
let day = ("0" + date.getDate()).slice(-2); // Add leading zero and slice last two characters | |
let month = ("0" + (date.getMonth() + 1)).slice(-2); // Add leading zero, months are 0-indexed in JavaScript | |
let year = date.getFullYear(); | |
return day + "/" + month + "/" + year; // Return formatted string | |
} | |
const run = async ( {hostname} ) => { | |
let today = new Date() | |
let inTenDays = new Date(today.getTime()); | |
let inTwentyDays = new Date(today.getTime()); | |
inTenDays.setDate(today.getDate() + 10) | |
inTwentyDays.setDate(today.getDate() + 20) | |
const payload = { | |
destinations: [ | |
'BAL' | |
], | |
startDate: formatDate(inTenDays), | |
endDate: formatDate(inTwentyDays), | |
quoteAddons: [], | |
tripType: 'SINGLE_TRIP', | |
travellersAges: '34,23,12', | |
numberOfChildren: 1 | |
} | |
console.log('-'.repeat(100)) | |
console.log('Requesting a Policy Quote with') | |
console.log(JSON.stringify(payload, null, 4)) | |
console.log('-'.repeat(100)) | |
const {body} = await createPolicyQuote(hostname, payload) | |
console.log('-'.repeat(100)) | |
console.log('Response from creating a Policy Quote Request') | |
console.log(JSON.stringify(body, null, 4)) | |
console.log('-'.repeat(100)) | |
// turned off because we dont support this yet. | |
// console.log('-'.repeat(100)) | |
// console.log('Polling the server') | |
// console.log('-'.repeat(100)) | |
// await pollTheServer(body._links.status.href, body._links.status.type) | |
// console.log('Done polling') | |
console.log('-'.repeat(100)) | |
console.log('Fetching the policy') | |
// const policyQuote = await getPolicyQuote(body._links.self.href, body._links.self.type) | |
const policyQuote = await getPolicyQuote(hostname,`${hostname}/api/v1/policyQuote/${body.token}`, 'GET') | |
console.log(JSON.stringify(policyQuote, null, 4)) | |
console.log('-'.repeat(100)) | |
return 'done' | |
} | |
promptUserForHostname() | |
.then(run) | |
.then((...args) => { | |
console.log(...args) | |
process.exit(0) | |
}) | |
.catch((err) => { | |
console.log(err) | |
process.exit(1) | |
}) |
This file contains 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
#!/usr/bin/env node | |
const { httpRequest, promptUserForHostname, parseURL, promptUserForToken} = require('./common-utils') | |
/** | |
* THis code doesnt work for two reasons: | |
* 1.) we are not supporting polling on the server | |
* 2.) the href links are mapped to the wrong server. | |
* | |
* @param _hostname | |
* @param method | |
* @param maxRetries | |
* @param baseDelay | |
* @returns {Promise<unknown>} | |
*/ | |
const pollTheServer = async (_hostname, method, maxRetries = 10, baseDelay = 500) => { | |
const parsedUrl = parseURL(_hostname) | |
const options = { | |
hostname: `${parsedUrl.protocol}//${parsedUrl.hostname}${ parsedUrl.port ? `:${parsedUrl.port}` : '' }`, | |
path: parsedUrl.path, | |
method, | |
headers: { | |
'Content-Type': 'application/json', | |
} | |
} | |
for (let i = 0; i < maxRetries; i++) { | |
const results = await httpRequest(options); | |
const {statusCode} = results; | |
switch (statusCode) { | |
case 200: | |
return results; // Request successful, exiting function | |
case 404: | |
case 410: | |
throw new Error(`Request failed with status code: ${statusCode}`); // Throw error on fail codes | |
case 202: | |
console.log('appears the server is still working.') | |
// Delay then continue the loop | |
const delay = baseDelay * Math.pow(2, i); // Exponential backoff | |
await new Promise(resolve => setTimeout(resolve, delay)); | |
break; | |
default: | |
throw new Error(`Unexpected status code: ${statusCode}`); | |
} | |
} | |
throw new Error(`Request failed after max retries ${maxRetries}`); | |
} | |
const createPolicyQuote = async (parameter) => { | |
const data = JSON.stringify(parameter) | |
const options = { | |
hostname, | |
path: '/api/v1/policyQuote', | |
method: 'POST', | |
headers: { | |
'Content-Type': 'application/json', | |
'Content-Length': data.length | |
} | |
} | |
return httpRequest(options, data) | |
} | |
const getPolicyQuote = async ({hostname, path}) => { | |
const options = { | |
hostname, | |
path, | |
method: 'GET', | |
headers: { | |
'Content-Type': 'application/json' | |
} | |
} | |
return httpRequest(options) | |
} | |
const run = async ( {hostname, token} ) => { | |
console.log('-'.repeat(100)) | |
console.log('Fetching the policy', hostname, token) | |
console.log('-'.repeat(100)) | |
// const policyQuote = await getPolicyQuote(body._links.self.href, body._links.self.type) | |
const policyQuote = await getPolicyQuote( { | |
hostname, | |
path: `/api/v1/policyQuote/${token}` | |
}) | |
console.log(JSON.stringify(policyQuote, null, 4)) | |
return 'done' | |
} | |
promptUserForHostname() | |
.then( promptUserForToken ) | |
.then(run) | |
.then((...args) => { | |
console.log(...args) | |
process.exit(0) | |
}) | |
.catch((err) => { | |
console.log(err) | |
process.exit(1) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment