From d24604ec247c19d34c2e62d650a5f9444e5d2e91 Mon Sep 17 00:00:00 2001 From: Luis Fernando Molina Date: Fri, 28 May 2021 02:05:22 +0200 Subject: [PATCH] API Data Fetcher feature parameters with default values at the API Map --- .../Docs-Nodes/api-query-parameter.json | 4 +- .../API-Data-Fetcher-Bot/FetchingProcess.js | 114 +++++++++++++----- 2 files changed, 85 insertions(+), 33 deletions(-) diff --git a/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json b/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json index c80fb3b09..ac68b8246 100644 --- a/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json +++ b/Projects/Superalgos/Schemas/Docs-Nodes/api-query-parameter.json @@ -16,8 +16,8 @@ }, { "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.", - "updated": 1621022463369 + "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 } ] } \ 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 b13a52c9d..8bd901f01 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 @@ -305,23 +305,49 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { /* Parameters to the query of the API are all optional. Here we will see if we need to place the API call with Parameters... + + The Parameters defined at the API Map will be the default values that + can be overwritten by the ones at the Data Mine. + + We will create parametersMap to handle this. + */ + let parametersMap = new Map() + if (endpointNode.apiQueryParameters !== undefined) { + for (let i = 0; i < endpointNode.apiQueryParameters.apiQueryParameters.length; i++) { + apiQueryParameter = endpointNode.apiQueryParameters.apiQueryParameters[i] + addParameterToMap(apiQueryParameter) + } + } + /* + The parameters defined at the Data Mine will overwrite whatever is at the API Map */ - let separator = "" if (productDefinition.apiQueryParameters !== undefined) { - queryString = '?' for (j = 0; j < productDefinition.apiQueryParameters.apiQueryParameters.length; j++) { let apiQueryParameter = productDefinition.apiQueryParameters.apiQueryParameters[j] + addParameterToMap(apiQueryParameter) + } + } - /* - There is a special parameter which has a flag to indicate is a Page Number. - This will be treated differently since we will need to iterate to get each possible page. - The page number will no be added here to the query string. - */ - if (apiQueryParameter.config.isPageNumber === true) { - pageNumberParameter = apiQueryParameter - } else { - queryString = queryString + separator + apiQueryParameter.config.codeName + '=' + apiQueryParameter.config.value - separator = "&" + let separator = "" + queryString = '?' + parametersMap.forEach(addParameterToQueryString) + + function addParameterToQueryString(value, key, map) { + queryString = queryString + separator + key + '=' + value + separator = "&" + } + + function addParameterToMap(apiQueryParameter) { + /* + There is a special parameter which has a flag to indicate is a Page Number. + This will be treated differently since we will need to iterate to get each possible page. + The page number will no be added here to the query string. + */ + if (apiQueryParameter.config.isPageNumber === true) { + pageNumberParameter = apiQueryParameter + } else { + if (apiQueryParameter.config.codeName !== undefined && apiQueryParameter.config.value !== undefined) { + parametersMap.set(apiQueryParameter.config.codeName, apiQueryParameter.config.value) } } } @@ -331,29 +357,55 @@ exports.newSuperalgosBotModulesFetchingProcess = function (processIndex) { /* Parameters to the path of the API are all optional. Here we will see if we need to place the API call with Parameters... - */ - let separator = "" - if (productDefinition.apiPathParameters !== undefined) { - pathString = '/' - for (let j = 0; j < productDefinition.apiPathParameters.apiPathParameters.length; j++) { - let apiPathParameter = productDefinition.apiPathParameters.apiPathParameters[j] + The Parameters defined at the API Map will be the default values that + can be overwritten by the ones at the Data Mine. - let parameterValue = apiPathParameter.config.codeName // 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 - } + We will create parametersMap to handle this. + */ + let parametersMap = new Map() + if (endpointNode.apiPathParameters !== undefined) { + for (let i = 0; i < endpointNode.apiPathParameters.apiPathParameters.length; i++) { + apiPathParameter = endpointNode.apiPathParameters.apiPathParameters[i] + addParameterToMap(apiPathParameter) + } + } + /* + The parameters defined at the Data Mine will overwrite whatever is at the API Map + */ + if (productDefinition.apiPathParameters !== undefined) { + for (j = 0; j < productDefinition.apiPathParameters.apiPathParameters.length; j++) { + let apiPathParameter = productDefinition.apiPathParameters.apiPathParameters[j] + addParameterToMap(apiPathParameter) + } + } + + function addParameterToMap(apiPathParameter) { + if (apiPathParameter.config.codeName !== undefined) { + parametersMap.set(apiPathParameter.config.codeName, apiPathParameter) + } + } + + let separator = "" + pathString = '/' + parametersMap.forEach(addParameterToPath) + + function addParameterToPath(value, key, map) { + 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 } } - pathString = pathString + separator + parameterValue - separator = "/" } + pathString = pathString + separator + parameterValue + separator = "/" } }