Skip to content

Commit

Permalink
implement call to SAP Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
gregorwolf committed Jan 19, 2024
1 parent 4e3f63a commit 286c5c1
Show file tree
Hide file tree
Showing 7 changed files with 82 additions and 11 deletions.
4 changes: 2 additions & 2 deletions app/config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Azure AD B2C sample endpoint
const clientId = "e760cab2-b9a1-4c0d-86fb-ff7084abd902";
const azureADconfig = {
const azureADconfigX = {
clientId: clientId,
scopes: [clientId],
authority:
Expand All @@ -16,7 +16,7 @@ const azureADconfigCSWb2c = {
};
// CSWEntraID
const clientIdCSWEntraID = "19702d90-5fb8-4d76-a6c9-557f55c771e8";
const azureADconfigCSWEntraID = {
const azureADconfig = {
clientId: clientIdCSWEntraID,
scopes: ["openid","profile","api://19702d90-5fb8-4d76-a6c9-557f55c771e8/SAP.ReadWrite"],
authority:
Expand Down
16 changes: 16 additions & 0 deletions app/msal-2/authPopup.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,4 +147,20 @@ function getBTPJWT() {
});
}

function readProductsFromSAPGraph() {
getTokenPopup(tokenRequest)
.then((response) => {
callBookshop(
bookshopConfig.readProductsFromSAPGraphEndpoint.method,
bookshopConfig.readProductsFromSAPGraphEndpoint.path,
bookshopConfig.readProductsFromSAPGraphEndpoint?.body,
response.accessToken,
updateUI
);
})
.catch((error) => {
console.error(error);
});
}

selectAccount();
5 changes: 5 additions & 0 deletions app/msal-2/bookshopConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,9 @@ const bookshopConfig = {
method: "POST",
body: "{}",
},
readProductsFromSAPGraphEndpoint: {
path: "/odata/v4/catalog/readProductsFromSAPGraph",
method: "POST",
body: "{}",
},
};
7 changes: 7 additions & 0 deletions app/msal-2/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,13 @@ <h5 class="card-title" id="WelcomeMessage">
>
get BTP JWT
</button>
<button
class="btn btn-primary"
id="readProductsFromSAPGraph"
onclick="readProductsFromSAPGraph()"
>
Read Products from SAP Graph
</button>
</div>
</div>
</div>
Expand Down
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@
"max": 10
}
},
"SAPGraph": {
"kind": "rest",
"credentials": {
"destination": "SAPGraph",
"requestTimeout": 30000
}
},
"uaa": {
"kind": "xsuaa"
},
Expand Down
5 changes: 3 additions & 2 deletions srv/catalog-service.cds
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ service CatalogService @(requires: 'authenticated-user') {
@readonly
entity Products as projection on external.SEPMRA_C_PD_Product;

action getBTPJWT() returns String;
action readSAPLogonTicket() returns String;
action getBTPJWT() returns String;
action readProductsFromSAPGraph() returns array of String;
action readSAPLogonTicket() returns String;
}
49 changes: 42 additions & 7 deletions srv/catalog-service.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
const cds = require("@sap/cds");
const LOG = cds.log("catalog-service");
const { AuthClient } = require("./AuthClient");
const axios = require("axios");
const { getDestination } = require("@sap-cloud-sdk/connectivity");

function getAuthToken(req) {
const authHeader = req._.req.headers["authorization"];
Expand All @@ -12,13 +14,7 @@ module.exports = async function (srv) {
const biscuitService = await cds.connect.to("biscuit");
const productService = await cds.connect.to("SEPMRA_PROD_MAN");

srv.on("READ", "Products", async (req) => {
const token = getAuthToken(req);
LOG.debug("Token: " + token);
return productService.run(req.query);
});

srv.on("getBTPJWT", async (req) => {
async function getBTPJWT(req) {
const token = getAuthToken(req);
const authClient = new AuthClient();
LOG.debug("Token: " + token);
Expand All @@ -28,6 +24,45 @@ module.exports = async function (srv) {
);
LOG.debug("BTP Access Token: " + btpAccessToken);
return btpAccessToken;
}

srv.on("READ", "Products", async (req) => {
const token = getAuthToken(req);
LOG.debug("Token: " + token);
return productService.run(req.query);
});

srv.on("getBTPJWT", async (req) => {
return getBTPJWT(req);
});

srv.on("readProductsFromSAPGraph", async (req) => {
const sapGraph = await cds.connect.to("SAPGraph");
const btpAccessToken = await getBTPJWT(req);
try {
const path =
"/api/s4hc/sap.s4/A_Product?$top=10&$select=Product,Brand,CountryOfOrigin&$count=true";
/*
// CAP request does not work due to authentication issue
const products = await sapGraph.send({
event: "get",
path,
headers: {
Authorization: `Bearer ${btpAccessToken}`,
},
});
*/
const graphDest = await getDestination({ destinationName: "SAPGraph" });
const graphUrl = graphDest.url + path;
let products = await axios.get(graphUrl, {
headers: {
Authorization: `Bearer ${btpAccessToken}`,
},
});
return products?.data?.value;
} catch (error) {
LOG.error("Error Message: " + error.message);
}
});

srv.on("readSAPLogonTicket", async (req) => {
Expand Down

0 comments on commit 286c5c1

Please sign in to comment.