add Google tasks API

This commit is contained in:
shraddha shaligram
2020-06-15 15:47:44 -07:00
parent 2ef5c8c836
commit 136596f465
9 changed files with 1429 additions and 481 deletions

View File

@@ -0,0 +1,83 @@
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
ILoadOptionsFunctions
} from 'n8n-core';
import { IDataObject } from 'n8n-workflow';
export async function googleApiRequest(
this: IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: any = {},
qs: IDataObject = {},
uri?: string,
headers: IDataObject = {}
): Promise<any> {
const options: OptionsWithUri = {
headers: {
'Content-Type': 'application/json'
},
method,
body,
qs,
uri: uri || `https://www.googleapis.com${resource}`,
json: true
};
try {
if (Object.keys(headers).length !== 0) {
options.headers = Object.assign({}, options.headers, headers);
}
if (Object.keys(body).length === 0) {
delete options.body;
}
//@ts-ignore
return await this.helpers.requestOAuth2.call(
this,
'googleTasksOAuth2Api',
options
);
} catch (error) {
if (error.response && error.response.body && error.response.body.message) {
// Try to return the error prettier
throw new Error(
`Google Tasks error response [${error.statusCode}]: ${error.response.body.message}`
);
}
throw error;
}
}
export async function googleApiRequestAllItems(
this: IExecuteFunctions | ILoadOptionsFunctions,
propertyName: string,
method: string,
endpoint: string,
body: any = {},
query: IDataObject = {}
): Promise<any> {
const returnData: IDataObject[] = [];
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;
}

View File

