2020-01-18 07:28:19 -08:00
|
|
|
import { createHash } from 'crypto';
|
2020-01-17 14:48:54 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-01-18 07:28:19 -08:00
|
|
|
|
2020-01-17 14:48:54 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
2020-01-18 07:28:19 -08:00
|
|
|
import {
|
|
|
|
ICredentialDataDecryptedObject,
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
2020-01-17 14:48:54 -08:00
|
|
|
|
|
|
|
export async function copperApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions | IWebhookFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
const credentials = this.getCredentials('copperApi');
|
|
|
|
if (credentials === undefined) {
|
|
|
|
throw new Error('No credentials got returned!');
|
|
|
|
}
|
2020-01-18 07:28:19 -08:00
|
|
|
|
2020-01-17 14:48:54 -08:00
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: {
|
|
|
|
'X-PW-AccessToken': credentials.apiKey,
|
|
|
|
'X-PW-Application': 'developer_api',
|
|
|
|
'X-PW-UserEmail': credentials.email,
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
|
|
|
uri: uri ||`https://api.prosperworks.com/developer_api/v1${resource}`,
|
|
|
|
json: true
|
|
|
|
};
|
|
|
|
options = Object.assign({}, options, option);
|
|
|
|
if (Object.keys(options.body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2020-01-18 07:28:19 -08:00
|
|
|
let errorMessage = error.message;
|
|
|
|
if (error.response.body && error.response.body.message) {
|
|
|
|
errorMessage = error.response.body.message;
|
|
|
|
}
|
|
|
|
|
|
|
|
throw new Error('Copper Error: ' + errorMessage);
|
2020-01-17 14:48:54 -08:00
|
|
|
}
|
|
|
|
}
|
2020-01-18 07:28:19 -08:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates a secret from the credentials
|
|
|
|
*
|
|
|
|
* @export
|
|
|
|
* @param {ICredentialDataDecryptedObject} credentials
|
|
|
|
* @returns
|
|
|
|
*/
|
|
|
|
export function getAutomaticSecret(credentials: ICredentialDataDecryptedObject) {
|
|
|
|
const data = `${credentials.email},${credentials.apiKey}`;
|
|
|
|
return createHash('md5').update(data).digest("hex");
|
|
|
|
}
|