77 lines
1.3 KiB
JavaScript
77 lines
1.3 KiB
JavaScript
|
'use strict';
|
||
|
|
||
|
const bhttp = require('bhttp');
|
||
|
const taskQueue = require('promise-task-queue');
|
||
|
|
||
|
const logger = require('../logger')(__filename);
|
||
|
|
||
|
const queue = taskQueue();
|
||
|
|
||
|
queue.on('concurrencyReached:httpGet', () => {
|
||
|
logger.silly('Queueing GET requests');
|
||
|
});
|
||
|
|
||
|
queue.on('concurrencyReached:httpPost', () => {
|
||
|
logger.silly('Queueing POST requests');
|
||
|
});
|
||
|
|
||
|
queue.define('httpGet', async ({
|
||
|
url,
|
||
|
timeout = 30000,
|
||
|
options = {},
|
||
|
}) => {
|
||
|
logger.silly(`GET ${url}`);
|
||
|
|
||
|
const res = await bhttp.get(url, {
|
||
|
responseTimeout: timeout,
|
||
|
...options,
|
||
|
});
|
||
|
|
||
|
res.code = res.statusCode;
|
||
|
|
||
|
return res;
|
||
|
}, {
|
||
|
concurrency: 20,
|
||
|
});
|
||
|
|
||
|
queue.define('httpPost', async ({
|
||
|
url,
|
||
|
body,
|
||
|
timeout = 30000,
|
||
|
options = {},
|
||
|
}) => {
|
||
|
logger.silly(`POST ${url} with ${body}`);
|
||
|
|
||
|
const res = await bhttp.post(url, body, {
|
||
|
responseTimeout: timeout,
|
||
|
...options,
|
||
|
});
|
||
|
|
||
|
res.code = res.statusCode;
|
||
|
|
||
|
return res;
|
||
|
}, {
|
||
|
concurrency: 20,
|
||
|
});
|
||
|
|
||
|
async function get(url, options) {
|
||
|
return queue.push('httpGet', {
|
||
|
method: 'get',
|
||
|
url,
|
||
|
options,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
async function post(url, body, options) {
|
||
|
return queue.push('httpPost', {
|
||
|
url,
|
||
|
body,
|
||
|
options,
|
||
|
});
|
||
|
}
|
||
|
|
||
|
module.exports = {
|
||
|
get,
|
||
|
post,
|
||
|
};
|