Improvements to #1083

This commit is contained in:
ricardo 2020-10-20 18:09:13 -04:00
parent f08d8c9375
commit bc887640fc
2 changed files with 233 additions and 142 deletions

View file

@ -46,16 +46,31 @@ export async function googleApiRequest(this: IExecuteFunctions | IExecuteSingleF
const { access_token } = await getAccessToken.call(this, credentials as IDataObject); const { access_token } = await getAccessToken.call(this, credentials as IDataObject);
options.headers!.Authorization = `Bearer ${access_token}`; options.headers!.Authorization = `Bearer ${access_token}`;
console.log(options);
//@ts-ignore //@ts-ignore
return await this.helpers.request(options); return await this.helpers.request(options);
} else { } else {
console.log(this.getCredentials('googleBooksOAuth2Api'));
//@ts-ignore //@ts-ignore
return await this.helpers.requestOAuth2.call(this, 'googleBooksOAuth2Api', options); return await this.helpers.requestOAuth2.call(this, 'googleBooksOAuth2Api', options);
} }
} catch (error) { } catch (error) {
if (error.response && error.response.body && error.response.body.message) { if (error.response && error.response.body && error.response.body.error) {
let errors;
if (error.response.body.error.errors) {
errors = error.response.body.error.errors;
errors = errors.map((e: IDataObject) => e.message).join('|');
} else {
errors = error.response.body.error.message;
}
// Try to return the error prettier // Try to return the error prettier
throw new Error(`Google Books error response [${error.statusCode}]: ${error.response.body.message}`); throw new Error(
`Google Books error response [${error.statusCode}]: ${errors}`
);
} }
throw error; throw error;
} }
@ -66,15 +81,13 @@ export async function googleApiRequestAllItems(this: IExecuteFunctions | ILoadOp
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
let responseData; let responseData;
query.maxResults = 100; query.maxResults = 40;
do { do {
responseData = await googleApiRequest.call(this, method, endpoint, body, query); responseData = await googleApiRequest.call(this, method, endpoint, body, query);
query.pageToken = responseData['nextPageToken']; returnData.push.apply(returnData, responseData[propertyName] || []);
returnData.push.apply(returnData, responseData[propertyName]);
} while ( } while (
responseData['nextPageToken'] !== undefined && returnData.length < responseData.totalItems
responseData['nextPageToken'] !== ''
); );
return returnData; return returnData;

View file

@ -4,6 +4,7 @@ import {
} from 'n8n-core'; } from 'n8n-core';
import { import {
IDataObject,
INodeExecutionData, INodeExecutionData,
INodeType, INodeType,
INodeTypeDescription, INodeTypeDescription,
@ -11,6 +12,7 @@ import {
import { import {
googleApiRequest, googleApiRequest,
googleApiRequestAllItems,
} from './GenericFunctions'; } from './GenericFunctions';
export interface IGoogleAuthCredentials { export interface IGoogleAuthCredentials {
@ -26,9 +28,10 @@ export class GoogleBooks implements INodeType {
group: ['input', 'output'], group: ['input', 'output'],
version: 1, version: 1,
description: 'Read data from Google Books', description: 'Read data from Google Books',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
defaults: { defaults: {
name: 'Google Books', name: 'Google Books',
color: '#0aa55c', color: '#4198e6',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
@ -81,17 +84,14 @@ export class GoogleBooks implements INodeType {
{ {
name: 'Bookshelf', name: 'Bookshelf',
value: 'bookshelf', value: 'bookshelf',
description: 'This resource allows you to view bookshelf metadata as well as to modify the contents of a bookshelf',
}, },
{ {
name: 'My Library', name: 'Bookshelf Volume',
value: 'library', value: 'bookshelfVolume',
description: 'This resource allows you to view and modify the contents of an authenticated users own bookshelf',
}, },
{ {
name: 'Volume', name: 'Volume',
value: 'volume', value: 'volume',
description: 'This resource is used to perform a search or listing the contents of a bookshelf and to retrieve volumes from another users public bookshelves',
}, },
], ],
default: 'bookshelf', default: 'bookshelf',
@ -104,18 +104,13 @@ export class GoogleBooks implements INodeType {
options: [ options: [
{ {
name: 'Get', name: 'Get',
value: 'getShelf', value: 'get',
description: 'Retrieve a specific bookshelf resource for the specified user', description: 'Retrieve a specific bookshelf resource for the specified user',
}, },
{ {
name: 'List', name: 'Get All',
value: 'listShelves', value: 'getAll',
description: 'Retrieve a list of public bookshelf resource for the specified user', description: 'Get all public bookshelf resource for the specified user',
},
{
name: 'Get volumes',
value: 'getVolumes',
description: 'Retrieve volumes in a specific bookshelf for the specified user',
}, },
], ],
displayOptions: { displayOptions: {
@ -125,7 +120,7 @@ export class GoogleBooks implements INodeType {
], ],
}, },
}, },
default: 'getShelf', default: 'get',
description: 'The operation to perform', description: 'The operation to perform',
}, },
{ {
@ -134,49 +129,39 @@ export class GoogleBooks implements INodeType {
type: 'options', type: 'options',
options: [ options: [
{ {
name: 'Add Volume', name: 'Add',
value: 'addVolume', value: 'add',
description: 'Adds a volume to a bookshelf', description: 'Add a volume to a bookshelf',
}, },
{ {
name: 'Clear Volumes', name: 'Clear',
value: 'clearVolumes', value: 'clear',
description: 'Clear all volumes from a bookshelf', description: 'Clears all volumes from a bookshelf',
}, },
{ {
name: 'Get', name: 'Get All',
value: 'getMyShelf', value: 'getAll',
description: 'Retrieve metadata for a specific bookshelf belonging to the authenticated user', description: 'Get all volumes in a specific bookshelf for the specified user',
}, },
{ {
name: 'List', name: 'Move',
value: 'listMyShelves', value: 'move',
description: 'Retrieve a list of bookshelves belonging to the authenticated user', description: 'Moves a volume within a bookshelf',
}, },
{ {
name: 'Move Volume', name: 'Remove',
value: 'moveVolume', value: 'remove',
description: 'Move a volume within a bookshelf', description: 'Removes a volume from a bookshelf',
},
{
name: 'Remove Volume',
value: 'removeVolume',
description: 'Remove a volume from a bookshelf',
},
{
name: 'List Volumes',
value: 'listVolumes',
description: 'Get volume information for volumes on a bookshelf',
}, },
], ],
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
'library', 'bookshelfVolume',
], ],
}, },
}, },
default: 'addVolume', default: 'getAll',
description: 'The operation to perform', description: 'The operation to perform',
}, },
{ {
@ -186,13 +171,13 @@ export class GoogleBooks implements INodeType {
options: [ options: [
{ {
name: 'Get', name: 'Get',
value: 'getVolume', value: 'get',
description: 'Retrieve a Volume resource based on ID', description: 'Get a volume resource based on ID',
}, },
{ {
name: 'List', name: 'Get All',
value: 'bookSearch', value: 'getAll',
description: 'Perform a book search', description: 'Get all volumes filtered by query',
}, },
], ],
displayOptions: { displayOptions: {
@ -202,9 +187,28 @@ export class GoogleBooks implements INodeType {
], ],
}, },
}, },
default: 'getVolume', default: 'get',
description: 'The operation to perform', description: 'The operation to perform',
}, },
{
displayName: 'My Library',
name: 'myLibrary',
type: 'boolean',
default: false,
required: true,
displayOptions: {
show: {
operation: [
'get',
'getAll',
],
resource: [
'bookshelf',
'bookshelfVolume'
],
},
},
},
// ---------------------------------- // ----------------------------------
// All // All
@ -219,7 +223,7 @@ export class GoogleBooks implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'bookSearch', 'getAll',
], ],
resource: [ resource: [
'volume', 'volume',
@ -237,12 +241,17 @@ export class GoogleBooks implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'getShelf', 'get',
'listShelves', 'getAll',
'getVolumes',
], ],
resource: [ resource: [
'bookshelf', 'bookshelf',
'bookshelfVolume',
],
},
hide: {
myLibrary: [
true,
], ],
}, },
}, },
@ -257,18 +266,15 @@ export class GoogleBooks implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'getShelf', 'get',
'getVolumes', 'add',
'addVolume', 'clear',
'clearVolumes', 'move',
'getMyShelf', 'remove',
'moveVolume',
'removeVolume',
'listVolumes',
], ],
resource: [ resource: [
'bookshelf', 'bookshelf',
'library', 'bookshelfVolume'
], ],
}, },
}, },
@ -283,15 +289,13 @@ export class GoogleBooks implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'getVolume', 'add',
'addVolume', 'move',
'moveVolume', 'remove',
'removeVolume', 'get',
], ],
resource: [ resource: [
'bookshelf', 'bookshelfVolume',
'library',
'volume',
], ],
}, },
}, },
@ -306,97 +310,171 @@ export class GoogleBooks implements INodeType {
displayOptions: { displayOptions: {
show: { show: {
operation: [ operation: [
'moveVolume', 'move',
], ],
resource: [ resource: [
'library', 'bookshelfVolume',
], ],
}, },
}, },
}, },
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: [
'getAll',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 40,
},
default: 40,
description: 'How many results to return.',
},
], ],
}; };
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 length = items.length as unknown as number;
const returnData: IDataObject[] = [];
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 = []; const qs: IDataObject = {};
let responseData;
for (let i=0; i < length; i++) { for (let i = 0; i < length; i++) {
if (resource === 'volume') { if (resource === 'volume') {
if (operation === 'getVolume') { if (operation === 'get') {
const volumeId = this.getNodeParameter('volumeId', i) as string; const volumeId = this.getNodeParameter('volumeId', i) as string;
responseData = await googleApiRequest.call(this, 'GET', `v1/volumes/${volumeId}`, {});
const response = await googleApiRequest.call(this, 'GET', `v1/volumes/${volumeId}`, {}); } else if (operation === 'getAll') {
responseData.push(response);
} else if (operation === 'bookSearch') {
const searchQuery = this.getNodeParameter('searchQuery', i) as string; const searchQuery = this.getNodeParameter('searchQuery', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const response = await googleApiRequest.call(this, 'GET', `v1/volumes?q=${searchQuery}`, {}); if (returnAll) {
responseData.push(response); responseData = await googleApiRequestAllItems.call(this, 'items', 'GET', `v1/volumes?q=${searchQuery}`, {});
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await googleApiRequest.call(this, 'GET', `v1/volumes?q=${searchQuery}`, {}, qs);
responseData = responseData.items || [];
}
} }
} else if (resource === 'bookshelf') { }
if (operation === 'getShelf') {
const userId = this.getNodeParameter('userId', i) as string; if (resource === 'bookshelf') {
if (operation === 'get') {
const shelfId = this.getNodeParameter('shelfId', i) as string; const shelfId = this.getNodeParameter('shelfId', i) as string;
const myLibrary = this.getNodeParameter('myLibrary', i) as boolean;
let endpoint;
if (myLibrary === false) {
const userId = this.getNodeParameter('userId', i) as string;
endpoint = `v1/users/${userId}/bookshelves/${shelfId}`;
} else {
endpoint = `v1/mylibrary/bookshelves/${shelfId}`;
}
const response = await googleApiRequest.call(this, 'GET', `v1/users/${userId}/bookshelves/${shelfId}`, {}); responseData = await googleApiRequest.call(this, 'GET', endpoint, {});
responseData.push(response); } else if (operation === 'getAll') {
} else if (operation === 'listShelves') { const myLibrary = this.getNodeParameter('myLibrary', i) as boolean;
const userId = this.getNodeParameter('userId', i) as string; const returnAll = this.getNodeParameter('returnAll', i) as boolean;
let endpoint;
const response = await googleApiRequest.call(this, 'GET', `v1/users/${userId}/bookshelves`, {}); if (myLibrary === false) {
responseData.push(response); const userId = this.getNodeParameter('userId', i) as string;
} else if (operation === 'getVolumes') { endpoint = `v1/users/${userId}/bookshelves`;
const userId = this.getNodeParameter('userId', i) as string; } else {
const shelfId = this.getNodeParameter('shelfId', i) as string; endpoint = `v1/mylibrary/bookshelves`;
}
const response = await googleApiRequest.call(this, 'GET', `v1/users/${userId}/bookshelves/${shelfId}/volumes`, {}); if (returnAll) {
responseData.push(response); responseData = await googleApiRequestAllItems.call(this, 'items', 'GET', endpoint, {});
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await googleApiRequest.call(this, 'GET', endpoint, {}, qs);
responseData = responseData.items || [];
}
} }
} else if (resource === 'library') { }
if (operation === 'addVolume') {
if (resource === 'bookshelfVolume') {
if (operation === 'add') {
const shelfId = this.getNodeParameter('shelfId', i) as string; const shelfId = this.getNodeParameter('shelfId', i) as string;
const volumeId = this.getNodeParameter('volumeId', i) as string; const volumeId = this.getNodeParameter('volumeId', i) as string;
const body: IDataObject = {
const response = await googleApiRequest.call(this, 'POST', `v1/mylibrary/bookshelves/${shelfId}/addVolume`, {volumeId}); volumeId,
responseData.push(response); };
} else if (operation === 'clearVolumes') { responseData = await googleApiRequest.call(this, 'POST', `mylibrary/bookshelves/${shelfId}/addVolume`, body);
const shelfId = this.getNodeParameter('shelfId', i) as string;
const response = await googleApiRequest.call(this, 'POST', `v1/mylibrary/bookshelves/${shelfId}/clearVolumes`, {});
responseData.push(response);
} else if (operation === 'getMyShelf') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const response = await googleApiRequest.call(this, 'GET', `v1/mylibrary/bookshelves/${shelfId}`, {});
responseData.push(response);
} else if (operation === 'listMyShelves') {
const response = await googleApiRequest.call(this, 'GET', `v1/mylibrary/bookshelves`, {});
responseData.push(response);
} else if (operation === 'moveVolume') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const volumeId = this.getNodeParameter('volumeId', i) as string;
const volumePosition = this.getNodeParameter('volumePosition', i) as string;
const response = await googleApiRequest.call(this, 'POST', `v1/mylibrary/bookshelves/${shelfId}/moveVolume`, {shelfId, volumeId, volumePosition});
responseData.push(response);
} else if (operation === 'removeVolume') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const volumeId = this.getNodeParameter('volumeId', i) as string;
const response = await googleApiRequest.call(this, 'POST', `v1/mylibrary/bookshelves/${shelfId}/removeVolume`, {shelfId, volumeId});
responseData.push(response);
} else if (operation === 'listVolumes') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const response = await googleApiRequest.call(this, 'GET', `v1/mylibrary/bookshelves/${shelfId}/volumes`, {});
responseData.push(response);
} }
if (operation === 'clear') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
responseData = await googleApiRequest.call(this, 'POST', `mylibrary/bookshelves/${shelfId}/clearVolumes`);
}
if (operation === 'getAll') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const myLibrary = this.getNodeParameter('myLibrary', i) as boolean;
let endpoint;
if (myLibrary === false) {
const userId = this.getNodeParameter('userId', i) as string;
endpoint = `v1/users/${userId}/bookshelves/${shelfId}/volumes`;
} else {
endpoint = `v1/mylibrary/bookshelves/${shelfId}/volumes`;
}
if (returnAll) {
responseData = await googleApiRequestAllItems.call(this, 'items', 'GET', endpoint, {});
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await googleApiRequest.call(this, 'GET', endpoint, {}, qs);
responseData = responseData.items || [];
}
}
if (operation === 'move') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const volumeId = this.getNodeParameter('volumeId', i) as string;
const volumePosition = this.getNodeParameter('volumePosition', i) as number;
const body: IDataObject = {
volumeId,
volumePosition,
};
responseData = await googleApiRequest.call(this, 'POST', `mylibrary/bookshelves/${shelfId}/moveVolume`, body);
}
if (operation === 'move') {
const shelfId = this.getNodeParameter('shelfId', i) as string;
const volumeId = this.getNodeParameter('volumeId', i) as string;
const body: IDataObject = {
volumeId,
};
responseData = await googleApiRequest.call(this, 'POST', `mylibrary/bookshelves/${shelfId}/removeVolume`, body);
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
} }
} }
return [this.helpers.returnJsonArray(responseData)]; return [this.helpers.returnJsonArray(responseData)];