Add Marketstack node (#2033)

* Add Marketstack Node

* Add optional HTTPS support

*  Refactor Marketstack node

* 🔥 Remove logging

*  Small improvement

* 🐛 Fix issue validating error condition

Co-authored-by: Ed Linklater <github@ed.geek.nz>
Co-authored-by: ricardo <ricardoespinoza105@gmail.com>
This commit is contained in:
Iván Ovejero 2021-08-01 13:49:24 +02:00 committed by GitHub
parent f900bfe897
commit 0d640fab87
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 647 additions and 0 deletions

View file

@ -0,0 +1,25 @@
import {
ICredentialType,
NodePropertyTypes,
} from 'n8n-workflow';
export class MarketstackApi implements ICredentialType {
name = 'marketstackApi';
displayName = 'Marketstack API';
documentationUrl = 'marketstack';
properties = [
{
displayName: 'API Key',
name: 'apiKey',
type: 'string' as NodePropertyTypes,
default: '',
},
{
displayName: 'Use HTTPS',
name: 'useHttps',
type: 'boolean' as NodePropertyTypes,
default: false,
description: 'Use HTTPS (paid plans only).',
},
];
}

View file

@ -0,0 +1,96 @@
import {
OptionsWithUri,
} from 'request';
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
NodeApiError,
NodeOperationError,
} from 'n8n-workflow';
export async function marketstackApiRequest(
this: IExecuteFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const credentials = this.getCredentials('marketstackApi') as IDataObject;
const protocol = credentials.useHttps ? 'https' : 'http'; // Free API does not support HTTPS
const options: OptionsWithUri = {
method,
uri: `${protocol}://api.marketstack.com/v1${endpoint}`,
qs: {
access_key: credentials.apiKey,
...qs,
},
json: true,
};
if (!Object.keys(body).length) {
delete options.body;
}
try {
return await this.helpers.request(options);
} catch (error) {
throw new NodeApiError(this.getNode(), error);
}
}
export async function marketstackApiRequestAllItems(
this: IExecuteFunctions,
method: string,
endpoint: string,
body: IDataObject = {},
qs: IDataObject = {},
) {
const returnAll = this.getNodeParameter('returnAll', 0, false) as boolean;
const limit = this.getNodeParameter('limit', 0, 0) as number;
let responseData;
const returnData: IDataObject[] = [];
qs.offset = 0;
do {
responseData = await marketstackApiRequest.call(this, method, endpoint, body, qs);
returnData.push(...responseData.data);
if (!returnAll && returnData.length > limit) {
return returnData.slice(0, limit);
}
qs.offset += responseData.count;
} while (
responseData.total > returnData.length
);
return returnData;
}
export const format = (datetime?: string) => datetime?.split('T')[0];
export function validateTimeOptions(
this: IExecuteFunctions,
timeOptions: boolean[],
) {
if (timeOptions.every(o => !o)) {
throw new NodeOperationError(
this.getNode(),
'Please filter by latest, specific date or timeframe (start and end dates).',
);
}
if (timeOptions.filter(Boolean).length > 1) {
throw new NodeOperationError(
this.getNode(),
'Please filter by one of latest, specific date, or timeframe (start and end dates).',
);
}
}

View file

