JavaScript
Without Router - Current Approach
// Assuming this as previous logic without Router
const axios = require('axios');
const entityMetadataFromCR = {
baseUrl: 'http://fip-1.dev.sahamati.org.in/fip-simulate',
id: "FIP-SIMULATOR"
}
const getHttpConfig = ({ baseUrl, route, headers, data, methodType }) => {
return { url: `${baseUrl}${route}`, headers: headers, data, methodType };
};
const executeDiscoveryRequest = () => {
const route = '/v2/Accounts/discover';
const config = getHttpConfig({ baseUrl: entityMetadataFromCR.baseUrl, route, headers: {}, data: {}, methodType: 'POST' });
axios.request(config)
.then(response => response.data)
.catch(error => {
console.error('Error making discovery request:', error.message);
throw error;
});
};
const performAnotherOperations = async (accountDiscoverResponse) => {
console.log('Perform another operations with the response', accountDiscoverResponse);
}
executeDiscoveryRequest()
.then(performAnotherOperations)
.then(() => console.log('Account discovery successful.'))
.catch(() => console.log('Account discovery failed.'))
.finally(() => console.log('Account discovery completed.'));
With Router Integration
// Changes to implementation to integrate with Router
const axios = require('axios');
// OLD START
const entityMetadataFromCR = {
baseUrl: 'http://fip-1.dev.sahamati.org.in/fip-simulate',
id: "FIP-SIMULATOR"
}
const getHttpConfig = ({ baseUrl, route, headers, data, methodType }) => {
return { url: `${baseUrl}${route}`, headers: headers, data, methodType };
};
const executeDiscoveryRequest = () => {
const route = '/v2/Accounts/discover';
let config = getHttpConfig({ baseUrl: entityMetadataFromCR.baseUrl, route, headers: {}, data: {}, methodType: 'POST' });
// add Sahamati configuration [Start]
config = addSahamatiConfiguration({ config, route });
// add Sahamati configuration [End]
axios.request(config)
.then(response => response.data)
.catch(error => {
console.error('Error making discovery request:', error.message);
throw error;
});
};
const performAnotherOperations = async (accountDiscoverResponse) => {
console.log('Perform another operations with the response', accountDiscoverResponse);
}
executeDiscoveryRequest()
.then(performAnotherOperations)
.then(() => console.log('Account discovery successful.'))
.catch(() => console.log('Account discovery failed.'))
.finally(() => console.log('Account discovery completed.'));
// OLD END
// NEW START
const generateBase64EncodedJson = (json) => {
return btoa(JSON.stringify(json));
}
const generateRequestMeta = (recipientId) => {
const context = { "recipient-id": recipientId }
return generateBase64EncodedJson(context);
}
const addSahamatiConfiguration = ({ config, route }) => {
const routerUrl = 'http://api.dev.sahamati.org.in/fip-router';
config.url = `${routerUrl}${route}`;
config.headers["x-request-meta"] = generateRequestMeta(entityMetadataFromCR.id);
return config;
}
// NEW END
Last updated
Was this helpful?