Remove parameter and improve retry functionality

This commit is contained in:
Jan Oberhauser 2019-10-30 14:56:58 +01:00
parent 5f0d222d6f
commit 117cca0505

View file

@ -1,3 +1,4 @@
import { get } from 'lodash';
import { IExecuteFunctions } from 'n8n-core'; import { IExecuteFunctions } from 'n8n-core';
import { import {
IDataObject, IDataObject,
@ -13,7 +14,6 @@ export class Discord implements INodeType {
icon: 'file:discord.png', icon: 'file:discord.png',
group: ['output'], group: ['output'],
version: 1, version: 1,
subtitle: '={{$parameter["resource"]}}',
description: 'Sends data to Discord', description: 'Sends data to Discord',
defaults: { defaults: {
name: 'Discord', name: 'Discord',
@ -22,27 +22,6 @@ export class Discord implements INodeType {
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
properties: [ properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Webhook',
value: 'webhook',
},
],
default: 'webhook',
description: 'The resource to operate on.',
},
// ----------------------------------
// message
// ----------------------------------
// ----------------------------------
// message:post
// ----------------------------------
{ {
displayName: 'Webhook URL', displayName: 'Webhook URL',
name: 'webhookUri', name: 'webhookUri',
@ -51,14 +30,7 @@ export class Discord implements INodeType {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
default: '', default: '',
displayOptions: { description: 'The webhook url.',
show: {
resource: [
'webhook',
],
},
},
description: 'The webhook url',
}, },
{ {
displayName: 'Text', displayName: 'Text',
@ -68,13 +40,6 @@ export class Discord implements INodeType {
alwaysOpenEditWindow: true, alwaysOpenEditWindow: true,
}, },
default: '', default: '',
displayOptions: {
show: {
resource: [
'webhook',
],
},
},
description: 'The text to send.', description: 'The text to send.',
} }
], ],
@ -85,10 +50,8 @@ export class Discord implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData(); const items = this.getInputData();
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let responseData;
let resource: string; const requestMethod = 'POST';
let requestMethod = 'POST';
// For Post // For Post
let body: IDataObject; let body: IDataObject;
@ -97,14 +60,7 @@ export class Discord implements INodeType {
const webhookUri = this.getNodeParameter('webhookUri', i) as string; const webhookUri = this.getNodeParameter('webhookUri', i) as string;
body = {}; body = {};
resource = this.getNodeParameter('resource', i) as string; body.content = this.getNodeParameter('text', i) as string;
if (resource === 'webhook') {
requestMethod = 'POST';
body.content = this.getNodeParameter('text', i) as string;
} else {
throw new Error(`The resource "${resource}" is not known!`);
}
const options = { const options = {
method: requestMethod, method: requestMethod,
@ -116,22 +72,32 @@ export class Discord implements INodeType {
json: true json: true
}; };
try { let maxTries = 5;
responseData = await this.helpers.request(options); do {
} catch (error) { try {
if (error.statusCode === 429) { await this.helpers.request(options);
// Waiting rating limit break;
setTimeout(async () => { } catch (error) {
responseData = await this.helpers.request(options) if (error.statusCode === 429) {
}, // Waiting rating limit
error.response.body.retry_after); await new Promise((resolve) => {
}else { setTimeout(async () => {
// If it's another error code then return the JSON response resolve();
throw error; }, get(error, 'response.body.retry_after', 150));
});
} else {
// If it's another error code then return the JSON response
throw error;
}
} }
} while (--maxTries);
if (maxTries <= 0) {
throw new Error('Could not send message. Max. amount of rate-limit retries got reached.');
} }
returnData.push(responseData as IDataObject); returnData.push({success: true});
} }
return [this.helpers.returnJsonArray(returnData)]; return [this.helpers.returnJsonArray(returnData)];