2019-10-20 13:57:06 -07:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2020-04-22 10:08:53 -07:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
2020-10-03 10:18:42 -07:00
|
|
|
} from 'request';
|
2020-04-22 10:08:53 -07:00
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2020-10-03 10:18:42 -07:00
|
|
|
} from 'n8n-workflow';
|
2019-10-20 13:57:06 -07:00
|
|
|
|
2020-10-03 10:18:42 -07:00
|
|
|
export interface IAttachment {
|
2020-04-11 05:00:30 -07:00
|
|
|
fields: {
|
|
|
|
item?: object[];
|
2020-04-14 10:17:55 -07:00
|
|
|
};
|
2020-04-11 05:00:30 -07:00
|
|
|
actions: {
|
|
|
|
item?: object[];
|
|
|
|
};
|
2020-04-14 10:17:55 -07:00
|
|
|
}
|
2019-10-21 15:12:35 -07:00
|
|
|
|
2019-10-20 13:57:06 -07:00
|
|
|
/**
|
|
|
|
* Make an API request to Telegram
|
|
|
|
*
|
|
|
|
* @param {IHookFunctions} this
|
|
|
|
* @param {string} method
|
|
|
|
* @param {string} url
|
|
|
|
* @param {object} body
|
|
|
|
* @returns {Promise<any>}
|
|
|
|
*/
|
|
|
|
export async function apiRequest(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: object, query?: IDataObject): Promise<any> { // tslint:disable-line:no-any
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('mattermostApi');
|
2019-10-20 13:57:06 -07:00
|
|
|
|
|
|
|
if (credentials === undefined) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeOperationError(this.getNode(), 'No credentials got returned!');
|
2019-10-20 13:57:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
query = query || {};
|
|
|
|
|
|
|
|
const options: OptionsWithUri = {
|
|
|
|
method,
|
|
|
|
body,
|
|
|
|
qs: query,
|
|
|
|
uri: `${credentials.baseUrl}/api/v4/${endpoint}`,
|
|
|
|
headers: {
|
|
|
|
Authorization: `Bearer ${credentials.accessToken}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
'content-type': 'application/json; charset=utf-8',
|
2019-10-20 13:57:06 -07:00
|
|
|
},
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2019-10-20 13:57:06 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2019-10-20 13:57:06 -07:00
|
|
|
}
|
|
|
|
}
|
2020-04-22 10:08:53 -07:00
|
|
|
|
|
|
|
export async function apiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
query.page = 0;
|
|
|
|
query.per_page = 100;
|
|
|
|
|
|
|
|
do {
|
|
|
|
responseData = await apiRequest.call(this, method, endpoint, body, query);
|
|
|
|
query.page++;
|
|
|
|
returnData.push.apply(returnData, responseData);
|
|
|
|
} while (
|
|
|
|
responseData.length !== 0
|
|
|
|
);
|
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|