diff --git a/Integration/OutboundRestDynamicEndpoint/README.md b/Integration/OutboundRestDynamicEndpoint/README.md new file mode 100644 index 0000000000..61baac05e0 --- /dev/null +++ b/Integration/OutboundRestDynamicEndpoint/README.md @@ -0,0 +1,26 @@ +This is a server-side Script Include that contains the core logic. It reads the endpoint configurations from a System Property, parses the JSON, and returns the appropriate URL based on the current instance's name. + +System Property: x_my_scope.api.endpoints +This property stores a JSON object containing the endpoint URLs for each environment. It must be created and populated in each instance that uses the utility. + +Sample JSON object: +{ + "dev": "https://dev-instance.example.com/api", + "test": "https://test-instance.example.com/api", + "prod": "https://prod-instance.example.com/api" +} + +Usage: +var endpointConfig = new EndpointConfig(); +var endpointUrl = endpointConfig.getEndpoint(); +if (endpointUrl) +{ + gs.info("Endpoint URL: " + endpointUrl); +//Use the endpointUrl in your REST call + var request = new sn_ws.RESTMessageV2(); + request.setEndpoint(endpointUrl); + // ... rest of your integration logic +} else { + gs.error("Failed to retrieve endpoint URL."); + } + diff --git a/Integration/OutboundRestDynamicEndpoint/scriptinclude.js b/Integration/OutboundRestDynamicEndpoint/scriptinclude.js new file mode 100644 index 0000000000..0dd7cbb606 --- /dev/null +++ b/Integration/OutboundRestDynamicEndpoint/scriptinclude.js @@ -0,0 +1,54 @@ + +//Create a sample system Property called x_my_scope.api.endpoints having below object as example. make sure your company instance includes those key such as dev,prod,test or modify it with your instance name + +// { +// "dev": "https://dev-instance.example.com/api", +// "test": "https://test-instance.example.com/api", +// "prod": "https://prod-instance.example.com/api" +// } + +var EndpointConfig = Class.create(); +EndpointConfig.prototype = { + initialize: function() { + // No hardcoded object here. It will be fetched from the System Property. + }, + + getEndpoint: function() { + var propertyName = 'x_my_scope.api.endpoints'; + var endpointObjectStr = gs.getProperty(propertyName); + if (gs.nil(endpointObjectStr)) { + gs.error("EndpointConfig: System property '" + propertyName + "' not found or is empty."); + return null; + } + + try { + var endpoints = JSON.parse(endpointObjectStr); + var instanceName = gs.getProperty('instance_name'); + var environmentKey; + + if (instanceName.includes('dev')) { + environmentKey = 'dev'; + } else if (instanceName.includes('test') || instanceName.includes('uat')) { + environmentKey = 'test'; + } else if (instanceName.includes('prod')) { + environmentKey = 'prod'; + } else { + gs.error("EndpointConfig: Could not determine environment for instance '" + instanceName + "'."); + return null; + } + + if (endpoints.hasOwnProperty(environmentKey)) { + return endpoints[environmentKey]; + } else { + gs.error("EndpointConfig: Configuration not found for environment '" + environmentKey + "'."); + return null; + } + + } catch (ex) { + gs.error("EndpointConfig: Failed to parse JSON from system property '" + propertyName + "'. Exception: " + ex); + return null; + } + }, + + type: 'EndpointConfig' +};