mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-12 05:17:28 -08:00
Add request for all items
This commit is contained in:
parent
cbbeb0f469
commit
dfd29f2e14
|
@ -5,7 +5,6 @@ import {
|
|||
|
||||
import {
|
||||
IDataObject,
|
||||
IOAuth2Options,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import {
|
||||
|
@ -14,21 +13,15 @@ import {
|
|||
|
||||
|
||||
/**
|
||||
* Make an API request to Reddit
|
||||
*
|
||||
* @param { IHookFunctions } this
|
||||
* @param { string } method
|
||||
* @param { string } endpoint
|
||||
* @param { IDataObject } qs
|
||||
* @returns { Promise<any> }
|
||||
* Make an API request to Reddit.
|
||||
*/
|
||||
export async function redditApiRequest(
|
||||
this: IHookFunctions | IExecuteFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
body?: IDataObject,
|
||||
qs?: IDataObject,
|
||||
noAuthRequired?: boolean,
|
||||
qs: IDataObject,
|
||||
body: IDataObject,
|
||||
noAuth: boolean,
|
||||
): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
const options: OptionsWithUri = {
|
||||
|
@ -36,20 +29,22 @@ export async function redditApiRequest(
|
|||
'user-agent': 'n8n',
|
||||
},
|
||||
method,
|
||||
uri: noAuthRequired ? `https://www.reddit.com/${endpoint}` : `https://oauth.reddit.com/api/v1/${endpoint}`,
|
||||
uri: noAuth ? `https://www.reddit.com/${endpoint}` : `https://oauth.reddit.com/api/v1/${endpoint}`,
|
||||
qs,
|
||||
body,
|
||||
json: true,
|
||||
};
|
||||
|
||||
if (body && Object.keys(body).length) {
|
||||
options.body = body;
|
||||
if (!Object.keys(body).length) {
|
||||
delete options.body;
|
||||
}
|
||||
|
||||
if (qs && Object.keys(qs).length) {
|
||||
options.qs = qs;
|
||||
if (!Object.keys(qs).length) {
|
||||
delete options.qs;
|
||||
}
|
||||
|
||||
try {
|
||||
return noAuthRequired
|
||||
return noAuth
|
||||
? await this.helpers.request.call(this, options)
|
||||
: await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
|
||||
} catch (error) {
|
||||
|
@ -59,26 +54,34 @@ export async function redditApiRequest(
|
|||
}
|
||||
throw error;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Make an API request to Reddit and return all results
|
||||
*
|
||||
* @export
|
||||
* @param { (IHookFunctions | IExecuteFunctions) } this
|
||||
* @param { string } method
|
||||
* @param { string } endpoint
|
||||
* @param { IDataObject } qs
|
||||
* @returns { Promise<any> }
|
||||
* Make an API request to Reddit and return all results.
|
||||
*/
|
||||
export async function redditApiRequestAllItems(
|
||||
this: IHookFunctions | IExecuteFunctions,
|
||||
method: string,
|
||||
endpoint: string,
|
||||
qs: IDataObject,
|
||||
body: IDataObject,
|
||||
noAuth: boolean,
|
||||
): Promise<any> { // tslint:disable-line:no-any
|
||||
|
||||
// ...
|
||||
let responseData;
|
||||
const returnData: IDataObject[] = [];
|
||||
|
||||
do {
|
||||
responseData = await redditApiRequest.call(this, method, endpoint, qs, body, noAuth);
|
||||
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;
|
||||
}
|
||||
|
|
|
@ -54,5 +54,51 @@ export const listingFields = [
|
|||
},
|
||||
},
|
||||
},
|
||||
|
||||
{
|
||||
displayName: 'Return All',
|
||||
name: 'returnAll',
|
||||
type: 'boolean',
|
||||
default: false,
|
||||
description: 'Return all results',
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'listing',
|
||||
],
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
type: [
|
||||
'best',
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
displayName: 'Limit',
|
||||
name: 'limit',
|
||||
type: 'number',
|
||||
default: 5,
|
||||
description: 'The number of results to return',
|
||||
typeOptions: {
|
||||
minValue: 1,
|
||||
maxValue: 100,
|
||||
},
|
||||
displayOptions: {
|
||||
show: {
|
||||
resource: [
|
||||
'listing',
|
||||
],
|
||||
operation: [
|
||||
'get',
|
||||
],
|
||||
type: [
|
||||
'best',
|
||||
],
|
||||
returnAll: [
|
||||
false,
|
||||
],
|
||||
},
|
||||
},
|
||||
},
|
||||
] as INodeProperties[];
|
||||
|
|
|
@ -117,7 +117,7 @@ export class Reddit implements INodeType {
|
|||
trophies: 'me/trophies',
|
||||
};
|
||||
|
||||
responseData = await redditApiRequest.call(this, 'GET', endpoints[details]);
|
||||
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
|
||||
|
||||
if (details === 'identity') {
|
||||
responseData = responseData.features;
|
||||
|
@ -145,7 +145,7 @@ export class Reddit implements INodeType {
|
|||
body.resubmit = true;
|
||||
}
|
||||
|
||||
responseData = await redditApiRequest.call(this, 'POST', 'submit', body);
|
||||
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false);
|
||||
|
||||
}
|
||||
|
||||
|
@ -162,9 +162,23 @@ export class Reddit implements INodeType {
|
|||
|
||||
} else if (type === 'best') {
|
||||
|
||||
const endpoint = 'best.json';
|
||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
|
||||
const returnAll = this.getNodeParameter('returnAll', i);
|
||||
|
||||
if (returnAll) {
|
||||
|
||||
const endpoint = 'best.json';
|
||||
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true);
|
||||
|
||||
} else {
|
||||
|
||||
const qs: IDataObject = {
|
||||
limit: this.getNodeParameter('limit', i),
|
||||
};
|
||||
const endpoint = 'best.json';
|
||||
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true);
|
||||
responseData = responseData.splice(0, qs.limit);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue