🔀 Merge branch 'feat/messagebird_add_balance' of https://github.com/Jefiozie/n8n into Jefiozie-feat/messagebird_add_balance

This commit is contained in:
Jan Oberhauser 2020-12-23 10:21:30 +01:00
commit 529c450f49
2 changed files with 42 additions and 7 deletions

View file

@ -43,8 +43,10 @@ export async function messageBirdApiRequest(
uri: `https://rest.messagebird.com${resource}`,
json: true,
};
try {
if (Object.keys(body).length === 0) {
delete options.body;
}
return await this.helpers.request(options);
} catch (error) {
if (error.statusCode === 401) {

View file

@ -1,6 +1,6 @@
import {
IExecuteFunctions,
} from 'n8n-core';
} from 'n8n-core';
import {
IDataObject,
@ -44,6 +44,10 @@ export class MessageBird implements INodeType {
name: 'SMS',
value: 'sms',
},
{
name: 'Balance',
value: 'balance',
},
],
default: 'sms',
description: 'The resource to operate on.',
@ -69,6 +73,27 @@ export class MessageBird implements INodeType {
default: 'send',
description: 'The operation to perform.',
},
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'balance',
],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get the balance',
},
],
default: 'get',
description: 'The operation to perform.',
},
// ----------------------------------
// sms:send
@ -269,11 +294,12 @@ export class MessageBird implements INodeType {
let resource: string;
// For POST
let bodyRequest: IDataObject;
let bodyRequest: IDataObject = {};
// For Query string
let qs: IDataObject;
let requestMethod;
let requestPath;
for (let i = 0; i < items.length; i++) {
qs = {};
@ -289,6 +315,7 @@ export class MessageBird implements INodeType {
// ----------------------------------
requestMethod = 'POST';
requestPath = '/messages';
const originator = this.getNodeParameter('originator', i) as string;
const body = this.getNodeParameter('message', i) as string;
@ -337,21 +364,27 @@ export class MessageBird implements INodeType {
}
const receivers = this.getNodeParameter('recipients', i) as string;
bodyRequest.recipients = receivers.split(',').map(item => {
return parseInt(item, 10);
});
} else {
}
else {
throw new Error(`The operation "${operation}" is not known!`);
}
} else {
} else if (resource === 'balance') {
requestMethod = 'GET';
requestPath = '/balance';
}
else {
throw new Error(`The resource "${resource}" is not known!`);
}
const responseData = await messageBirdApiRequest.call(
this,
requestMethod,
'/messages',
requestPath,
bodyRequest,
qs,
);