mirror of
https://github.com/TryGhost/Ghost.git
synced 2025-02-10 23:36:14 -05:00
Use request lib in webhooks api (#9336)
refs #9178 - add retries and timeout
This commit is contained in:
parent
941379ddba
commit
2956c2c247
1 changed files with 12 additions and 16 deletions
|
@ -3,40 +3,37 @@
|
||||||
// also known as "REST Hooks", see http://resthooks.org
|
// also known as "REST Hooks", see http://resthooks.org
|
||||||
var Promise = require('bluebird'),
|
var Promise = require('bluebird'),
|
||||||
_ = require('lodash'),
|
_ = require('lodash'),
|
||||||
https = require('https'),
|
|
||||||
url = require('url'),
|
|
||||||
pipeline = require('../lib/promise/pipeline'),
|
pipeline = require('../lib/promise/pipeline'),
|
||||||
localUtils = require('./utils'),
|
localUtils = require('./utils'),
|
||||||
models = require('../models'),
|
models = require('../models'),
|
||||||
common = require('../lib/common'),
|
common = require('../lib/common'),
|
||||||
|
request = require('../lib/request'),
|
||||||
docName = 'webhooks',
|
docName = 'webhooks',
|
||||||
webhooks;
|
webhooks;
|
||||||
|
|
||||||
// TODO: Use the request util. Do we want retries here?
|
|
||||||
function makeRequest(webhook, payload, options) {
|
function makeRequest(webhook, payload, options) {
|
||||||
var event = webhook.get('event'),
|
var event = webhook.get('event'),
|
||||||
targetUrl = webhook.get('target_url'),
|
targetUrl = webhook.get('target_url'),
|
||||||
webhookId = webhook.get('id'),
|
webhookId = webhook.get('id'),
|
||||||
reqOptions, reqPayload, req;
|
|
||||||
|
|
||||||
reqOptions = url.parse(targetUrl);
|
|
||||||
reqOptions.method = 'POST';
|
|
||||||
reqOptions.headers = {'Content-Type': 'application/json'};
|
|
||||||
|
|
||||||
reqPayload = JSON.stringify(payload);
|
reqPayload = JSON.stringify(payload);
|
||||||
|
|
||||||
common.logging.info('webhook.trigger', event, targetUrl);
|
common.logging.info('webhook.trigger', event, targetUrl);
|
||||||
req = https.request(reqOptions);
|
|
||||||
|
|
||||||
req.write(reqPayload);
|
request(targetUrl, {
|
||||||
req.on('error', function (err) {
|
body: reqPayload,
|
||||||
|
headers: {
|
||||||
|
'Content-Length': Buffer.byteLength(reqPayload),
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
},
|
||||||
|
timeout: 2 * 1000,
|
||||||
|
retries: 5
|
||||||
|
}).catch(function (err) {
|
||||||
// when a webhook responds with a 410 Gone response we should remove the hook
|
// when a webhook responds with a 410 Gone response we should remove the hook
|
||||||
if (err.status === 410) {
|
if (err.statusCode === 410) {
|
||||||
common.logging.info('webhook.destroy (410 response)', event, targetUrl);
|
common.logging.info('webhook.destroy (410 response)', event, targetUrl);
|
||||||
return models.Webhook.destroy({id: webhookId}, options);
|
return models.Webhook.destroy({id: webhookId}, options);
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: use i18n?
|
|
||||||
common.logging.error(new common.errors.GhostError({
|
common.logging.error(new common.errors.GhostError({
|
||||||
err: err,
|
err: err,
|
||||||
context: {
|
context: {
|
||||||
|
@ -47,7 +44,6 @@ function makeRequest(webhook, payload, options) {
|
||||||
}
|
}
|
||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
req.end();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function makeRequests(webhooksCollection, payload, options) {
|
function makeRequests(webhooksCollection, payload, options) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue