mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
Clean up auth parameter
This commit is contained in:
parent
49001ce789
commit
36c3fb2039
|
@ -10,7 +10,6 @@ import {
|
||||||
import {
|
import {
|
||||||
OptionsWithUri,
|
OptionsWithUri,
|
||||||
} from 'request';
|
} from 'request';
|
||||||
import { queryString } from '../TheHive/QueryFunctions';
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -22,15 +21,17 @@ export async function redditApiRequest(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
qs: IDataObject,
|
qs: IDataObject,
|
||||||
body: IDataObject,
|
body: IDataObject,
|
||||||
noAuth: boolean,
|
|
||||||
): Promise<any> { // tslint:disable-line:no-any
|
): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
|
||||||
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
|
const requiresAuth = ['myAccount', 'submission'].includes(resource);
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
headers: {
|
headers: {
|
||||||
'user-agent': 'n8n',
|
'user-agent': 'n8n',
|
||||||
},
|
},
|
||||||
method,
|
method,
|
||||||
uri: noAuth ? `https://www.reddit.com/${endpoint}` : `https://oauth.reddit.com/api/v1/${endpoint}`,
|
uri: requiresAuth ? `https://oauth.reddit.com/api/v1/${endpoint}` : `https://www.reddit.com/${endpoint}`,
|
||||||
qs,
|
qs,
|
||||||
body,
|
body,
|
||||||
json: true,
|
json: true,
|
||||||
|
@ -45,9 +46,10 @@ export async function redditApiRequest(
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
return noAuth
|
return requiresAuth
|
||||||
? await this.helpers.request.call(this, options)
|
? await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options)
|
||||||
: await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options);
|
: await this.helpers.request.call(this, options);
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.message) {
|
if (error.message) {
|
||||||
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]);
|
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]);
|
||||||
|
@ -67,14 +69,13 @@ export async function redditApiRequestAllItems(
|
||||||
endpoint: string,
|
endpoint: string,
|
||||||
qs: IDataObject,
|
qs: IDataObject,
|
||||||
body: IDataObject,
|
body: IDataObject,
|
||||||
noAuth: boolean,
|
|
||||||
): Promise<any> { // tslint:disable-line:no-any
|
): Promise<any> { // tslint:disable-line:no-any
|
||||||
|
|
||||||
let responseData;
|
let responseData;
|
||||||
const returnData: IDataObject[] = [];
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
do {
|
do {
|
||||||
responseData = await redditApiRequest.call(this, method, endpoint, qs, body, noAuth);
|
responseData = await redditApiRequest.call(this, method, endpoint, qs, body);
|
||||||
qs.after = responseData.after;
|
qs.after = responseData.after;
|
||||||
responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any
|
responseData.data.children.forEach((child: any) => returnData.push(child.data)); // tslint:disable-line:no-any
|
||||||
|
|
||||||
|
@ -99,13 +100,13 @@ export async function handleListing(
|
||||||
|
|
||||||
const returnAll = this.getNodeParameter('returnAll', i);
|
const returnAll = this.getNodeParameter('returnAll', i);
|
||||||
if (returnAll) {
|
if (returnAll) {
|
||||||
return await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {}, true);
|
return await redditApiRequestAllItems.call(this, 'GET', endpoint, {}, {});
|
||||||
}
|
}
|
||||||
|
|
||||||
const qs: IDataObject = {
|
const qs: IDataObject = {
|
||||||
limit: this.getNodeParameter('limit', i),
|
limit: this.getNodeParameter('limit', i),
|
||||||
};
|
};
|
||||||
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {}, true);
|
responseData = await redditApiRequestAllItems.call(this, 'GET', endpoint, qs, {});
|
||||||
responseData = responseData.splice(0, qs.limit);
|
responseData = responseData.splice(0, qs.limit);
|
||||||
|
|
||||||
return responseData;
|
return responseData;
|
||||||
|
|
|
@ -12,7 +12,6 @@ import {
|
||||||
import {
|
import {
|
||||||
handleListing,
|
handleListing,
|
||||||
redditApiRequest,
|
redditApiRequest,
|
||||||
redditApiRequestAllItems,
|
|
||||||
} from './GenericFunctions';
|
} from './GenericFunctions';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
@ -129,7 +128,7 @@ export class Reddit implements INodeType {
|
||||||
if (information === 'trending') {
|
if (information === 'trending') {
|
||||||
|
|
||||||
const endpoint = 'api/trending_subreddits.json';
|
const endpoint = 'api/trending_subreddits.json';
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {}, true);
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, {}, {});
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
|
@ -153,7 +152,7 @@ export class Reddit implements INodeType {
|
||||||
};
|
};
|
||||||
|
|
||||||
const details = this.getNodeParameter('details', i) as string;
|
const details = this.getNodeParameter('details', i) as string;
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {}, false);
|
responseData = await redditApiRequest.call(this, 'GET', endpoints[details], {}, {});
|
||||||
|
|
||||||
if (details === 'identity') {
|
if (details === 'identity') {
|
||||||
responseData = responseData.features;
|
responseData = responseData.features;
|
||||||
|
@ -181,7 +180,7 @@ export class Reddit implements INodeType {
|
||||||
body.resubmit = true;
|
body.resubmit = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body, false);
|
responseData = await redditApiRequest.call(this, 'POST', 'submit', {}, body);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -197,7 +196,7 @@ export class Reddit implements INodeType {
|
||||||
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
|
if (['about', 'rules', 'sidebar', 'sticky'].includes(content)) {
|
||||||
|
|
||||||
const endpoint = `r/${subreddit}/about/${content}.json`;
|
const endpoint = `r/${subreddit}/about/${content}.json`;
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {});
|
||||||
|
|
||||||
if (content === 'rules') {
|
if (content === 'rules') {
|
||||||
responseData = responseData.rules;
|
responseData = responseData.rules;
|
||||||
|
@ -217,7 +216,7 @@ export class Reddit implements INodeType {
|
||||||
query: this.getNodeParameter('keyword', i),
|
query: this.getNodeParameter('keyword', i),
|
||||||
};
|
};
|
||||||
|
|
||||||
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {}, true);
|
responseData = await redditApiRequest.call(this, 'GET', endpoint, qs, {});
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue