Files
dougal-software/lib/www/server/queues/asaqc/despatch.js
D. Berge 5c190e5554 Add ASAQC queue processor.
This code implements the backend processing side
of the ASAQC queue, i.e., the bit that communicates
with the remote API.

Its expected use it to have it running at regular
intervals, e.g., via cron. The entry point is:

lib/www/server/queues/asaqc/index.js

That file is executable and can be run directly
from the shell or within a script. Read the comments
in that file for further instructions.
2021-10-04 02:21:00 +02:00

56 lines
1.3 KiB
JavaScript

const fs = require('fs');
const path = require('path');
const https = require('https');
const fetch = require('node-fetch');
const DOUGAL_ROOT = require('../../lib/config').DOUGAL_ROOT;
const cfg = require('../../lib/config').global.queues.asaqc.request;
/**
* Return a suitably configured httpsAgent with the client's TLS
* credentials if given.
*/
function httpsAgent () {
// References:
// https://github.com/node-fetch/node-fetch/issues/904
// https://nodejs.org/api/https.html#https_https_request_options_callback
if (!cfg.httpsAgent) {
return;
}
const options = {
key: fs.readFileSync(path.resolve(DOUGAL_ROOT, cfg.httpsAgent.key)),
cert: fs.readFileSync(path.resolve(DOUGAL_ROOT, cfg.httpsAgent.cert))
}
return https.Agent(options);
}
/**
* Send a payload to the ASAQC `upload-file-encoded` endpoint.
* https://api.equinor.com/docs/services/vessel-track/operations/FileUploadEncoded
*/
async function despatchPayload(payload) {
try {
const res = await fetch(cfg.url, {
...cfg.args,
body: JSON.stringify(payload),
agent: httpsAgent()
});
if (res) {
return await res.json();
} else {
console.error("NO RESPONSE FROM ASAQC ENDPOINT");
}
} catch (err) {
console.error(err);
return {error: err};
}
}
module.exports = despatchPayload;