Add Oura node (#1609)

*  Add Oura node

* 🔨 Make it work

*  Improvements

*  Improvements

* 🎨 Fix SVG size and position

*  Fix parameter error & other improvements

Co-authored-by: Iván Ovejero <ivov.src@gmail.com>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
Co-authored-by: Jan Oberhauser <jan.oberhauser@gmail.com>
This commit is contained in:
Tanay Pant 2021-04-02 18:29:20 +02:00 committed by GitHub
parent bbf875cd08
commit 45c0d6598f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 409 additions and 0 deletions

View file

@ -0,0 +1,18 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class OuraApi implements ICredentialType {
name = 'ouraApi';
displayName = 'Oura API';
documentationUrl = 'oura';
properties = [
{
displayName: 'Personal Access Token',
name: 'accessToken',
type: 'string' as NodePropertyTypes,
default: '',
},
];
}

View file

@ -0,0 +1,63 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
IExecuteSingleFunctions,
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} from 'n8n-workflow';
export async function ouraApiRequest(
this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions,
method: string,
resource: string,
body: IDataObject = {},
qs: IDataObject = {},
uri?: string,
option: IDataObject = {},
) {
const credentials = this.getCredentials('ouraApi');
if (credentials === undefined) {
throw new Error('No credentials got returned!');
}
let options: OptionsWithUri = {
headers: {
Authorization: `Bearer ${credentials.accessToken}`,
},
method,
qs,
body,
uri: uri || `https://api.ouraring.com/v1${resource}`,
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
if (!Object.keys(qs).length) {
delete options.qs;
}
options = Object.assign({}, options, option);
try {
return await this.helpers.request!(options);
} catch (error) {
const errorMessage = error?.response?.body?.message;
if (errorMessage) {
throw new Error(`Oura error response [${error.statusCode}]: ${errorMessage}`);
}
throw error;
}
}

View file

@ -0,0 +1,178 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
} from 'n8n-workflow';
import {
ouraApiRequest,
} from './GenericFunctions';
import {
profileOperations,
} from './ProfileDescription';
import {
summaryFields,
summaryOperations,
} from './SummaryDescription';
import * as moment from 'moment';
export class Oura implements INodeType {
description: INodeTypeDescription = {
displayName: 'Oura',
name: 'oura',
icon: 'file:oura.svg',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Oura API',
defaults: {
name: 'Oura',
color: '#2f4a73',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'ouraApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Profile',
value: 'profile',
},
{
name: 'Summary',
value: 'summary',
},
],
default: 'summary',
description: 'Resource to consume.',
},
...profileOperations,
...summaryOperations,
...summaryFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const length = items.length;
let responseData;
const returnData: IDataObject[] = [];
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
for (let i = 0; i < length; i++) {
if (resource === 'profile') {
// *********************************************************************
// profile
// *********************************************************************
// https://cloud.ouraring.com/docs/personal-info
if (operation === 'get') {
// ----------------------------------
// profile: get
// ----------------------------------
responseData = await ouraApiRequest.call(this, 'GET', '/userinfo');
}
} else if (resource === 'summary') {
// *********************************************************************
// summary
// *********************************************************************
// https://cloud.ouraring.com/docs/daily-summaries
const qs: IDataObject = {};
const { start, end } = this.getNodeParameter('filters', i) as { start: string; end: string; };
const returnAll = this.getNodeParameter('returnAll', 0) as boolean;
if (start) {
qs.start = moment(start).format('YYYY-MM-DD');
}
if (end) {
qs.end = moment(end).format('YYYY-MM-DD');
}
if (operation === 'getActivity') {
// ----------------------------------
// profile: getActivity
// ----------------------------------
responseData = await ouraApiRequest.call(this, 'GET', '/activity', {}, qs);
responseData = responseData.activity;
if (returnAll === false) {
const limit = this.getNodeParameter('limit', 0) as number;
responseData = responseData.splice(0, limit);
}
} else if (operation === 'getReadiness') {
// ----------------------------------
// profile: getReadiness
// ----------------------------------
responseData = await ouraApiRequest.call(this, 'GET', '/readiness', {}, qs);
responseData = responseData.readiness;
if (returnAll === false) {
const limit = this.getNodeParameter('limit', 0) as number;
responseData = responseData.splice(0, limit);
}
} else if (operation === 'getSleep') {
// ----------------------------------
// profile: getSleep
// ----------------------------------
responseData = await ouraApiRequest.call(this, 'GET', '/sleep', {}, qs);
responseData = responseData.sleep;
if (returnAll === false) {
const limit = this.getNodeParameter('limit', 0) as number;
responseData = responseData.splice(0, limit);
}
}
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,27 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const profileOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'profile',
],
},
},
options: [
{
name: 'Get',
value: 'get',
description: 'Get the user\'s personal information.',
},
],
default: 'get',
description: 'Operation to perform.',
},
] as INodeProperties[];

