The Destination service lets you find the destination information that is required to access a remote service or system from your Cloud Foundry application. Below fields are relevant while creating the destination:
Please find the below screenshot on precise data to be entered to create Destination for Purchase requisition, Purchase Order and Sales order respectively.
---
applications:
- name: botnodejs
# command: node server.js
memory: 128M
buildpacks:
- nodejs_buildpack
services:
- connectivity-bot-01
- destination-bot-01
{
"dependencies": {
"axios": "^0.21.0",
"express": "^4.17.1"
}
}
//Load libraries
const axios = require('axios')
const express = require('express')
var url = require('url')
const app = express()
var queryParam;
//to get data from VCAP_SERVICES:: Applications running in Cloud Foundry gain access
//to the bound service instances via credentials stored in an environment variable called VCAP_SERVICES.
const VCAP_SERVICES = JSON.parse(process.env.VCAP_SERVICES);
const destSrvCred = VCAP_SERVICES.destination[0].credentials;
const conSrvCred = VCAP_SERVICES.connectivity[0].credentials;
app.listen(process.env.PORT, function () {
console.log('botNodeJS application started')
})
//to fetch auth token using URL, client and secret values
const _fetchJwtToken = async function (oauthUrl, oauthClient, oauthSecret) {
return new Promise((resolve, reject) => {
//prepare URL
const tokenUrl = oauthUrl + '/oauth/token?grant_type=client_credentials&response_type=token'
//prepare for the call
const config = {
headers: {
Authorization: "Basic " + Buffer.from(oauthClient + ':' + oauthSecret).toString("base64")
}
}
//backend get call to fetch auth token
axios.get(tokenUrl, config)
.then(response => {
resolve(response.data.access_token)
})
.catch(error => {
reject(error)
})
})
}
// Reads Destination configuration based on destinationName, destUri(fetched from VCAP_SERVICES)
// and jwtToken(fetched from _fetchJwtToken) . Result will be an object with Destination Configuration info
const _readDestinationConfig = async function (destinationName, destUri, jwtToken) {
return new Promise((resolve, reject) => {
//prepare URL
const destSrvUrl = destUri + '/destination-configuration/v1/destinations/' + destinationName
// preparation for the call
const config = {
headers: {
Authorization: 'Bearer ' + jwtToken
}
}
//backend get call to fetch destination config
axios.get(destSrvUrl, config)
.then(response => {
resolve(response.data.destinationConfiguration)
})
.catch(error => {
reject(error)
})
})
}
//podetails: entity using which application exposes the PO Data
app.get('/podetails', async function (req, res) {
// call destination service //
//fetch detination auth token
const destJwtToken = await _fetchJwtToken(destSrvCred.url, destSrvCred.clientid, destSrvCred.clientsecret)
//read destination config
const destiConfi = await _readDestinationConfig('S4_purchase_order', destSrvCred.uri, destJwtToken)
//to fetch query parameter from URL
queryParam = url.parse(req.url, true).query;
// call onPrem/Remote system using the connectivity service via the Cloud Connector//
// fetch connectivity auth token
const connJwtToken = await _fetchJwtToken(conSrvCred.token_service_url, conSrvCred.clientid, conSrvCred.clientsecret)
try {
// method to make a call to onPrem/Remote system, and save the result in variable "result"
const result = await _poDetails(conSrvCred.onpremise_proxy_host, conSrvCred.onpremise_proxy_http_port, connJwtToken, destiConfi)
res.json(result);
}
//catch block to handle any errors
catch (e) {
console.log('Catch an error: ', e)
res.json({ "d": { "error": "error" } })
}
})
//to make a backend call to the onPrejm/Remote system using connProxyHost, connProxyPort, ConnJwtToken (fetched
//using the connectivity service) and destiConfi (destination configuration fetched using destination service)
const _poDetails = async function (connProxyHost, connProxyPort, connJwtToken, destiConfi) {
return new Promise((resolve, reject) => {
// make target URL
const targetUrl = destiConfi.URL + "/C_PurchaseOrderTP(PurchaseOrder='" + queryParam.number + "',DraftUUID=guid'00000000-0000-0000-0000-000000000000',IsActiveEntity=true)"
//encode user creds fetched from the destination configuration
const encodedUser = Buffer.from(destiConfi.User + ':' + destiConfi.Password).toString("base64")
//preparation for the onPrem/Remote system call
const config = {
headers: {
Authorization: "Basic " + encodedUser,
'Proxy-Authorization': 'Bearer ' + connJwtToken,
'SAP-Connectivity-SCC-Location_ID': destiConfi.CloudConnectorLocationId
},
proxy: {
host: connProxyHost,
port: connProxyPort
}
}
// get call to the onPrem/Remote system to fetch data
axios.get(targetUrl, config)
.then(response => {
resolve(response.data)
})
.catch(error => {
reject(error)
})
})
}
//prdetails: entity using which application exposes the PR Data
app.get('/prdetails', async function (req, res) {
// call destination service
//fetch detination auth token
const destJwtToken = await _fetchJwtToken(destSrvCred.url, destSrvCred.clientid, destSrvCred.clientsecret)
//read destination config
const destiConfi = await _readDestinationConfig('S4_purchase_req', destSrvCred.uri, destJwtToken)
//to fetch query parameter from URL
queryParam = url.parse(req.url, true).query;
// call onPrem/Remote system using the connectivity service via the Cloud Connector//
// fetch connectivity auth token
const connJwtToken = await _fetchJwtToken(conSrvCred.token_service_url, conSrvCred.clientid, conSrvCred.clientsecret)
try {
// method to make a call to onPrem/Remote system, and save the result in variable "result"
const result = await _prDetails(conSrvCred.onpremise_proxy_host, conSrvCred.onpremise_proxy_http_port, connJwtToken, destiConfi)
res.json(result);
}
//catch block to handle any errors
catch (e) {
console.log('Catch an error: ', e)
res.json({ "d": { "error": "error" } })
}
})
//to make a backend call to the onPrem/Remote system using connProxyHost, connProxyPort, ConnJwtToken (fetched
//using the connectivity service) and destiConfi (destination configuration fetched using destination service)
const _prDetails = async function (connProxyHost, connProxyPort, connJwtToken, destiConfi) {
return new Promise((resolve, reject) => {
// make target URL
const targetUrl = destiConfi.URL + "/C_PurchaseReqnHeader(PurchaseRequisition='" + queryParam.number + "',DraftUUID=guid'00000000-0000-0000-0000-000000000000',IsActiveEntity=true)"
//encode user creds fetched from the destination configuration
const encodedUser = Buffer.from(destiConfi.User + ':' + destiConfi.Password).toString("base64")
//preparation for the onPrem/Remote system call
const config = {
headers: {
Authorization: "Basic " + encodedUser,
'Proxy-Authorization': 'Bearer ' + connJwtToken,
'SAP-Connectivity-SCC-Location_ID': destiConfi.CloudConnectorLocationId
},
proxy: {
host: connProxyHost,
port: connProxyPort
}
}
// get call to the onPrem/Remote system to fetch data
axios.get(targetUrl, config)
.then(response => {
resolve(response.data)
})
.catch(error => {
reject(error)
})
})
}
//sodetails: entity using which application exposes the SO Data
app.get('/sodetails', async function (req, res) {
var destinationNames = 'S4_sales_order';
// call destination service //
//fetch detination auth token
const destJwtToken = await _fetchJwtToken(destSrvCred.url, destSrvCred.clientid, destSrvCred.clientsecret)
//read destination config
const destiConfi = await _readDestinationConfig( destinationNames, destSrvCred.uri, destJwtToken)
//to fetch query parameter from URL
queryParam = url.parse(req.url, true).query;
// call onPrem/Remote system using the connectivity service via the Cloud Connector//
// fetch connectivity auth token
const connJwtToken = await _fetchJwtToken(conSrvCred.token_service_url, conSrvCred.clientid, conSrvCred.clientsecret)
try {
// method to make a call to onPrem/Remote system, and save the result in variable "result"
const result = await _soDetails(conSrvCred.onpremise_proxy_host, conSrvCred.onpremise_proxy_http_port, connJwtToken, destiConfi)
res.json(result);
}
//catch block to handle any errors
catch (e) {
console.log('Catch an error: ', e)
res.json({ "d": { "error": "error" } })
}
})
//to make a backend call to the onPrejm/Remote system using connProxyHost, connProxyPort, ConnJwtToken (fetched
//using the connectivity service) and destiConfi (destination configuration fetched using destination service)
const _soDetails = async function (connProxyHost, connProxyPort, connJwtToken, destiConfi) {
return new Promise((resolve, reject) => {
// make target URL
const targetUrl = destiConfi.URL + "/zabibot01('" + queryParam.number + "')"
//encode user creds fetched from the destination configuration
const encodedUser = Buffer.from(destiConfi.User + ':' + destiConfi.Password).toString("base64")
//preparation for the onPrem/Remote system call
const config = {
headers: {
Authorization: "Basic " + encodedUser,
'Proxy-Authorization': 'Bearer ' + connJwtToken,
'SAP-Connectivity-SCC-Location_ID': destiConfi.CloudConnectorLocationId
},
proxy: {
host: connProxyHost,
port: connProxyPort
}
}
// get call to the onPrem/Remote system to fetch data
axios.get(targetUrl, config)
.then(response => {
resolve(response.data)
})
.catch(error => {
reject(error)
})
})
}
We are now at the end of this blog post. During the course of this blog post, successfully created Destinations, created Connectivity & Destination instance. Once done, created a nodeJS application in the local system and deployed the nodeJS application to the SAP BTP using CLI. Once all is done we have a public service API.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
18 | |
10 | |
8 | |
5 | |
4 | |
4 | |
3 | |
3 | |
2 | |
2 |