@ -0,0 +1,203 @@
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeExecutionData,
INodeType,
INodeTypeDescription,
NodeOperationError,
} from 'n8n-workflow';
import {
endOfDayDataFields,
endOfDayDataOperations,
exchangeFields,
exchangeOperations,
tickerFields,
tickerOperations,
} from './descriptions';
import {
format,
marketstackApiRequest,
marketstackApiRequestAllItems,
validateTimeOptions,
} from './GenericFunctions';
import {
EndOfDayDataFilters,
Operation,
Resource,
} from './types';
export class Marketstack implements INodeType {
description: INodeTypeDescription = {
displayName: 'Marketstack',
name: 'marketstack',
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
icon: 'file:marketstack.svg',
group: ['transform'],
version: 1,
description: 'Consume Marketstack API',
defaults: {
name: 'Marketstack',
color: '#02283e',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'marketstackApi',
required: true,
},
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'End-of-Day Data',
value: 'endOfDayData',
description: 'Stock market closing data',
},
{
name: 'Exchange',
value: 'exchange',
description: 'Stock market exchange',
},
{
name: 'Ticker',
value: 'ticker',
description: 'Stock market symbol',
},
],
default: 'endOfDayData',
required: true,
},
...endOfDayDataOperations,
...endOfDayDataFields,
...exchangeOperations,
...exchangeFields,
...tickerOperations,
...tickerFields,
],
};
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData();
const resource = this.getNodeParameter('resource', 0) as Resource;
const operation = this.getNodeParameter('operation', 0) as Operation;
let responseData: any; // tslint:disable-line: no-any
const returnData: IDataObject[] = [];
for (let i = 0; i < items.length; i++) {
try {
if (resource === 'endOfDayData') {
if (operation === 'getAll') {
// ----------------------------------
// endOfDayData: getAll
// ----------------------------------
const qs: IDataObject = {
symbols: this.getNodeParameter('symbols', i),
};
const {
latest,
specificDate,
dateFrom,
dateTo,
...rest
} = this.getNodeParameter('filters', i) as EndOfDayDataFilters;
validateTimeOptions.call(this, [
latest !== undefined && latest !== false,
specificDate !== undefined,
dateFrom !== undefined && dateTo !== undefined,
]);
if (Object.keys(rest).length) {
Object.assign(qs, rest);
}
let endpoint: string;
if (latest) {
endpoint = '/eod/latest';
} else if (specificDate) {
endpoint = `/eod/${format(specificDate)}`;
} else {
if (!dateFrom || !dateTo) {
throw new NodeOperationError(
this.getNode(),
'Please enter a start and end date to filter by timeframe.',
);
}
endpoint = '/eod';
qs.date_from = format(dateFrom);
qs.date_to = format(dateTo);
}
responseData = await marketstackApiRequestAllItems.call(this, 'GET', endpoint, {}, qs);
}
} else if (resource === 'exchange') {
if (operation === 'get') {
// ----------------------------------
// exchange: get
// ----------------------------------
const exchange = this.getNodeParameter('exchange', i);
const endpoint = `/exchanges/${exchange}`;
responseData = await marketstackApiRequest.call(this, 'GET', endpoint);
}
} else if (resource === 'ticker') {
if (operation === 'get') {
// ----------------------------------
// ticker: get
// ----------------------------------
const symbol = this.getNodeParameter('symbol', i);
const endpoint = `/tickers/${symbol}`;
responseData = await marketstackApiRequest.call(this, 'GET', endpoint);
}
}
} catch (error) {
if (this.continueOnFail()) {
returnData.push({ error: error.message });
continue;
}
throw error;
}
Array.isArray(responseData)
? returnData.push(...responseData)
: returnData.push(responseData);
}
return [this.helpers.returnJsonArray(returnData)];
}
}

View file

@ -0,0 +1,157 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const endOfDayDataOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Get All',
value: 'getAll',
},
],
default: 'getAll',
displayOptions: {
show: {
resource: [
'endOfDayData',
],
},
},
},
];
export const endOfDayDataFields: INodeProperties[] = [
{
displayName: 'Ticker',
name: 'symbols',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'endOfDayData',
],
operation: [
'getAll',
],
},
},
default: '',
description: 'One or multiple comma-separated stock symbols (tickers) to retrieve, e.g. <code>AAPL</code> or <code>AAPL,MSFT</code>',
},
{
displayName: 'Return All',
name: 'returnAll',
type: 'boolean',
default: false,
description: 'Whether to return all results or only up to a given limit',
displayOptions: {
show: {
resource: [
'endOfDayData',
],
operation: [
'getAll',
],
},
},
},
{
displayName: 'Limit',
name: 'limit',
type: 'number',
default: 50,
description: 'How many results to return',
typeOptions: {
minValue: 1,
},
displayOptions: {
show: {
resource: [
'endOfDayData',
],
operation: [
'getAll',
],
returnAll: [
false,
],
},
},
},
{
displayName: 'Filters',
name: 'filters',
type: 'collection',
placeholder: 'Add Filter',
default: {},
displayOptions: {
show: {
resource: [
'endOfDayData',
],
operation: [
'getAll',
],
},
},
options: [
{
displayName: 'Exchange',
name: 'exchange',
type: 'string',
default: '',
description: 'Stock exchange to filter results by, specified by <a target="_blank" href="https://en.wikipedia.org/wiki/Market_Identifier_Code">Market Identifier Code</a>, e.g. <code>XNAS</code>',
},
{
displayName: 'Latest',
name: 'latest',
type: 'boolean',
default: false,
description: 'Whether to fetch the most recent stock market data',
},
{
displayName: 'Sort Order',
name: 'sort',
description: 'Order to sort results in',
type: 'options',
options: [
{
name: 'Ascending',
value: 'ASC',
},
{
name: 'Descending',
value: 'DESC',
},
],
default: 'DESC',
},
{
displayName: 'Specific Date',
name: 'specificDate',
type: 'dateTime',
default: '',
description: 'Date in YYYY-MM-DD format, e.g. <code>2020-01-01</code>, or in ISO-8601 date format, e.g. <code>2020-05-21T00:00:00+0000</code>',
},
{
displayName: 'Timeframe Start Date',
name: 'dateFrom',
type: 'dateTime',
default: '',
description: 'Timeframe start date in YYYY-MM-DD format, e.g. <code>2020-01-01</code>, or in ISO-8601 date format, e.g. <code>2020-05-21T00:00:00+0000</code>',
},
{
displayName: 'Timeframe End Date',
name: 'dateTo',
type: 'dateTime',
default: '',
description: 'Timeframe end date in YYYY-MM-DD format, e.g. <code>2020-01-01</code>, or in ISO-8601 date format, e.g. <code>2020-05-21T00:00:00+0000</code>',
},
],
},
];

