From 936fc68b5a5462071ad5c5a3ba1f29173e778f90 Mon Sep 17 00:00:00 2001 From: Valentina Date: Tue, 1 Feb 2022 13:33:51 +0200 Subject: [PATCH] Test authentication --- nodes/FriendGrid/FriendGrid.node.ts | 57 ++++++++++++++++++++++++++-- nodes/FriendGrid/GenericFunctions.ts | 6 +-- 2 files changed, 57 insertions(+), 6 deletions(-) diff --git a/nodes/FriendGrid/FriendGrid.node.ts b/nodes/FriendGrid/FriendGrid.node.ts index 36d139d..3159c8e 100644 --- a/nodes/FriendGrid/FriendGrid.node.ts +++ b/nodes/FriendGrid/FriendGrid.node.ts @@ -3,10 +3,13 @@ import { } from 'n8n-core'; import { + ICredentialsDecrypted, + ICredentialTestFunctions, IDataObject, INodeExecutionData, INodeType, INodeTypeDescription, + NodeCredentialTestResult, } from 'n8n-workflow'; import { @@ -18,6 +21,10 @@ import { friendGridApiRequest, } from './GenericFunctions'; +import { + OptionsWithUri +} from 'request'; + export class FriendGrid implements INodeType { description: INodeTypeDescription = { displayName: 'FriendGrid', @@ -37,6 +44,7 @@ export class FriendGrid implements INodeType { { name: 'friendGridApi', required: true, + testedBy: 'testFriendGridApiAuth', }, ], properties: [ @@ -59,17 +67,59 @@ export class FriendGrid implements INodeType { ], }; + methods = { + credentialTest: { + async testFriendGridApiAuth(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise { + + // https://docs.sendgrid.com/api-reference/users-api/retrieve-your-username + const options: OptionsWithUri = { + method: 'GET', + headers: { + 'Accept': ' application/json', + 'Authorization': `Bearer ${credential!.data!.apiKey}`, + }, + uri: 'https://api.sendgrid.com/v3/marketing/user/username', + json: true, + }; + + try { + const response = await this.helpers.request(options); + + if (response.error) { + return { + status: 'Error', + message: `${response.error}`, + }; + } + } catch (err) { + return { + status: 'Error', + message: `${err.message}`, + }; + } + + return { + status: 'OK', + message: 'Connection successful!', + }; + }, + }, + }; + async execute(this: IExecuteFunctions): Promise { const items = this.getInputData(); let responseData; const returnData: IDataObject[] = []; const resource = this.getNodeParameter('resource', 0) as string; const operation = this.getNodeParameter('operation', 0) as string; + let body: IDataObject = {}; + const qs: IDataObject = {}; for (let i = 0; i < items.length; i++) { try { if (resource === 'contact') { if (operation === 'create') { + // https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact const email = this.getNodeParameter('email', i) as string; @@ -80,18 +130,19 @@ export class FriendGrid implements INodeType { Object.assign(data, additionalFields); - const body: IDataObject = { + body = { contacts: [ data, ], }; - responseData = await friendGridApiRequest.call(this, 'POST', '/contact', body); + responseData = await friendGridApiRequest.call(this, 'POST', '/marketing/contact', body); } } + if (Array.isArray(responseData)) { returnData.push.apply(returnData, responseData as IDataObject[]); - } else { + } else if (responseData !== undefined) { returnData.push(responseData as IDataObject); } } catch (error) { diff --git a/nodes/FriendGrid/GenericFunctions.ts b/nodes/FriendGrid/GenericFunctions.ts index 3aabeda..0135d85 100644 --- a/nodes/FriendGrid/GenericFunctions.ts +++ b/nodes/FriendGrid/GenericFunctions.ts @@ -16,7 +16,7 @@ import { } from 'n8n-workflow'; export async function friendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, - method: string, endpoint: string, body?: object, query?: object, uri?: string): Promise { // tslint:disable-line:no-any + method: string, endpoint: string, body?: object, qs?: object, uri?: string): Promise { // tslint:disable-line:no-any //Get credentials the user provided for this node const credentials = await this.getCredentials('friendGridApi') as IDataObject; @@ -32,9 +32,9 @@ export async function friendGridApiRequest(this: IHookFunctions | IExecuteFuncti 'Accept': ' application/json', 'Authorization': `Bearer ${credentials.apiKey}`, }, - qs: query, + qs, body, - uri: uri || `https://api.sendgrid.com/v3/marketing/${endpoint}`, + uri: uri || `https://api.sendgrid.com/v3/${endpoint}`, json: true, };