Test authentication

This commit is contained in:
Valentina 2022-02-01 13:33:51 +02:00
commit 936fc68b5a
2 changed files with 57 additions and 6 deletions

View file

@ -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<NodeCredentialTestResult> {
// 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<INodeExecutionData[][]> {
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) {

View file

@ -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<any> { // tslint:disable-line:no-any
method: string, endpoint: string, body?: object, qs?: object, uri?: string): Promise<any> { // 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,
};