35

BTP CAP : How to Connect to Remote Services Locally in CAP Node JS Application

 1 year ago
source link: https://blogs.sap.com/2022/12/12/btp-cap-how-to-connect-to-remote-services-locally-in-cap-node-js-application/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Introduction

In this blog post, we will learn How to Connect to Cloud Services From CAP Application Locally

Though CAP Official Document is clearly explained hybrid-testing, I would like to illustrate with an use-case  “Connect to Northwind Cloud Service Locally in CAP Node JS Application using Function Import”

Here Service can be from any external remote system or SAP System like S/4 HANA

Example Business Partner OData service in SAP S/4HANA

Reference : https://cap.cloud.sap/docs/advanced/hybrid-testing

I assume you already aware how to integrate any Remote Service on BTP

  • If it is Cloud Service for example (S/4 HANA Cloud ) then you need to use BTP Destination service
  • If it is On Premise System which requires VPN to connect to then you need to use BTP BTP Connectivity service & SAP Cloud Connector

Target Audience

Developers

Prerequisites

I assume you already configured required services for CAP APP

Destination

Authorization and Trust Management Service XSUAA  Service

1-BTP-DestinationInstance-Key.png
2-BTP-Auth-Instance-Key.png

Sample Source Code

Below is sample CAP Node JS Source code to connect to BTP Destination

this.on(“getRemoteNWData”, async req => {
letresponse;
constcpqservice = awaitcds.connect.to(‘ext_Northwind’)
LOG.info(“start demo Northwind connecting”)
response = awaitcpqservice.send({
query:’GET /v2/northwind/northwind.svc/Products’,
headers: {
‘Content-Type’:’application/json’
LOG.info(“end Northwind connection “)
returnresponse;
} catch (error) {
req.error(500, error.message)
You can easily test your CAP application using a local database and mock ups. But at some point, you’re going to want to test with real cloud services. Of course, you can always deploy your application to the cloud.

Approach 1 [deprecated]

  • Create default-env.json
  • Copy VCAP_SERVICES Values from BTP Environment Variable of your application
  • run cds watch from terminal
1-Default-env.png

Note :

https://cap.cloud.sap/docs/node.js/cds-env?q=default-env.json#in-default-envjson

Recently The use of default-env.json is deprecated

Approach 2 [Hybrid Testing]

A new approach to bind BTP services locally is to use cds bind

#1) cds bind -2 sap-organickart-destination-instance:sap-organickart-srv-key

Retrieving data from Cloud Foundry…
[bind] – Binding destinations to Cloud Foundry service sap-organickart-destination-instance key sap-organickart-srv-key with kind destinations
[cds] – :: .cdsrc-private.json
[bind] – Saving bindings to .cdsrc-private.json in profile hybrid
[bind] –
[bind] – TIP: Run with cloud bindings: cds watch –profile hybrid

Ccds-bind-2-destination.png

#2)  cds bind -2 sap-organickart-auth:sap-organickart-auth-key

Retrieving data from Cloud Foundry…
[bind] – Binding auth to Cloud Foundry service sap-organickart-auth key sap-organickart-auth-key with kind xsuaa
[cds] – :: .cdsrc-private.json
[bind] – Saving bindings to .cdsrc-private.json in profile hybrid
[bind] –

cds bind -2 servicename:keyname will generate cdsrc-private.json file in project as shown below

cdsrc-private.png

Unlike Default-env.json which is un secure to maintain service sensitive data ,Running the “cds watch” in hybrid mode, which allows you to retrieve service credentials from BTP CF/Kyma on runtime

#3)  cds watch –profile hybrid

cds serve all –with-mocks –in-memory? –profile hybrid
watching: cds,csn,csv,ts,mjs,cjs,js,json,properties,edmx,xml,env,css,gif,html,jpg,png,svg…
live reload enabled for browsers
[bind] – resolving cloud service bindings…
[bind] – bound auth to Cloud Foundry service sap-organickart-auth key sap-organickart-auth-key
[bind] – bound destinations to Cloud Foundry service sap-organickart-destination-instance key sap-organickart-srv-key

3-cds-watch-profile-hybrid.png

Now you can browse your function import using local host port which fetch Products from NorthWind Remote  Services

Example : https://port4004-workspaces-ws-x.eu10.applicationstudio.cloud.sap/auth/getRemoteNWProducts()

4-NorthWind-Products.png

Feel free to also ‘Like’ ,‘Share’ , ‘Follow’ me to get new updates.

Happy Testing 🙂 !


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK