// imports
const express = require("express");
const cfenv = require('cfenv');
const axios = require("axios");
const app = express();
const PORT = process.env.PORT || 4000;
const getProjectDef = async (req, res) => {
const MAX_ROWS = "1000";
// Get the UAA and destination services
const UAA_SERVICE = cfenv.getAppEnv().getService('UAA_INSTANCE_NAME');
const DESTINATION_SERVICE = cfenv.getAppEnv().getService('DESTINATION_SERVICE_NAME');
// Combine the client ID and secret for the UAA service into a single string
const UAA_CREDENTIALS = DESTINATION_SERVICE.credentials.clientid + ':' + DESTINATION_SERVICE.credentials.clientsecret;
// Set the name of the destination to retrieve and the endpoint to call
const DESTINATION_NAME = 'DESTINATION_NAME';
const END_POINT = 'END_POINT_URL';
// Set the payload for the POST request to the UAA to get a token
const post_payload = {
'client_id': DESTINATION_SERVICE.credentials.clientid,
'grant_type': 'client_credentials'
};
// Set the configuration options for the POST request to the UAA
const post_config = {
method: 'POST',
url: UAA_SERVICE.credentials.url + '/oauth/token',
headers: {
'Authorization': 'Basic ' + Buffer.from(UAA_CREDENTIALS).toString('base64'), // Encode the client ID and secret as base64
'Content-type': 'application/x-www-form-urlencoded'
},
data: new URLSearchParams(post_payload).toString() // Encode the payload as x-www-form-urlencoded
};
// Make the POST request to the UAA to get a token
axios(post_config)
.then((response) => {
if (response.status === 200) {
const token = response.data.access_token; // Get the token from the response data
// Set the configuration options for the GET request to the destination service
const get_config = {
method: 'GET',
url: DESTINATION_SERVICE.credentials.uri + '/destination-configuration/v1/destinations/' + DESTINATION_NAME,
headers: {
'Authorization': 'Bearer ' + token // Include the token in the authorization header of the GET request
}
};
// Make the GET request to the destination service to retrieve the destination configuration
axios(get_config)
.then((response) => {
const DESTINATION = response.data; // Get the destination configuration from the response data
// Get the auth token from the destination configuration
const token = DESTINATION.authTokens[0];
// Set the configuration options for the POST request to the endpoint of the destination service
// a CPI is built over the BAPI: BAPI_PROJECTDEF_GETLIST with OAuth2ClientCredentials authentication
const options = {
method: 'POST',
url: DESTINATION.destinationConfiguration.URL + END_POINT,
headers: {
'Authorization': `${token.type} ${token.value}` // Include the auth token in the authorization header of the GET request
},
data: {
MaxRows: MAX_ROWS,
ProjectDefinitionRange: {
item: [
{
Sign: req.data.sign,
Option: req.data.option,
Low: req.data.low,
High: req.data.high,
},
],
},
}
};
// Make the GET request to the endpoint of the destination service
axios(options)
.then((response) => {
res.send(response.data); // Return data
})
.catch((error) => {
res.send(error); // Return error
});
})
.catch((error) => {
res.send(error); // Return error
});
}
})
.catch((error) => {
res.send(error); // Return error
});
console.log('Exit :------->');
};
// endpoint
app.get("/getProjectDef", getProjectDef);
app.listen(PORT),
() => {
console.log(`Listnening to the PORT: ${PORT}`);
};
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
22 | |
8 | |
7 | |
5 | |
5 | |
5 | |
4 | |
4 | |
3 | |
3 |