View file

@ -0,0 +1,46 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const exchangeOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Get',
value: 'get',
},
],
default: 'get',
displayOptions: {
show: {
resource: [
'exchange',
],
},
},
},
];
export const exchangeFields: INodeProperties[] = [
{
displayName: 'Exchange',
name: 'exchange',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'exchange',
],
operation: [
'get',
],
},
},
default: '',
description: 'Stock exchange to retrieve, specified by <a target="_blank" href="https://en.wikipedia.org/wiki/Market_Identifier_Code">Market Identifier Code</a>, e.g. <code>XNAS</code>',
},
];

View file

@ -0,0 +1,46 @@
import {
INodeProperties,
} from 'n8n-workflow';
export const tickerOperations: INodeProperties[] = [
{
displayName: 'Operation',
name: 'operation',
type: 'options',
options: [
{
name: 'Get',
value: 'get',
},
],
default: 'get',
displayOptions: {
show: {
resource: [
'ticker',
],
},
},
},
];
export const tickerFields: INodeProperties[] = [
{
displayName: 'Ticker',
name: 'symbol',
type: 'string',
required: true,
displayOptions: {
show: {
resource: [
'ticker',
],
operation: [
'get',
],
},
},
default: '',
description: 'Stock symbol (ticker) to retrieve, e.g. <code>AAPL</code>',
},
];

View file

@ -0,0 +1,3 @@
export * from './EndOfDayDataDescription';
export * from './TickerDescription';
export * from './ExchangeDescription';

View file

