pdfer-fetch-imacros
Version:
fetch details about a pdf document using the sha1 hash value
58 lines (55 loc) • 1.74 kB
JavaScript
/**
* Get a list of existing bills from the DocParse api server
*/
function getURL(data) {
var config = data.config;
var hash = data.hash;
var email = config.pdfer.email
var password = config.pdfer.password
email = encodeURIComponent(email);
password = encodeURIComponent(password);
var url = 'https://'+ email + ':' + password
+ '@'+config.pdfer.host
+ '/api/fetch/' + hash;
return url;
}
function parseResponse(request, cb) {
var resData;
var body = request.response;
if (body === 'Unauthorized') {
iimDisplay('fetch failed, "Unauthorized"');
return cb('error fetching pdfer data, body: "Unauthorized"');
}
try {
resData = JSON.parse(body);
}
catch(err) {
iimDisplay('error parsing pdfer fetch repsonse: ' + JSON.stringify(err, null, ' '));
return cb({
message: 'error parsing pdfer fetch response',
body: body,
error: err
});
}
var statusCode = request.status;
if (statusCode !== 200) {
iimDisplay('fetch failed, bad status code: ' + statusCode);
return cb('error fetching data for hash, bad status code: ' + statusCode);
}
if (!resData.hasOwnProperty('textPages')) {
iimDisplay('fetch reply missing "textPages" field');
return cb('fetch reply missing "textPages" property');
}
cb(null, resData);
}
module.exports = function(data, cb) {
var url = getURL(data);
var request = new XMLHttpRequest();
var async = false;
request.open('GET', url, async);
request.send();
// because of "false" above, will block until the request is done and status
// is available. Not recommended, however it works for simple cases.
iimDisplay('parsing fetch response: ' + request.response);
parseResponse(request, cb);
};