mirror of
https://github.com/n8n-io/n8n-nodes-starter.git
synced 2025-10-29 14:22:26 -05:00
Test authentication
This commit is contained in:
parent
58b5d0d9c0
commit
936fc68b5a
2 changed files with 57 additions and 6 deletions
|
|
@ -3,10 +3,13 @@ import {
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
ICredentialsDecrypted,
|
||||||
|
ICredentialTestFunctions,
|
||||||
IDataObject,
|
IDataObject,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
NodeCredentialTestResult,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
|
@ -18,6 +21,10 @@ import {
|
||||||
friendGridApiRequest,
|
friendGridApiRequest,
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
|
import {
|
||||||
|
OptionsWithUri
|
||||||
|
} from 'request';
|
||||||
|
|
||||||
export class FriendGrid implements INodeType {
|
export class FriendGrid implements INodeType {
|
||||||
description: INodeTypeDescription = {
|
description: INodeTypeDescription = {
|
||||||
displayName: 'FriendGrid',
|
displayName: 'FriendGrid',
|
||||||
|
|
@ -37,6 +44,7 @@ export class FriendGrid implements INodeType {
|
||||||
{
|
{
|
||||||
name: 'friendGridApi',
|
name: 'friendGridApi',
|
||||||
required: true,
|
required: true,
|
||||||
|
testedBy: 'testFriendGridApiAuth',
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
properties: [
|
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[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
let responseData;
|
let responseData;
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
const resource = this.getNodeParameter('resource', 0) as string;
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
const operation = this.getNodeParameter('operation', 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++) {
|
for (let i = 0; i < items.length; i++) {
|
||||||
try {
|
try {
|
||||||
if (resource === 'contact') {
|
if (resource === 'contact') {
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
|
// https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact
|
||||||
|
|
||||||
const email = this.getNodeParameter('email', i) as string;
|
const email = this.getNodeParameter('email', i) as string;
|
||||||
|
|
||||||
|
|
@ -80,18 +130,19 @@ export class FriendGrid implements INodeType {
|
||||||
|
|
||||||
Object.assign(data, additionalFields);
|
Object.assign(data, additionalFields);
|
||||||
|
|
||||||
const body: IDataObject = {
|
body = {
|
||||||
contacts: [
|
contacts: [
|
||||||
data,
|
data,
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
responseData = await friendGridApiRequest.call(this, 'POST', '/contact', body);
|
responseData = await friendGridApiRequest.call(this, 'POST', '/marketing/contact', body);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Array.isArray(responseData)) {
|
if (Array.isArray(responseData)) {
|
||||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||||
} else {
|
} else if (responseData !== undefined) {
|
||||||
returnData.push(responseData as IDataObject);
|
returnData.push(responseData as IDataObject);
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ import {
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
export async function friendGridApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
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
|
//Get credentials the user provided for this node
|
||||||
const credentials = await this.getCredentials('friendGridApi') as IDataObject;
|
const credentials = await this.getCredentials('friendGridApi') as IDataObject;
|
||||||
|
|
@ -32,9 +32,9 @@ export async function friendGridApiRequest(this: IHookFunctions | IExecuteFuncti
|
||||||
'Accept': ' application/json',
|
'Accept': ' application/json',
|
||||||
'Authorization': `Bearer ${credentials.apiKey}`,
|
'Authorization': `Bearer ${credentials.apiKey}`,
|
||||||
},
|
},
|
||||||
qs: query,
|
qs,
|
||||||
body,
|
body,
|
||||||
uri: uri || `https://api.sendgrid.com/v3/marketing/${endpoint}`,
|
uri: uri || `https://api.sendgrid.com/v3/${endpoint}`,
|
||||||
json: true,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue