mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 10:02:05 +00:00
Drift OAuth2 support
This commit is contained in:
@@ -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 );
|
||||
|
||||
Reference in New Issue
Block a user