2019-12-21 18:44:56 -08:00
|
|
|
import { OptionsWithUri } from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-10-01 05:01:39 -07:00
|
|
|
IExecuteSingleFunctions,
|
2019-12-21 18:44:56 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2019-12-31 12:19:37 -08:00
|
|
|
IPollFunctions,
|
2019-12-21 18:44:56 -08:00
|
|
|
ITriggerFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-11-08 06:28:21 -08:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
2019-12-21 18:44:56 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function togglApiRequest(
|
|
|
|
this:
|
|
|
|
| ITriggerFunctions
|
|
|
|
| IPollFunctions
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
body: any = {},
|
|
|
|
query?: IDataObject,
|
|
|
|
uri?: string,
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('togglApi');
|
2022-08-17 08:50:24 -07:00
|
|
|
const headerWithAuthentication = Object.assign(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
Authorization: ` Basic ${Buffer.from(
|
|
|
|
`${credentials.username}:${credentials.password}`,
|
|
|
|
).toString('base64')}`,
|
|
|
|
},
|
|
|
|
);
|
2019-12-21 18:44:56 -08:00
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
|
|
|
qs: query,
|
2021-11-13 01:14:25 -08:00
|
|
|
uri: uri || `https://api.track.toggl.com/api/v8${resource}`,
|
2019-12-21 18:44:56 -08:00
|
|
|
body,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-12-21 18:44:56 -08:00
|
|
|
};
|
|
|
|
if (Object.keys(options.body).length === 0) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-12-21 18:44:56 -08:00
|
|
|
}
|
|
|
|
}
|