mirror of
https://github.com/n8n-io/n8n.git
synced 2025-01-27 04:12:38 -08:00
Add request for all items
This commit is contained in:
parent
cbbeb0f469
commit
dfd29f2e14
|
@ -5,7 +5,6 @@ import {
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IDataObject,
|
IDataObject,
|
||||||
IOAuth2Options,
|
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -14,21 +13,15 @@ import {
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make an API request to Reddit
|
* Make an API request to Reddit.
|
||||||
*
|
|
||||||
* @param { IHookFunctions } this
|
|
||||||
* @param { string } method
|
|
||||||
* @param { string } endpoint
|
|
||||||
* @param { IDataObject } qs
|
|
||||||
* @returns { Promise<any> }
|
|
||||||
*/
|
*/
|
||||||
export async function redditApiRequest(
|
export async function redditApiRequest(
|
||||||
this: IHookFunctions | IExecuteFunctions,
|
this: IHookFunctions | IExecuteFunctions,
|
||||||
method: string,
|
method: string,
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
body?: IDataObject,
|
qs: IDataObject,
|
||||||
qs?: IDataObject,
|
body: IDataObject,
|
||||||
noAuthRequired?: boolean,
|
noAuth: boolean,
|
||||||
): Promise<any> { // tslint:disable-line:no-any
|
): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
|
@ -36,20 +29,22 @@ export async function redditApiRequest(
|
||||||
'user-agent': 'n8n',
|
'user-agent': 'n8n',
|
||||||
},
|
},
|
||||||
method,
|
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,
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (body && Object.keys(body).length) {
|
if (!Object.keys(body).length) {
|
||||||
options.body = body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (qs && Object.keys(qs).length) {
|
if (!Object.keys(qs).length) {
|
||||||
options.qs = qs;
|
delete options.qs;
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return noAuthRequired
|
return noAuth
|
||||||
? await this.helpers.request.call(this, options)
|
? await this.helpers.request.call(this, options)
|
||||||
: await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
|
: await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
@ -59,26 +54,34 @@ export async function redditApiRequest(
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Make an API request to Reddit and return all results
|
* 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> }
|
|
||||||
*/
|
*/
|
||||||
export async function redditApiRequestAllItems(
|
export async function redditApiRequestAllItems(
|
||||||
this: IHookFunctions | IExecuteFunctions,
|
this: IHookFunctions | IExecuteFunctions,
|
||||||
method: string,
|
method: string,
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
qs: IDataObject,
|
qs: IDataObject,
|
||||||
|
body: IDataObject,
|
||||||
|
noAuth: boolean,
|
||||||
): Promise<any> { // tslint:disable-line:no-any
|
): 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[];
|
] as INodeProperties[];
|
||||||
|
|
|
@ -117,7 +117,7 @@ export class Reddit implements INodeType {
|
||||||
trophies: 'me/trophies',
|
trophies: 'me/trophies',
|
||||||
};
|
};
|
||||||
|
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoints[details]);
|
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
|
||||||
|
|
||||||
if (details === 'identity') {
|
if (details === 'identity') {
|
||||||
responseData = responseData.features;
|
responseData = responseData.features;
|
||||||
|
@ -145,7 +145,7 @@ export class Reddit implements INodeType {
|
||||||
body.resubmit = true;
|
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') {
|
} else if (type === 'best') {
|
||||||
|
|
||||||
const endpoint = 'best.json';
|
const returnAll = this.getNodeParameter('returnAll', i);
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
|
|
||||||
|
|
||||||
|
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