Drift OAuth2 support

This commit is contained in:
Rupenieks
2020-06-16 11:45:47 +02:00
parent 346ae8efc9
commit 9a449284ae
4 changed files with 96 additions and 14 deletions

View File

@@ -12,25 +12,15 @@ import {
} from 'n8n-workflow';
export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunctions | IHookFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('driftApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
const endpoint = 'https://driftapi.com';
let options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
headers: {},
method,
body,
qs: query,
uri: uri || `${endpoint}${resource}`,
uri: uri || `https://driftapi.com${resource}`,
json: true
};
if (!Object.keys(body).length) {
delete options.form;
}
@@ -38,8 +28,23 @@ export async function driftApiRequest(this: IExecuteFunctions | IWebhookFunction
delete options.qs;
}
options = Object.assign({}, options, option);
const authenticationMethod = this.getNodeParameter('authentication', 0);
try {
return await this.helpers.request!(options);
if (authenticationMethod === 'accessToken') {
const credentials = this.getCredentials('driftApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
options.headers!['Authorization'] = `Bearer ${credentials.accessToken}`;
return await this.helpers.request!(options);
} else {
return await this.helpers.requestOAuth2!.call(this, 'driftOAuth2Api', options);
}
} catch (error) {
if (error.response) {
const errorMessage = error.message || (error.response.body && error.response.body.message );