Merge pull request #675 from n8n-io/Eventbrite-OAuth2-support

Eventbrite OAuth2 support & Deprecated webhook API fixed
This commit is contained in:
Ricardo Espinoza 2020-06-25 19:45:58 -04:00 committed by GitHub
commit 671aea3dd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 134 additions and 23 deletions

View file

@ -8,7 +8,7 @@ export class EventbriteApi implements ICredentialType {
displayName = 'Eventbrite API';
properties = [
{
displayName: 'API Key',
displayName: 'Private Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',

View file

@ -0,0 +1,47 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class EventbriteOAuth2Api implements ICredentialType {
name = 'eventbriteOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Eventbrite OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://www.eventbrite.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://www.eventbrite.com/oauth/token',
required: true,
},
{
displayName: 'Scope',
name: 'scope',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Auth URI Query Parameters',
name: 'authQueryParameters',
type: 'hidden' as NodePropertyTypes,
default: '',
},
{
displayName: 'Authentication',
name: 'authentication',
type: 'hidden' as NodePropertyTypes,
default: 'body'
},
];
}

View file

@ -35,7 +35,25 @@ export class EventbriteTrigger implements INodeType {
{
name: 'eventbriteApi',
required: true,
}
displayOptions: {
show: {
authentication: [
'privateKey',
],
},
},
},
{
name: 'eventbriteOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
],
webhooks: [
{
@ -46,6 +64,23 @@ export class EventbriteTrigger implements INodeType {
},
],
properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'Private Key',
value: 'privateKey',
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'privateKey',
description: 'The resource to operate on.',
},
{
displayName: 'Organization',
name: 'organization',
@ -149,7 +184,6 @@ export class EventbriteTrigger implements INodeType {
description: 'By default does the webhook-data only contain the URL to receive<br />the object data manually. If this option gets activated it<br />will resolve the data automatically.',
},
],
};
methods = {
@ -192,23 +226,39 @@ export class EventbriteTrigger implements INodeType {
default: {
async checkExists(this: IHookFunctions): Promise<boolean> {
const webhookData = this.getWorkflowStaticData('node');
if (webhookData.webhookId === undefined) {
return false;
const webhookUrl = this.getNodeWebhookUrl('default');
const organisation = this.getNodeParameter('organization') as string;
const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/organizations/${organisation}/webhooks/`;
const { webhooks } = await eventbriteApiRequest.call(this, 'GET', endpoint);
const check = (currentActions: string[], webhookActions: string[]) => {
for (const currentAction of currentActions) {
if (!webhookActions.includes(currentAction)) {
return false;
}
}
return true;
};
for (const webhook of webhooks) {
if (webhook.endpoint_url === webhookUrl && check(actions, webhook.actions)) {
webhookData.webhookId = webhook.id;
return true;
}
}
const endpoint = `/webhooks/${webhookData.webhookId}/`;
try {
await eventbriteApiRequest.call(this, 'GET', endpoint);
} catch (e) {
return false;
}
return true;
return false;
},
async create(this: IHookFunctions): Promise<boolean> {
const webhookUrl = this.getNodeWebhookUrl('default');
const webhookData = this.getWorkflowStaticData('node');
const organisation = this.getNodeParameter('organization') as string;
const event = this.getNodeParameter('event') as string;
const actions = this.getNodeParameter('actions') as string[];
const endpoint = `/webhooks/`;
const endpoint = `/organizations/${organisation}/webhooks/`;
const body: IDataObject = {
endpoint_url: webhookUrl,
actions: actions.join(','),

View file

@ -1,4 +1,7 @@
import { OptionsWithUri } from 'request';
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
@ -6,16 +9,14 @@ import {
ILoadOptionsFunctions,
IWebhookFunctions,
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
import {
IDataObject,
} from 'n8n-workflow';
export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('eventbriteApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let options: OptionsWithUri = {
headers: { 'Authorization': `Bearer ${credentials.apiKey}`},
headers: {},
method,
qs,
body,
@ -27,14 +28,26 @@ export async function eventbriteApiRequest(this: IHookFunctions | IExecuteFuncti
delete options.body;
}
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
return await this.helpers.request!(options);
if (authenticationMethod === 'privateKey') {
const credentials = this.getCredentials('eventbriteApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.headers!['Authorization'] = `Bearer ${credentials.apiKey}`;
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'eventbriteOAuth2Api', options);
}
} catch (error) {
let errorMessage = error.message;
if (error.response.body && error.response.body.error_description) {
errorMessage = error.response.body.error_description;
}
throw new Error('Eventbrite Error: ' + errorMessage);
}
}

View file

@ -49,6 +49,7 @@
"dist/credentials/DriftApi.credentials.js",
"dist/credentials/DropboxApi.credentials.js",
"dist/credentials/EventbriteApi.credentials.js",
"dist/credentials/EventbriteOAuth2Api.credentials.js",
"dist/credentials/FacebookGraphApi.credentials.js",
"dist/credentials/FreshdeskApi.credentials.js",
"dist/credentials/FileMaker.credentials.js",