@ -0,0 +1,57 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Generator: Adobe Illustrator 24.0.2, SVG Export Plug-In . SVG Version: 6.00 Build 0) -->
<svg
version="1.1"
id="Layer_1"
x="0px"
y="0px"
viewBox="-2457.7 882.5 59.999998 60"
xml:space="preserve"
sodipodi:docname="marketstack.svg"
width="60"
height="60"
inkscape:version="1.1 (c68e22c387, 2021-05-23)"
xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
xmlns="http://www.w3.org/2000/svg"
xmlns:svg="http://www.w3.org/2000/svg"
xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
xmlns:cc="http://creativecommons.org/ns#"
xmlns:dc="http://purl.org/dc/elements/1.1/"><defs
id="defs77" /><sodipodi:namedview
id="namedview75"
pagecolor="#ffffff"
bordercolor="#666666"
borderopacity="1.0"
inkscape:pageshadow="2"
inkscape:pageopacity="0.0"
inkscape:pagecheckerboard="0"
showgrid="false"
width="60px"
inkscape:zoom="6.8111408"
inkscape:cx="145.49692"
inkscape:cy="32.44684"
inkscape:window-width="3840"
inkscape:window-height="2066"
inkscape:window-x="-11"
inkscape:window-y="-11"
inkscape:window-maximized="1"
inkscape:current-layer="Layer_1" />
<style
type="text/css"
id="style44">
.st0{enable-background:new ;}
.st1{fill:#FFFFFF;}
</style>
<title
id="title46">Marketstack</title>
<path
class="st1"
d="m -2398.9017,942.5 c 0,-0.38462 0.096,-0.67308 0.096,-0.86538 0,-1.63462 0,-3.36539 0,-5 0,-17.69231 0,-35.48077 0,-53.17308 0,-0.96154 0,-0.96154 -0.9616,-0.96154 -7.1154,0 -14.2307,0 -21.3461,0 -0.6731,0 -0.8654,0.19231 -0.8654,0.86538 0,3.36539 0,6.63462 0,10 0,0.67308 0.2884,0.86539 0.8654,0.86539 3.2692,0 6.5384,0 9.7115,0 0.9615,0 0.9615,0 0.9615,0.96154 0,6.53846 0,13.07692 0,19.61538 0,7.30769 0,14.51923 0,21.82693 0,0.48076 0,0.86538 0.577,1.15384 2.8846,1.25 5.7692,2.59616 8.5577,3.84616 0.7692,0.1923 1.5384,0.48076 2.4038,0.86538 z m -17.2115,-21.44231 c 0,0 0,0 0,0 0,-6.82692 0,-13.65384 0,-20.48077 0,-0.86538 0,-0.86538 -0.8654,-0.86538 -7.2116,0 -14.3269,0 -21.5385,0 -0.6731,0 -0.8654,0.19231 -0.8654,0.86538 0,3.36539 0,6.63462 0,10 0,0.67308 0.2885,0.86539 0.8654,0.86539 3.1731,0 6.3462,0 9.5193,0 1.25,0 1.25,-0.19231 1.25,1.25 0,7.98077 0,15.86538 0,23.84615 0,0.67308 0.1923,1.05769 0.8653,1.25 1.4423,0.57692 2.7885,1.25 4.2308,1.92308 2.0192,0.86538 3.9423,1.73077 5.9615,2.69231 0.4808,0.1923 0.6731,0.0961 0.6731,-0.38462 0,-0.19231 0,-0.38461 0,-0.57692 -0.096,-6.92308 -0.096,-13.65385 -0.096,-20.38462 z m -17.3077,8.75 c 0,0 0,0 0,0 0,-3.94231 0,-7.98077 0,-11.92307 0,-0.57693 -0.1923,-0.86539 -0.8654,-0.86539 -7.1154,0 -14.3269,0 -21.4423,0 -0.8654,0 -0.8654,0 -0.8654,0.86539 0,5.1923 0,10.48076 0,15.67307 0,0.67308 0.1923,0.96154 0.7692,1.15385 2.1154,0.67308 4.1346,1.44231 6.25,2.21154 5.0962,1.73077 10.1923,3.55769 15.2885,5.28846 0.8654,0.28846 0.8654,0.28846 0.8654,-0.67308 0,-3.84615 0,-7.78846 0,-11.73077 z"
id="path72"
style="fill:#02283e;fill-opacity:1;stroke-width:0.961538" />
<metadata
id="metadata1072"><rdf:RDF><cc:Work
rdf:about=""><dc:title>Marketstack</dc:title></cc:Work></rdf:RDF></metadata></svg>

After

Width:  |  Height:  |  Size: 3.2 KiB

View file

@ -0,0 +1,12 @@
export type Resource = 'endOfDayData' | 'exchange' | 'ticker';
export type Operation = 'get' | 'getAll';
export type EndOfDayDataFilters = {
latest?: boolean;
sort?: 'ASC' | 'DESC';
specificDate?: string;
dateFrom?: string;
dateTo?: string;
exchange?: string;
};

View file

@ -155,6 +155,7 @@
"dist/credentials/MailjetEmailApi.credentials.js",
"dist/credentials/MailjetSmsApi.credentials.js",
"dist/credentials/MandrillApi.credentials.js",
"dist/credentials/MarketstackApi.credentials.js",
"dist/credentials/MatrixApi.credentials.js",
"dist/credentials/MattermostApi.credentials.js",
"dist/credentials/MauticApi.credentials.js",
@ -452,6 +453,7 @@
"dist/nodes/Mailjet/Mailjet.node.js",
"dist/nodes/Mailjet/MailjetTrigger.node.js",
"dist/nodes/Mandrill/Mandrill.node.js",
"dist/nodes/Marketstack/Marketstack.node.js",
"dist/nodes/Matrix/Matrix.node.js",
"dist/nodes/Mattermost/Mattermost.node.js",
"dist/nodes/Mautic/Mautic.node.js",