@@ -0,0 +1,290 @@
import { IExecuteFunctions } from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeTypeDescription,
INodeType,
ILoadOptionsFunctions,
INodePropertyOptions
} from 'n8n-workflow';
import { taskOperations, taskFields } from './TaskDescription';
import { googleApiRequest, googleApiRequestAllItems } from './GenericFunctions';
export class GoogleTasks implements INodeType {
description: INodeTypeDescription = {
displayName: 'Google Tasks',
name: 'googleTasks',
icon: 'file:googleTasks.png',
group: ['input'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Google Tasks API.',
defaults: {
name: 'Google Tasks',
color: '#3E87E4'
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'googleTasksOAuth2Api',
required: true
}
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Task',
value: 'task'
}
],
default: 'task',
description: 'The resource to operate on.'
},
...taskOperations,
...taskFields
]
};
methods = {
loadOptions: {
// Get all the tasklists to display them to user so that he can select them easily
async getTasks(
this: ILoadOptionsFunctions
): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const tasks = await googleApiRequestAllItems.call(
this,
'items',
'GET',
'/tasks/v1/users/@me/lists'
);
for (const task of tasks) {
const taskName = task.title;
const taskId = task.id;
returnData.push({
name: taskName,
value: taskId
});
}
return returnData;
}
}
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = (items.length as unknown) as number;
const qs: IDataObject = {};
let responseData;
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
let body: IDataObject = {};
for (let i = 0; i < length; i++) {
if (resource === 'task') {
if (operation === 'create') {
body = {};
//https://developers.google.com/tasks/v1/reference/tasks/insert
const taskId = this.getNodeParameter('task', i) as string;
const additionalFields = this.getNodeParameter(
'additionalFields',
i
) as IDataObject;
if (additionalFields.parent)
qs.parent = additionalFields.parent as string;
if (additionalFields.previous)
qs.previous = additionalFields.previous as string;
if (additionalFields.links) {
body.links = (additionalFields.links as string[]).map(link => {
return { link: link };
});
}
if (additionalFields.status)
body.status = additionalFields.status as string;
if (additionalFields.notes)
body.notes = additionalFields.notes as string;
if (additionalFields.title)
body.title = additionalFields.title as string;
if (additionalFields.dueDate)
body.dueDate = additionalFields.dueDate as string;
if (additionalFields.completed)
body.completed = additionalFields.completed as string;
if (additionalFields.deleted)
body.deleted = additionalFields.deleted as boolean;
if (additionalFields.hidden)
body.hidden = additionalFields.hidden as boolean;
if (additionalFields.position)
body.position = additionalFields.position as string;
if (additionalFields.selfLink)
body.selfLink = additionalFields.selfLink as string;
responseData = await googleApiRequest.call(
this,
'POST',
`/tasks/v1/lists/${taskId}/tasks`,
body,
qs
);
}
if (operation == 'delete') {
//https://developers.google.com/tasks/v1/reference/tasks/delete
const taskListId = this.getNodeParameter('task', i) as string;
const taskId = this.getNodeParameter('taskId', i) as string;
responseData = await googleApiRequest.call(
this,
'DELETE',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
{}
);
responseData = { success: true };
}
if (operation === 'get') {
//https://developers.google.com/tasks/v1/reference/tasks/get
const taskListId = this.getNodeParameter('task', i) as string;
const taskId = this.getNodeParameter('taskId', i) as string;
responseData = await googleApiRequest.call(
this,
'GET',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
{},
qs
);
}
if (operation === 'getAll') {
//https://developers.google.com/tasks/v1/reference/tasks/list
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
const taskListId = this.getNodeParameter('task', i) as string;
const options = this.getNodeParameter(
'additionalFields',
i
) as IDataObject;
if (options.completedMax) {
qs.completedMax = options.completedMax as string;
}
if (options.completedMin) {
qs.completedMin = options.completedMin as string;
}
if (options.dueMin) {
qs.dueMin = options.dueMin as string;
}
if (options.dueMax) {
qs.dueMax = options.dueMax as string;
}
if (options.pageToken) {
qs.pageToken = options.pageToken as string;
}
if (options.showCompleted) {
qs.showCompleted = options.showCompleted as boolean;
}
if (options.showDeleted) {
qs.showDeleted = options.showDeleted as boolean;
}
if (options.showHidden) {
qs.showHidden = options.showHidden as boolean;
}
if (options.updatedMin) {
qs.updatedMin = options.updatedMin as string;
}
if (returnAll) {
responseData = await googleApiRequestAllItems.call(
this,
'items',
'GET',
`/tasks/v1/lists/${taskListId}/tasks`,
{},
qs
);
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await googleApiRequest.call(
this,
'GET',
`/tasks/v1/lists/${taskListId}/tasks`,
{},
qs
);
responseData = responseData.items;
}
}
if (operation == 'update') {
body = {};
//https://developers.google.com/tasks/v1/reference/tasks/patch
const taskListId = this.getNodeParameter('task', i) as string;
const taskId = this.getNodeParameter('taskId', i) as string;
const updateFields = this.getNodeParameter(
'updateFields',
i
) as IDataObject;
if (updateFields.parent) qs.parent = updateFields.parent as string;
if (updateFields.previous)
qs.previous = updateFields.previous as string;
if (updateFields.links) {
body.links = (updateFields.links as string[]).map(link => {
return { link: link };
});
}
if (updateFields.status) body.status = updateFields.status as string;
if (updateFields.notes) body.notes = updateFields.notes as string;
if (updateFields.title) body.title = updateFields.title as string;
if (updateFields.dueDate)
body.dueDate = updateFields.dueDate as string;
if (updateFields.completed)
body.completed = updateFields.completed as string;
if (updateFields.deleted)
body.deleted = updateFields.deleted as boolean;
if (updateFields.hidden) body.hidden = updateFields.hidden as boolean;
if (updateFields.position)
body.position = updateFields.position as string;
if (updateFields.selfLink)
body.selfLink = updateFields.selfLink as string;
responseData = await googleApiRequest.call(
this,
'PATCH',
`/tasks/v1/lists/${taskListId}/tasks/${taskId}`,
body,
qs
);
}
}
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else if (responseData !== undefined) {
returnData.push(responseData as IDataObject);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View File

@@ -0,0 +1,539 @@
import { INodeProperties } from 'n8n-workflow';
export const taskOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: ['task']
}
},
options: [
{
name: 'Create',
value: 'create',
description: 'Add a task to tasklist'
},
{
name: 'Delete',
value: 'delete',
description: 'Delete a task'
},
{
name: 'Get',
value: 'get',
description: 'Retrieve a task'
},
{
name: 'Get All',
value: 'getAll',
description: 'Retrieve all tasks from a tasklist'
},
{
name: 'Update',
value: 'update',
description: 'Update a task'
}
],
default: 'create',
description: 'The operation to perform.'
}
] as INodeProperties[];
export const taskFields = [
/* -------------------------------------------------------------------------- */
/* task:create */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
name: 'task',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTasks'
},
required: true,
displayOptions: {
show: {
operation: ['create'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: ['create'],
resource: ['task']
}
},
options: [
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'needs Action',
value: 'needsAction'
},
{
name: 'completed',
value: 'completed'
}
],
default: '',
description: 'Current status of the task.'
},
{
displayName: 'Links',
name: 'links',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Link'
},
default: '',
description: 'The links to insert in the task.'
},
{
displayName: 'Notes',
name: 'notes',
type: 'string',
default: '',
description: 'Additional Notes.'
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
description: 'Title of the task.'
},
{
displayName: 'Due Date',
name: 'dueDate',
type: 'dateTime',
default: '',
description: 'Due date of the task.'
},
{
displayName: 'Completion date',
name: 'completed',
type: 'dateTime',
default: '',
description: `Completion date of the task (as a RFC 3339 timestamp). This field is omitted if the task has not been completed.`
},
{
displayName: 'Deleted status',
name: 'deleted',
type: 'boolean',
default: false,
description: 'Flag indicating whether the task has been deleted.'
},
{
displayName: 'Hidden',
name: 'hidden',
type: 'boolean',
default: false,
description: 'Flag indicating whether the task is hidden.'
},
{
displayName: 'Parent',
name: 'parent',
type: 'string',
default: '',
description:
'Parent task identifier.This field is omitted if it is a top-level task.'
},
{
displayName: 'Position',
name: 'position',
type: 'string',
default: '',
description:
'Parent task identifier.This field is omitted if it is a top-level task.'
},
{
displayName: 'Self Link',
name: 'selfLink',
type: 'string',
default: '',
description:
'URL pointing to this task. Used to retrieve, update, or delete this task.'
},
{
displayName: 'Previous',
name: 'previous',
type: 'string',
default: '',
description:
'Previous sibling task identifier. If the task is created at the first position among its siblings, this parameter is omitted.'
}
]
},
/* -------------------------------------------------------------------------- */
/* task:delete */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
name: 'task',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTasks'
},
required: true,
displayOptions: {
show: {
operation: ['delete'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Task ID',
name: 'taskId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: ['delete'],
resource: ['task']
}
},
default: ''
},
/* -------------------------------------------------------------------------- */
/* task:get */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
name: 'task',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTasks'
},
required: true,
displayOptions: {
show: {
operation: ['get'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Task ID',
name: 'taskId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: ['get'],
resource: ['task']
}
},
default: ''
},
/* -------------------------------------------------------------------------- */
/* task:getAll */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
name: 'task',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTasks'
},
required: true,
displayOptions: {
show: {
operation: ['getAll'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
operation: ['getAll'],
resource: ['task']
}
},
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'],
resource: ['task'],
returnAll: [false]
}
},
typeOptions: {
minValue: 1,
maxValue: 100
},
default: 20,
description: 'How many results to return.'
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
type: 'collection',
placeholder: 'Add Field',
default: {},
displayOptions: {
show: {
operation: ['getAll'],
resource: ['task']
}
},
options: [
{
displayName: 'Completed Max',
name: 'completedMax',
type: 'dateTime',
default: '',
description:
'Upper bound for a task completion date (as a RFC 3339 timestamp) to filter by.'
},
{
displayName: 'Completed Min',
name: 'completedMin',
type: 'dateTime',
default: '',
description:
'Lower bound for a task completion date (as a RFC 3339 timestamp) to filter by.'
},
{
displayName: 'Due Min',
name: 'dueMin',
type: 'dateTime',
default: '',
description:
'Lower bound for a task due date (as a RFC 3339 timestamp) to filter by.'
},
{
displayName: 'Due Max',
name: 'dueMax',
type: 'dateTime',
default: '',
description:
'Upper bound for a task due date (as a RFC 3339 timestamp) to filter by.'
},
{
displayName: 'Page Token',
name: 'pageToken',
type: 'string',
default: '',
description: 'Token specifying the result page to return.'
},
{
displayName: 'Show Completed',
name: 'showCompleted',
type: 'boolean',
default: true,
description:
'Flag indicating whether completed tasks are returned in the result'
},
{
displayName: 'Show Deleted',
name: 'showDeleted',
type: 'boolean',
default: false,
description:
'Flag indicating whether deleted tasks are returned in the result'
},
{
displayName: 'Show Hidden',
name: 'showHidden',
type: 'boolean',
default: false,
description:
'Flag indicating whether hidden tasks are returned in the result'
},
{
displayName: 'updated Min',
name: 'updatedMin',
type: 'string',
description:
'Lower bound for a task last modification time (as a RFC 3339 timestamp) to filter by.'
}
]
},
/* -------------------------------------------------------------------------- */
/* task:update */
/* -------------------------------------------------------------------------- */
{
displayName: 'TaskList',
name: 'task',
type: 'options',
typeOptions: {
loadOptionsMethod: 'getTasks'
},
required: true,
displayOptions: {
show: {
operation: ['update'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Task ID',
name: 'taskId',
type: 'string',
required: true,
displayOptions: {
show: {
operation: ['update'],
resource: ['task']
}
},
default: ''
},
{
displayName: 'Update Fields',
name: 'updateFields',
type: 'collection',
placeholder: 'Update Field',
default: {},
displayOptions: {
show: {
operation: ['update'],
resource: ['task']
}
},
options: [
{
displayName: 'Status',
name: 'status',
type: 'options',
options: [
{
name: 'needs Action',
value: 'needsAction'
},
{
name: 'completed',
value: 'completed'
}
],
default: '',
description: 'Current status of the task.'
},
{
displayName: 'Links',
name: 'links',
type: 'string',
typeOptions: {
multipleValues: true,
multipleValueButtonText: 'Add Link'
},
default: '',
description: 'The links to insert in the task.'
},
{
displayName: 'Notes',
name: 'notes',
type: 'string',
default: '',
description: 'Additional Notes.'
},
{
displayName: 'Title',
name: 'title',
type: 'string',
default: '',
description: 'Title of the task.'
},
{
displayName: 'Due Date',
name: 'dueDate',
type: 'dateTime',
default: '',
description: 'Due date of the task.'
},
{
displayName: 'Completion date',
name: 'completed',
type: 'dateTime',
default: '',
description: `Completion date of the task (as a RFC 3339 timestamp). This field is omitted if the task has not been completed.`
},
{
displayName: 'Deleted status',
name: 'deleted',
type: 'boolean',
default: false,
description: 'Flag indicating whether the task has been deleted.'
},
{
displayName: 'Hidden',
name: 'hidden',
type: 'boolean',
default: false,
description: 'Flag indicating whether the task is hidden.'
},
{
displayName: 'Parent',
name: 'parent',
type: 'string',
default: '',
description:
'Parent task identifier.This field is omitted if it is a top-level task.'
},
{
displayName: 'Position',
name: 'position',
type: 'string',
default: '',
description:
'Parent task identifier.This field is omitted if it is a top-level task.'
},
{
displayName: 'Self Link',
name: 'selfLink',
type: 'string',
default: '',
description:
'URL pointing to this task. Used to retrieve, update, or delete this task.'
},
{
displayName: 'Previous',
name: 'previous',
type: 'string',
default: '',
description:
'Previous sibling task identifier. If the task is created at the first position among its siblings, this parameter is omitted.'
}
]
}
] as INodeProperties[];

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB