From e2dc178ec12b1579e12afe85669c0443e2789a36 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Mon, 31 May 2021 18:16:52 +0200 Subject: [PATCH] API Data Fetcher replaceBy Config Property Extended --- .../replaceby-config-property.json | 78 +++++++++++++++++++ .../Docs-Nodes/api-path-parameter.json | 8 +- .../Docs-Nodes/api-query-parameter.json | 9 +++ .../API-Data-Fetcher-Bot/FetchingProcess.js | 53 ++++++++++--- 4 files changed, 133 insertions(+), 15 deletions(-) create mode 100644 Projects/Superalgos/Schemas/Docs-Concepts/replaceby-config-property.json diff --git a/Projects/Superalgos/Schemas/Docs-Concepts/replaceby-config-property.json b/Projects/Superalgos/Schemas/Docs-Concepts/replaceby-config-property.json new file mode 100644 index 000000000..fd8e568e3 --- /dev/null +++ b/Projects/Superalgos/Schemas/Docs-Concepts/replaceby-config-property.json @@ -0,0 +1,78 @@ +{ + "type": "replaceBy Config Property", + "definition": { + "text": "This property allows you to instruct the API Fetcher Bot to set a value to this parameter based on contextual information that can not be defined at the Data Mine or the API Map. This contextual information can be for example the current Base Asset or the current time.", + "updated": 1622476480596 + }, + "paragraphs": [ + { + "style": "Block", + "text": "How To Use It", + "updated": 1622477226772 + }, + { + "style": "Subtitle", + "text": "replaceBy Config Property Usage" + }, + { + "style": "Text", + "text": "This property is optional, meaning that the Bot will use the default value when it is not present. The default value for an API Query Parameter is the value configu property, while the default for an API Path Parameter is the codeName config property." + }, + { + "style": "Text", + "text": "The possible values for this property are:" + }, + { + "style": "List", + "text": "@BaseAsset: This will force the Bot to use the current Base Asset." + }, + { + "style": "Javascript", + "text": "parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE\n.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName", + "updated": 1622476773559 + }, + { + "style": "List", + "text": "@QuotedAsset: This will force the Bot to use the current Quoted Asset.", + "updated": 1622476819686 + }, + { + "style": "Javascript", + "text": "parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE\n.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName", + "updated": 1622476849667 + }, + { + "style": "List", + "text": "@BeginCurrentMinute: This will instruct the Bot to use a timestamp at the begining of the current minute.", + "updated": 1622476907142 + }, + { + "style": "Javascript", + "text": "parameterValue = \nMath.trunc((new Date()).valueOf() / \nTS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nTS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS", + "updated": 1622477009814 + }, + { + "style": "List", + "text": "@EndCurrentMinute: This will instruct the Bot to use a timestamp at the ending of the current minute.", + "updated": 1622476926765 + }, + { + "style": "Javascript", + "text": "parameterValue = Math.trunc((new Date()).valueOf() / \nTS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * \nTS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS + \nTS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1", + "updated": 1622477088684 + }, + { + "style": "Note", + "text": "If users assing a value that is not listed above, the property will be ingnored and the default value will be used.", + "updated": 1622477178155 + }, + { + "style": "Text", + "text": "" + }, + { + "style": "Text", + "text": "" + } + ] +} \ No newline at end of file diff --git a/Projects/Superalgos/Schemas/Docs-Nodes/api-path-parameter.json b/Projects/Superalgos/Schemas/Docs-Nodes/api-path-parameter.json index fabb2abb1..771a99ae5 100644 --- a/Projects/Superalgos/Schemas/Docs-Nodes/api-path-parameter.json +++ b/Projects/Superalgos/Schemas/Docs-Nodes/api-path-parameter.json @@ -21,11 +21,13 @@ }, { "style": "List", - "text": "replaceBy: this property allows you to instruct the API Fetcher Bot to set a value to this parameter. The possible values are: @BaseAsset and @QuotedAsset" + "text": "replaceBy: this property allows you to instruct the API Fetcher Bot to set a value to this parameter replacing the current default and using contextual information available only at runtime. ", + "updated": 1622477486319 }, { - "style": "Important", - "text": "The replacedBy property is the way users have to send some contextual values to the API Server like for example the Base Asset or Quoted Asset of the Defautl Market." + "style": "Include", + "text": "Superalgos->Concept->replaceBy Config Property->How To Use It", + "updated": 1622477440806 } ] } \ No newline at end of file diff --git a/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json b/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json index ac68b8246..863b6d9f5 100644 --- a/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json +++ b/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json @@ -18,6 +18,15 @@ "style": "List", "text": "value: It is the value to be used at this parameter. The API Map can define the value, that would be considered a default value. The same parameter defined at the Data Mine will have the actual value to be used replacing the default. If you are defining a API Map to be shared as a Plugin, you can avoid setting this property at the API Map level, and let the users set it at their Data Mine when they use your API Map. For completness, this parameter should be included at the the API Map even if the value property is not there.", "updated": 1622160191941 + }, + { + "style": "List", + "text": "replaceBy: this property allows you to instruct the API Fetcher Bot to set a value to this parameter replacing the current default and using contextual information available only at runtime. ", + "updated": 1622477507235 + }, + { + "style": "Include", + "text": "Superalgos->Concept->replaceBy Config Property->How To Use It" } ] } \ No newline at end of file diff --git a/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js b/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js index c93bd96a6..0286ed6cc 100644 --- a/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js +++ b/Projects/Superalgos/TS/Bot-Modules/API-Data-Fetcher-Bot/FetchingProcess.js @@ -346,9 +346,19 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { if (apiQueryParameter.config.isPageNumber === true) { pageNumberParameter = apiQueryParameter } else { + /* + Whatever is in Value is the default value. + */ if (apiQueryParameter.config.codeName !== undefined && apiQueryParameter.config.value !== undefined) { parametersMap.set(apiQueryParameter.config.codeName, apiQueryParameter.config.value) } + /* + If there is a replace action to be made, then that could replace the default value. + */ + let replaceValue = checkReplaceBy(apiQueryParameter) + if (replaceValue !== undefined) { + parametersMap.set(apiQueryParameter.config.codeName, replaceValue) + } } } } @@ -393,23 +403,42 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { if (pathString === "") { pathString = '/' } let apiPathParameter = value let parameterValue = key // This is the default value - if (apiPathParameter.config.replaceBy !== undefined) { - switch (apiPathParameter.config.replaceBy) { - case '@BaseAsset': { - parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName - break - } - case '@QuotedAsset': { - parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName - break - } - } + let replaceValue = checkReplaceBy(apiPathParameter) + + if (replaceValue !== undefined) { + parameterValue = replaceValue } + pathString = pathString + separator + parameterValue separator = "/" } } + function checkReplaceBy(apiParameter) { + let parameterValue + if (apiParameter.config.replaceBy !== undefined) { + switch (apiParameter.config.replaceBy) { + case '@BaseAsset': { + parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.baseAsset.referenceParent.config.codeName + break + } + case '@QuotedAsset': { + parameterValue = TS.projects.superalgos.globals.taskConstants.TASK_NODE.parentNode.parentNode.parentNode.referenceParent.quotedAsset.referenceParent.config.codeName + break + } + case '@BeginCurrentMinute': { + parameterValue = Math.trunc((new Date()).valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * TS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS + break + } + case '@EndCurrentMinute': { + parameterValue = Math.trunc((new Date()).valueOf() / TS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS) * TS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS + TS.projects.superalgos.globals.timeConstants.ONE_MIN_IN_MILISECONDS - 1 + break + } + } + } + return parameterValue + } + async function fetchAllPages() { if (pageNumberParameter === undefined) { @@ -653,7 +682,7 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { resolve('NO_MORE_PAGES') } else { resolve('PAGE_FETCHED') - } + } /* If we received an errror code, we abort the processing at this point. */