View file

@ -0,0 +1,105 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const summaryOperations = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
displayOptions: {
show: {
resource: [
'summary',
],
},
},
options: [
{
name: 'Get Activity Summary',
value: 'getActivity',
description: 'Get the user\'s activity summary.',
},
{
name: 'Get Readiness Summary',
value: 'getReadiness',
description: 'Get the user\'s readiness summary.',
},
{
name: 'Get Sleep Periods',
value: 'getSleep',
description: 'Get the user\'s sleep summary.',
},
],
default: 'getSleep',
description: 'Operation to perform.',
},
] as INodeProperties[];
export const summaryFields = [
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
displayOptions: {
show: {
resource: [
'summary',
],
},
},
default: false,
description: 'If all results should be returned or only up to a given limit.',
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
displayOptions: {
show: {
resource: [
'summary',
],
returnAll: [
false,
],
},
},
typeOptions: {
minValue: 1,
maxValue: 10,
},
default: 5,
description: 'How many results to return.',
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
displayOptions: {
show: {
resource: [
'summary',
],
},
},
default: {},
options: [
{
displayName: 'End Date',
name: 'end',
type: 'dateTime',
default: '',
description: 'End date for the summary retrieval. If omitted, it defaults to the current day.',
},
{
displayName: 'Start Date',
name: 'start',
type: 'dateTime',
default: '',
description: 'Start date for the summary retrieval. If omitted, it defaults to a week ago.',
},
],
},
] as INodeProperties[];

View file

@ -0,0 +1,16 @@
<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
"http://www.w3.org/TR/2001/REC-SVG-20010904/DTD/svg10.dtd">
<svg version="1.0" xmlns="http://www.w3.org/2000/svg"
viewBox="12 12 35.000000 35.000000"
preserveAspectRatio="xMidYMid meet">
<g transform="translate(0.000000,60.000000) scale(0.100000,-0.100000)"
fill="#000000" stroke="none">
<path d="M240 435 c0 -12 13 -15 60 -15 47 0 60 3 60 15 0 12 -13 15 -60 15
-47 0 -60 -3 -60 -15z"/>
<path d="M260 387 c-41 -14 -80 -68 -80 -111 0 -77 79 -138 151 -117 98 29
121 154 39 212 -35 24 -69 29 -110 16z m100 -44 c49 -43 43 -121 -10 -148 -42
-22 -74 -18 -109 11 -43 36 -44 93 -2 135 38 38 80 39 121 2z"/>
</g>
</svg>

After

Width:  |  Height:  |  Size: 705 B

View file

@ -169,6 +169,7 @@
"dist/credentials/OAuth2Api.credentials.js",
"dist/credentials/OpenWeatherMapApi.credentials.js",
"dist/credentials/OrbitApi.credentials.js",
"dist/credentials/OuraApi.credentials.js",
"dist/credentials/PaddleApi.credentials.js",
"dist/credentials/PagerDutyApi.credentials.js",
"dist/credentials/PagerDutyOAuth2Api.credentials.js",
@ -436,6 +437,7 @@
"dist/nodes/OpenThesaurus/OpenThesaurus.node.js",
"dist/nodes/OpenWeatherMap.node.js",
"dist/nodes/Orbit/Orbit.node.js",
"dist/nodes/Oura/Oura.node.js",
"dist/nodes/Paddle/Paddle.node.js",
"dist/nodes/PagerDuty/PagerDuty.node.js",
"dist/nodes/PayPal/PayPal.node.js",