mirror of
https://github.com/n8n-io/n8n.git
synced 2025-03-05 20:50:17 -08:00
🔨 Refactor Google Slides node
This commit is contained in:
parent
fd8d537276
commit
1cc820eb73
|
@ -16,71 +16,62 @@ import * as moment from 'moment-timezone';
|
||||||
|
|
||||||
import * as jwt from 'jsonwebtoken';
|
import * as jwt from 'jsonwebtoken';
|
||||||
|
|
||||||
export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, qs: IDataObject = {}, uri?: string, headers: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
export async function googleApiRequest(
|
||||||
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||||
|
method: string,
|
||||||
|
resource: string,
|
||||||
|
body: IDataObject = {},
|
||||||
|
qs: IDataObject = {},
|
||||||
|
) {
|
||||||
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
|
const authenticationMethod = this.getNodeParameter('authentication', 0, 'serviceAccount') as string;
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri & { headers: IDataObject } = {
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json',
|
'Content-Type': 'application/json',
|
||||||
},
|
},
|
||||||
method,
|
method,
|
||||||
body,
|
body,
|
||||||
qs,
|
qs,
|
||||||
uri: uri || `https://slides.googleapis.com${resource}`,
|
uri: `https://slides.googleapis.com/v1/presentations${resource}`,
|
||||||
json: true
|
json: true,
|
||||||
};
|
};
|
||||||
try {
|
|
||||||
if (Object.keys(headers).length !== 0) {
|
if (!Object.keys(body).length) {
|
||||||
options.headers = Object.assign({}, options.headers, headers);
|
|
||||||
}
|
|
||||||
if (Object.keys(body).length === 0) {
|
|
||||||
delete options.body;
|
delete options.body;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!Object.keys(qs).length) {
|
||||||
|
delete options.qs;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
|
||||||
if (authenticationMethod === 'serviceAccount') {
|
if (authenticationMethod === 'serviceAccount') {
|
||||||
const credentials = this.getCredentials('googleApi');
|
|
||||||
|
|
||||||
if (credentials === undefined) {
|
const credentials = this.getCredentials('googleApi') as { access_token: string, email: string, privateKey: string };
|
||||||
throw new Error('No credentials got returned!');
|
const { access_token } = await getAccessToken.call(this, credentials);
|
||||||
}
|
options.headers.Authorization = `Bearer ${access_token}`;
|
||||||
|
return await this.helpers.request!(options);
|
||||||
|
|
||||||
const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
|
|
||||||
|
|
||||||
options.headers!.Authorization = `Bearer ${access_token}`;
|
|
||||||
//@ts-ignore
|
|
||||||
return await this.helpers.request(options);
|
|
||||||
} else {
|
} else {
|
||||||
//@ts-ignore
|
|
||||||
return await this.helpers.requestOAuth2.call(this, 'googleSlidesOAuth2Api', options);
|
return await this.helpers.requestOAuth2!.call(this, 'googleSlidesOAuth2Api', options);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response && error.response.body && error.response.body.message) {
|
|
||||||
// Try to return the error prettier
|
if (error?.response?.body?.message) {
|
||||||
throw new Error(`Google Slides error response [${error.statusCode}]: ${error.response.body.message}`);
|
throw new Error(`Google Slides error response [${error.statusCode}]: ${error.response.body.message}`);
|
||||||
}
|
}
|
||||||
throw error;
|
throw error;
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, method: string, endpoint: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
function getAccessToken(
|
||||||
|
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
|
||||||
const returnData: IDataObject[] = [];
|
{ email, privateKey }: { email: string, privateKey: string },
|
||||||
|
) {
|
||||||
let responseData;
|
|
||||||
query.maxResults = 100;
|
|
||||||
|
|
||||||
do {
|
|
||||||
responseData = await googleApiRequest.call(this, method, endpoint, body, query);
|
|
||||||
query.pageToken = responseData['nextPageToken'];
|
|
||||||
returnData.push.apply(returnData, responseData[propertyName]);
|
|
||||||
} while (
|
|
||||||
responseData['nextPageToken'] !== undefined &&
|
|
||||||
responseData['nextPageToken'] !== ''
|
|
||||||
);
|
|
||||||
|
|
||||||
return returnData;
|
|
||||||
}
|
|
||||||
|
|
||||||
function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, credentials: IDataObject): Promise<IDataObject> {
|
|
||||||
// https://developers.google.com/identity/protocols/oauth2/service-account#httprest
|
// https://developers.google.com/identity/protocols/oauth2/service-account#httprest
|
||||||
|
|
||||||
const scopes = [
|
const scopes = [
|
||||||
|
@ -93,22 +84,22 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa
|
||||||
|
|
||||||
const signature = jwt.sign(
|
const signature = jwt.sign(
|
||||||
{
|
{
|
||||||
'iss': credentials.email as string,
|
iss: email,
|
||||||
'sub': credentials.email as string,
|
sub: email,
|
||||||
'scope': scopes.join(' '),
|
scope: scopes.join(' '),
|
||||||
'aud': `https://oauth2.googleapis.com/token`,
|
aud: 'https://oauth2.googleapis.com/token',
|
||||||
'iat': now,
|
iat: now,
|
||||||
'exp': now + 3600,
|
exp: now + 3600,
|
||||||
},
|
},
|
||||||
credentials.privateKey as string,
|
privateKey,
|
||||||
{
|
{
|
||||||
algorithm: 'RS256',
|
algorithm: 'RS256',
|
||||||
header: {
|
header: {
|
||||||
'kid': credentials.privateKey as string,
|
kid: privateKey,
|
||||||
'typ': 'JWT',
|
typ: 'JWT',
|
||||||
'alg': 'RS256',
|
alg: 'RS256',
|
||||||
|
},
|
||||||
},
|
},
|
||||||
}
|
|
||||||
);
|
);
|
||||||
|
|
||||||
const options: OptionsWithUri = {
|
const options: OptionsWithUri = {
|
||||||
|
@ -121,9 +112,8 @@ function getAccessToken(this: IExecuteFunctions | IExecuteSingleFunctions | ILoa
|
||||||
assertion: signature,
|
assertion: signature,
|
||||||
},
|
},
|
||||||
uri: 'https://oauth2.googleapis.com/token',
|
uri: 'https://oauth2.googleapis.com/token',
|
||||||
json: true
|
json: true,
|
||||||
};
|
};
|
||||||
|
|
||||||
//@ts-ignore
|
return this.helpers.request!(options);
|
||||||
return this.helpers.request(options);
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
|
|
||||||
import {
|
import {
|
||||||
IExecuteFunctions,
|
IExecuteFunctions,
|
||||||
} from 'n8n-core';
|
} from 'n8n-core';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
|
IDataObject,
|
||||||
INodeExecutionData,
|
INodeExecutionData,
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
|
@ -26,7 +26,7 @@ export class GoogleSlides implements INodeType {
|
||||||
group: ['input', 'output'],
|
group: ['input', 'output'],
|
||||||
version: 1,
|
version: 1,
|
||||||
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
|
||||||
description: 'Read data from Google Slides',
|
description: 'Consume the Google Slides API',
|
||||||
defaults: {
|
defaults: {
|
||||||
name: 'Google Slides',
|
name: 'Google Slides',
|
||||||
color: '#edba25',
|
color: '#edba25',
|
||||||
|
@ -63,14 +63,14 @@ export class GoogleSlides implements INodeType {
|
||||||
name: 'authentication',
|
name: 'authentication',
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
|
||||||
name: 'Service Account',
|
|
||||||
value: 'serviceAccount',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'OAuth2',
|
name: 'OAuth2',
|
||||||
value: 'oAuth2',
|
value: 'oAuth2',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Service Account',
|
||||||
|
value: 'serviceAccount',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: 'serviceAccount',
|
default: 'serviceAccount',
|
||||||
},
|
},
|
||||||
|
@ -79,17 +79,17 @@ export class GoogleSlides implements INodeType {
|
||||||
name: 'resource',
|
name: 'resource',
|
||||||
type: 'options',
|
type: 'options',
|
||||||
options: [
|
options: [
|
||||||
{
|
|
||||||
name: 'Presentation',
|
|
||||||
value: 'presentation',
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
name: 'Page',
|
name: 'Page',
|
||||||
value: 'page',
|
value: 'page',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Presentation',
|
||||||
|
value: 'presentation',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
default: 'presentation',
|
default: 'presentation',
|
||||||
description: 'The resource to operate on',
|
description: 'Resource to operate on',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Operation',
|
displayName: 'Operation',
|
||||||
|
@ -106,6 +106,11 @@ export class GoogleSlides implements INodeType {
|
||||||
value: 'get',
|
value: 'get',
|
||||||
description: 'Get a presentation',
|
description: 'Get a presentation',
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: 'Get Slides',
|
||||||
|
value: 'getSlides',
|
||||||
|
description: 'Get presentation slides',
|
||||||
|
},
|
||||||
],
|
],
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
@ -115,7 +120,7 @@ export class GoogleSlides implements INodeType {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: 'create',
|
default: 'create',
|
||||||
description: 'The operation to perform',
|
description: 'Operation to perform',
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Operation',
|
displayName: 'Operation',
|
||||||
|
@ -141,109 +146,153 @@ export class GoogleSlides implements INodeType {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
default: 'get',
|
default: 'get',
|
||||||
description: 'The operation to perform',
|
description: 'Operation to perform',
|
||||||
},
|
},
|
||||||
|
|
||||||
// ----------------------------------
|
|
||||||
// All
|
|
||||||
// ----------------------------------
|
|
||||||
{
|
{
|
||||||
displayName: 'Title',
|
displayName: 'Title',
|
||||||
name: 'title',
|
name: 'title',
|
||||||
|
description: 'Title of the presentation to create.',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
required: true,
|
required: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
|
||||||
'create',
|
|
||||||
],
|
|
||||||
resource: [
|
resource: [
|
||||||
'presentation',
|
'presentation',
|
||||||
],
|
],
|
||||||
|
operation: [
|
||||||
|
'create',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Presentation ID',
|
displayName: 'Presentation ID',
|
||||||
name: 'presentationId',
|
name: 'presentationId',
|
||||||
|
description: 'ID of the presentation to retrieve. Found in the presentation URL:<br><code>https://docs.google.com/presentation/d/PRESENTATION_ID/edit</code>',
|
||||||
|
placeholder: '1wZtNFZ8MO-WKrxhYrOLMvyiqSgFwdSz5vn8_l_7eNqw',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
required: true,
|
required: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
operation: [
|
|
||||||
'get',
|
|
||||||
'getThumbnail'
|
|
||||||
],
|
|
||||||
resource: [
|
resource: [
|
||||||
'presentation',
|
'presentation',
|
||||||
'page',
|
'page',
|
||||||
],
|
],
|
||||||
|
operation: [
|
||||||
|
'get',
|
||||||
|
'getThumbnail',
|
||||||
|
'getSlides',
|
||||||
|
],
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
displayName: 'Page Object ID',
|
displayName: 'Page Object ID',
|
||||||
name: 'pageObjectId',
|
name: 'pageObjectId',
|
||||||
|
description: 'ID of the page object to retrieve.',
|
||||||
type: 'string',
|
type: 'string',
|
||||||
default: '',
|
default: '',
|
||||||
required: true,
|
required: true,
|
||||||
displayOptions: {
|
displayOptions: {
|
||||||
show: {
|
show: {
|
||||||
|
resource: [
|
||||||
|
'page',
|
||||||
|
],
|
||||||
operation: [
|
operation: [
|
||||||
'get',
|
'get',
|
||||||
'getThumbnail',
|
'getThumbnail',
|
||||||
],
|
],
|
||||||
resource: [
|
|
||||||
'page',
|
|
||||||
],
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const length = items.length as unknown as number;
|
|
||||||
|
|
||||||
const resource = this.getNodeParameter('resource', 0) as string;
|
const resource = this.getNodeParameter('resource', 0) as string;
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
const responseData = [];
|
|
||||||
|
|
||||||
for (let i=0; i < length; i++) {
|
let responseData;
|
||||||
if (resource === 'presentation') {
|
const returnData: IDataObject[] = [];
|
||||||
|
|
||||||
|
for (let i = 0; i < items.length; i++) {
|
||||||
|
|
||||||
|
if (resource === 'page') {
|
||||||
|
|
||||||
|
// *********************************************************************
|
||||||
|
// page
|
||||||
|
// *********************************************************************
|
||||||
|
|
||||||
|
if (operation === 'get') {
|
||||||
|
|
||||||
|
// ----------------------------------
|
||||||
|
// page: get
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
||||||
|
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
|
||||||
|
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}/pages/${pageObjectId}`);
|
||||||
|
|
||||||
|
} else if (operation === 'getThumbnail') {
|
||||||
|
|
||||||
|
// ----------------------------------
|
||||||
|
// page: getThumbnail
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
||||||
|
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
|
||||||
|
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}/pages/${pageObjectId}/thumbnail`);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if (resource === 'presentation') {
|
||||||
|
|
||||||
|
// *********************************************************************
|
||||||
|
// presentation
|
||||||
|
// *********************************************************************
|
||||||
|
|
||||||
if (operation === 'create') {
|
if (operation === 'create') {
|
||||||
const title = this.getNodeParameter('title', i) as string;
|
|
||||||
let body = {
|
// ----------------------------------
|
||||||
"title": title
|
// presentation: create
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const body = {
|
||||||
|
title: this.getNodeParameter('title', i) as string,
|
||||||
};
|
};
|
||||||
|
|
||||||
const response = await googleApiRequest.call(this, 'POST', `/v1/presentations`, body);
|
responseData = await googleApiRequest.call(this, 'POST', '', body);
|
||||||
responseData.push(response);
|
|
||||||
} else if (operation === 'get') {
|
} else if (operation === 'get') {
|
||||||
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
|
||||||
|
|
||||||
const response = await googleApiRequest.call(this, 'GET', `/v1/presentations/${presentationId}`, {});
|
// ----------------------------------
|
||||||
responseData.push(response);
|
// presentation: get
|
||||||
}
|
// ----------------------------------
|
||||||
} else if (resource === 'page') {
|
|
||||||
if (operation === 'get') {
|
|
||||||
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
|
||||||
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
|
|
||||||
|
|
||||||
const response = await googleApiRequest.call(this, 'GET', `/v1/presentations/${presentationId}/pages/${pageObjectId}`, {});
|
|
||||||
responseData.push(response);
|
|
||||||
} else if (operation === 'getThumbnail') {
|
|
||||||
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
||||||
const pageObjectId = this.getNodeParameter('pageObjectId', i) as string;
|
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}`);
|
||||||
|
|
||||||
|
} else if (operation === 'getSlides') {
|
||||||
|
|
||||||
|
// ----------------------------------
|
||||||
|
// presentation: getSlides
|
||||||
|
// ----------------------------------
|
||||||
|
|
||||||
|
const presentationId = this.getNodeParameter('presentationId', i) as string;
|
||||||
|
responseData = await googleApiRequest.call(this, 'GET', `/${presentationId}`, {}, { fields: 'slides' });
|
||||||
|
|
||||||
const response = await googleApiRequest.call(this, 'GET', `/v1/presentations/${presentationId}/pages/${pageObjectId}/thumbnail`, {});
|
|
||||||
responseData.push(response);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Array.isArray(responseData)
|
||||||
|
? returnData.push(...responseData)
|
||||||
|
: returnData.push(responseData);
|
||||||
|
|
||||||
}
|
}
|
||||||
return [this.helpers.returnJsonArray(responseData)];
|
return [this.helpers.returnJsonArray(responseData)];
|
||||||
}
|
}
|
||||||
|
|
|
@ -1 +1 @@
|
||||||
<svg height="512" viewBox="0 0 512 512" width="512" xmlns="http://www.w3.org/2000/svg"><path d="M440.8 140.2V479c0 18.2-14.8 33-33 33H102.9c-18.2 0-33-14.8-33-33V33c0-18.2 14.8-33 33-33h197.6z" fill="#f5b912"/><path d="M323.6 233.5H189.1c-12.5 0-22.6 10.1-22.6 22.6v134.5c0 12.5 10.1 22.6 22.6 22.6h134.5c12.5 0 22.6-10.1 22.6-22.6V256c0-12.4-10.1-22.5-22.6-22.5zm-.7 139.5h-133v-90.5h133z" fill="#fff"/><path d="M319.6 134.4l121.1 98.1v-92.2L372 100.5z" opacity=".19"/><path d="M441.4 140.2H334.2c-18.2 0-33-14.8-33-33V0z" fill="#fadc87"/></svg>
|
<svg viewBox="-60 -60 630 630" xmlns="http://www.w3.org/2000/svg"><path d="M440.8 140.2V479c0 18.2-14.8 33-33 33H102.9c-18.2 0-33-14.8-33-33V33c0-18.2 14.8-33 33-33h197.6z" fill="#f5b912"/><path d="M323.6 233.5H189.1c-12.5 0-22.6 10.1-22.6 22.6v134.5c0 12.5 10.1 22.6 22.6 22.6h134.5c12.5 0 22.6-10.1 22.6-22.6V256c0-12.4-10.1-22.5-22.6-22.5zm-.7 139.5h-133v-90.5h133z" fill="#fff"/><path d="M319.6 134.4l121.1 98.1v-92.2L372 100.5z" opacity=".19"/><path d="M441.4 140.2H334.2c-18.2 0-33-14.8-33-33V0z" fill="#fadc87"/></svg>
|
||||||
|
|
Before Width: | Height: | Size: 546 B After Width: | Height: | Size: 526 B |
Loading…
Reference in a new issue