const https = require('https');
function getPromise_token() {
return new Promise(( resolve, reject ) => {
if ( x_okapi_token == '' ) {
const data = JSON.stringify ({
username: 'diku_admin',
password: 'admin'
});
const options = {
hostname: 'folio-snapshot-okapi.dev.folio.org',
port: '443',
path: '/authn/login',
method: 'POST',
headers: {
'Content-type': 'application/json',
'Content-Length': data.length,
'x-okapi-tenant': 'diku',
'x-okapi-token': ''
}
}
const req = https.request(options, res => {
res.on('data', d => {
process.stdout.write(d);
})
x_okapi_token = res.headers['x-okapi-token'];
resolve(x_okapi_token);
})
req.on('error', error => {
reject(error);
})
req.write(data);
req.end();
} else {
resolve(x_okapi_token);
}
});
}
async function get_token() {
try {
let http_promise = getPromise_token();
let x_okapi_token = await http_promise;
return x_okapi_token;
}
catch(error) {
console.log(error);
}
} |