Merge pull request #641 from n8n-io/Pipedrive-OAuth2-support

Pipedrive OAuth2 Support
This commit is contained in:
Ricardo Espinoza 2020-07-23 17:42:13 -04:00 committed by GitHub
commit 61d7c52787
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 110 additions and 21 deletions

View file

@ -0,0 +1,46 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class PipedriveOAuth2Api implements ICredentialType {
name = 'pipedriveOAuth2Api';
extends = [
'oAuth2Api',
];
displayName = 'Pipedrive OAuth2 API';
properties = [
{
displayName: 'Authorization URL',
name: 'authUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://oauth.pipedrive.com/oauth/authorize',
required: true,
},
{
displayName: 'Access Token URL',
name: 'accessTokenUrl',
type: 'hidden' as NodePropertyTypes,
default: 'https://oauth.pipedrive.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: 'header',
},
];
}

View file

@ -25,7 +25,6 @@ export interface ICustomProperties {
[key: string]: ICustomInterface; [key: string]: ICustomInterface;
} }
/** /**
* Make an API request to Pipedrive * Make an API request to Pipedrive
* *
@ -36,16 +35,7 @@ export interface ICustomProperties {
* @returns {Promise<any>} * @returns {Promise<any>}
*/ */
export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise<any> { // tslint:disable-line:no-any export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: IDataObject, query?: IDataObject, formData?: IDataObject, downloadFile?: boolean): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('pipedriveApi'); const authenticationMethod = this.getNodeParameter('authentication', 0);
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
if (query === undefined) {
query = {};
}
query.api_token = credentials.apiToken;
const options: OptionsWithUri = { const options: OptionsWithUri = {
headers: { headers: {
@ -70,9 +60,28 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
options.formData = formData; options.formData = formData;
} }
if (query === undefined) {
query = {};
}
let responseData;
try { try {
//@ts-ignore if (authenticationMethod === 'basicAuth' || authenticationMethod === 'apiToken') {
const responseData = await this.helpers.request(options);
const credentials = this.getCredentials('pipedriveApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
query.api_token = credentials.apiToken;
//@ts-ignore
responseData = await this.helpers.request(options);
} else {
responseData = await this.helpers.requestOAuth2!.call(this, 'pipedriveOAuth2Api', options);
}
if (downloadFile === true) { if (downloadFile === true) {
return { return {
@ -88,7 +97,7 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
additionalData: responseData.additional_data, additionalData: responseData.additional_data,
data: responseData.data, data: responseData.data,
}; };
} catch (error) { } catch(error) {
if (error.statusCode === 401) { if (error.statusCode === 401) {
// Return a clear error // Return a clear error
throw new Error('The Pipedrive credentials are not valid!'); throw new Error('The Pipedrive credentials are not valid!');
@ -108,8 +117,6 @@ export async function pipedriveApiRequest(this: IHookFunctions | IExecuteFunctio
} }
} }
/** /**
* Make an API request to paginated Pipedrive endpoint * Make an API request to paginated Pipedrive endpoint
* and return all results * and return all results

View file

@ -62,9 +62,44 @@ export class Pipedrive implements INodeType {
{ {
name: 'pipedriveApi', name: 'pipedriveApi',
required: true, required: true,
} displayOptions: {
show: {
authentication: [
'apiToken',
],
},
},
},
{
name: 'pipedriveOAuth2Api',
required: true,
displayOptions: {
show: {
authentication: [
'oAuth2',
],
},
},
},
], ],
properties: [ properties: [
{
displayName: 'Authentication',
name: 'authentication',
type: 'options',
options: [
{
name: 'API Token',
value: 'apiToken'
},
{
name: 'OAuth2',
value: 'oAuth2',
},
],
default: 'apiToken',
description: 'Method of authentication.',
},
{ {
displayName: 'Resource', displayName: 'Resource',
name: 'resource', name: 'resource',
@ -2102,6 +2137,7 @@ export class Pipedrive implements INodeType {
displayName: 'Term', displayName: 'Term',
name: 'term', name: 'term',
type: 'string', type: 'string',
required: true,
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [

View file

@ -14,8 +14,10 @@ import {
} from './GenericFunctions'; } from './GenericFunctions';
import * as basicAuth from 'basic-auth'; import * as basicAuth from 'basic-auth';
import { Response } from 'express';
import {
Response,
} from 'express';
function authorizationError(resp: Response, realm: string, responseCode: number, message?: string) { function authorizationError(resp: Response, realm: string, responseCode: number, message?: string) {
if (message === undefined) { if (message === undefined) {
@ -179,7 +181,6 @@ export class PipedriveTrigger implements INodeType {
description: 'Type of object to receive notifications about.', description: 'Type of object to receive notifications about.',
}, },
], ],
}; };
// @ts-ignore (because of request) // @ts-ignore (because of request)
@ -276,8 +277,6 @@ export class PipedriveTrigger implements INodeType {
}, },
}; };
async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> { async webhook(this: IWebhookFunctions): Promise<IWebhookResponseData> {
const req = this.getRequestObject(); const req = this.getRequestObject();
const resp = this.getResponseObject(); const resp = this.getResponseObject();

View file

@ -113,6 +113,7 @@
"dist/credentials/PagerDutyOAuth2Api.credentials.js", "dist/credentials/PagerDutyOAuth2Api.credentials.js",
"dist/credentials/PayPalApi.credentials.js", "dist/credentials/PayPalApi.credentials.js",
"dist/credentials/PipedriveApi.credentials.js", "dist/credentials/PipedriveApi.credentials.js",
"dist/credentials/PipedriveOAuth2Api.credentials.js",
"dist/credentials/Postgres.credentials.js", "dist/credentials/Postgres.credentials.js",
"dist/credentials/PostmarkApi.credentials.js", "dist/credentials/PostmarkApi.credentials.js",
"dist/credentials/QuestDb.credentials.js", "dist/credentials/QuestDb.credentials.js",