Add Account operations and OAuth2 request

This commit is contained in:
Iván Ovejero 2021-01-15 20:15:32 -03:00
parent f24d7e5dc2
commit 97c04df938
2 changed files with 74 additions and 16 deletions

View file

@ -5,6 +5,7 @@ import {
import {
IDataObject,
IOAuth2Options,
} from 'n8n-workflow';
import {
@ -28,9 +29,12 @@ export async function redditApiRequest(
qs?: IDataObject,
): Promise<any> { // tslint:disable-line:no-any
const options: OptionsWithUri = {
headers: {
'user-agent': 'n8n',
},
method,
qs,
uri: `https://www.reddit.com/api/v1/${endpoint}`,
uri: `https://oauth.reddit.com/api/v1/${endpoint}`,
json: true,
};
@ -38,13 +42,17 @@ export async function redditApiRequest(
delete options.qs;
}
console.log(options);
const oAuth2Options: IOAuth2Options = {
tokenType: 'Bearer',
};
try {
return await this.helpers.request!(options);
return await this.helpers.requestOAuth2.call(this, 'redditOAuth2Api', options, oAuth2Options);
} catch (error) {
// ...
if (error.message) {
const errorObject = JSON.parse(error.message.match(/{.*}/)[0]);
throw new Error(`Reddit error response [${errorObject.error}]: ${errorObject.message}`);
}
throw error;
}
}

View file

@ -69,12 +69,37 @@ export class Reddit implements INodeType {
},
options: [
{
name: 'Get self',
value: 'getSelf',
description: 'Return the identity of the user',
name: 'Get myself',
value: 'getMyself',
description: 'Return the identity of the logged-in user',
},
{
name: 'Get blocked users',
value: 'getMyBlockedUsers',
description: 'Return the identity of the logged-in user',
},
{
name: 'Get my friends',
value: 'getMyFriends',
description: 'Return a list of friends for the logged-in user',
},
{
name: 'Get my karma',
value: 'getMyKarma',
description: 'Return a breakdown of subreddit karma',
},
{
name: 'Get my preferences',
value: 'getMyPrefs',
description: 'Return the preference settings of the logged-in user',
},
{
name: 'Get my trophies',
value: 'getMyTrophies',
description: 'Return a list of trophies for the logged-in user',
},
],
default: 'getSelf',
default: 'getMyself',
description: 'Operation to perform',
},
],
@ -82,24 +107,49 @@ export class Reddit implements INodeType {
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let responseData;
const returnData: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
if (resource === 'account') {
if (operation === 'getSelf') {
const requestMethod = 'GET';
const endpoint = 'me';
const responseData = await redditApiRequest.call(this, requestMethod, endpoint);
console.log(responseData);
returnData.push(responseData as IDataObject);
if (operation === 'getMyself') {
responseData = await redditApiRequest.call(this, 'GET', 'me');
responseData = responseData.features;
} else if (operation === 'getMyBlockedUsers') {
responseData = await redditApiRequest.call(this, 'GET', 'me/blocked');
} else if (operation === 'getMyFriends') {
responseData = await redditApiRequest.call(this, 'GET', 'me/friends');
} else if (operation === 'getMyKarma') {
responseData = await redditApiRequest.call(this, 'GET', 'me/karma');
} else if (operation === 'getMyPrefs') {
responseData = await redditApiRequest.call(this, 'GET', 'me/prefs');
} else if (operation === 'getMyTrophies') {
responseData = await redditApiRequest.call(this, 'GET', 'me/trophies');
}
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];