2021-01-15 13:06:17 -08:00
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IDataObject,
|
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-17 11:17:17 -08:00
|
|
|
* Make an API request to Reddit.
|
2021-01-15 13:06:17 -08:00
|
|
|
*/
|
|
|
|
export async function redditApiRequest(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
2021-01-17 11:17:17 -08:00
|
|
|
qs: IDataObject,
|
|
|
|
body: IDataObject,
|
2021-01-15 13:06:17 -08:00
|
|
|
): Promise<any> { // tslint:disable-line:no-any
|
2021-01-17 10:00:21 -08:00
|
|
|
|
2021-01-17 15:37:43 -08:00
|
|
|
const resource = this.getNodeParameter('resource', 0) as string;
|
|
|
|
const requiresAuth = ['myAccount', 'submission'].includes(resource);
|
|
|
|
|
2021-01-15 13:06:17 -08:00
|
|
|
const options: OptionsWithUri = {
|
2021-01-15 15:15:32 -08:00
|
|
|
headers: {
|
|
|
|
'user-agent': 'n8n',
|
|
|
|
},
|
2021-01-15 13:06:17 -08:00
|
|
|
method,
|
2021-01-17 15:37:43 -08:00
|
|
|
uri: requiresAuth ? `https://oauth.reddit.com/api/v1/${endpoint}` : `https://www.reddit.com/${endpoint}`,
|
2021-01-17 11:17:17 -08:00
|
|
|
qs,
|
|
|
|
body,
|
2021-01-15 13:06:17 -08:00
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
|
2021-01-17 11:17:17 -08:00
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
2021-01-15 16:20:37 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 11:17:17 -08:00
|
|
|
if (!Object.keys(qs).length) {
|
|
|
|
delete options.qs;
|
2021-01-15 13:06:17 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2021-01-17 15:37:43 -08:00
|
|
|
return requiresAuth
|
|
|
|
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
|
|
|
|
: await this.helpers.request.call(this, options);
|
|
|
|
|
2021-01-15 13:06:17 -08:00
|
|
|
} catch (error) {
|
2021-01-15 15:15:32 -08:00
|
|
|
if (error.message) {
|
|
|
|
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]);
|
|
|
|
throw new Error(`Reddit error response [${errorObject.error}]: ${errorObject.message}`);
|
|
|
|
}
|
2021-01-15 13:06:17 -08:00
|
|
|
throw error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
2021-01-17 11:17:17 -08:00
|
|
|
* Make an API request to Reddit and return all results.
|
2021-01-15 13:06:17 -08:00
|
|
|
*/
|
|
|
|
export async function redditApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions,
|
|
|
|
method: string,
|
|
|
|
endpoint: string,
|
|
|
|
qs: IDataObject,
|
2021-01-17 11:17:17 -08:00
|
|
|
body: IDataObject,
|
2021-01-15 13:06:17 -08:00
|
|
|
): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
2021-01-17 11:17:17 -08:00
|
|
|
let responseData;
|
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
do {
|
2021-01-17 15:37:43 -08:00
|
|
|
responseData = await redditApiRequest.call(this, method, endpoint, qs, body);
|
2021-01-17 11:17:17 -08:00
|
|
|
qs.after = responseData.after;
|
|
|
|
responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any
|
|
|
|
|
|
|
|
if (qs.limit && returnData.length >= qs.limit) {
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
} while (responseData.after);
|
|
|
|
|
|
|
|
return returnData;
|
2021-01-15 13:06:17 -08:00
|
|
|
}
|
2021-01-17 14:36:30 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles a large Reddit listing by returning all items or up to a limit.
|
|
|
|
*/
|
|
|
|
export async function handleListing(
|
|
|
|
this: IExecuteFunctions,
|
|
|
|
i: number,
|
|
|
|
endpoint: string,
|
|
|
|
): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
const returnAll = this.getNodeParameter('returnAll', i);
|
|
|
|
if (returnAll) {
|
2021-01-17 15:37:43 -08:00
|
|
|
return await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {});
|
2021-01-17 14:36:30 -08:00
|
|
|
}
|
|
|
|
|
2021-01-17 15:26:04 -08:00
|
|
|
const qs: IDataObject = {
|
|
|
|
limit: this.getNodeParameter('limit', i),
|
|
|
|
};
|
2021-01-17 15:37:43 -08:00
|
|
|
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {});
|
2021-01-17 14:36:30 -08:00
|
|
|
responseData = responseData.splice(0, qs.limit);
|
|
|
|
|
|
|
|
return responseData;
|
|
|
|
}
|