SAP CAP: Consume SAP API Hub Sandbox Data as an external service Part- 2
source link: https://blogs.sap.com/2022/03/18/sap-cap-consume-sap-api-hub-sandbox-data-as-an-external-service-part-2/
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.
SAP CAP: Consume SAP API Hub Sandbox Data as an external service Part- 2
What:
This blog post is about consuming external services from BTP after deploying your service/application. This is the continuation of the previous post Part- 1.
Here we are going to deploy and access the same external service from the cloud foundry
How:
Deploying Application
Now we are going to deploy our CAP project in SCP Cloud Foundry
Before deploying using the command, we need to build the project.
$ cds build/all
Create mta.yaml file by using the below command.
$ cds add mta
MTA file will look like below –
---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
- builder: custom
commands:
- npm ci
- npx -p @sap/cds-dk cds build --production
modules:
- name: BusinessPartner-srv
type: nodejs
path: gen/srv
parameters:
buildpack: nodejs_buildpack
build-parameters:
builder: npm-ci
provides:
- name: srv-api # required by consumers of CAP services (e.g. approuter)
properties:
srv-url: ${default-url}
As this file does not hold enough information to run the application in the Cloud foundry, We need two resources binding with our service application to up and running in CF.
- Destination service
- xsuaa service
Let create the Destination service in BTP
Login to your account and search for destination under service marketplace.
Click on create Button and provide the Instance name. In my case, I have created demo-destination instance.
Service: Destination Service
Plan: lite
Runtime Environment: Cloud Foundry
Space: dev
Instance Name: demo-destination
Click on demo-destination instance and configure a new destination
URL=https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER/
Name=API_BUSINESS_PARTNER
ProxyType=Internet
Type=HTTP
Authentication=NoAuthentication
Description=Business Partner SandBox API
Now we have created one service instance for our application. Let’s create xs-security.json for uaa service instance.
Run the below command
$ cds compile srv/ --to xsuaa > xs-security.json
We have to update our MTA file to make binding these two services with our application
---
_schema-version: '3.1'
ID: BusinessPartner
version: 1.0.0
description: "A simple CAP project."
parameters:
enable-parallel-deployments: true
build-parameters:
before-all:
- builder: custom
commands:
- npm ci
- npx -p @sap/cds-dk cds build --production
modules:
- name: BusinessPartner-srv
type: nodejs
path: gen/srv
parameters:
buildpack: nodejs_buildpack
build-parameters:
builder: npm-ci
provides:
- name: srv-api # required by consumers of CAP services (e.g. approuter)
properties:
srv-url: ${default-url}
requires:
- name: demo-destination
- name: businesspartner-uaa
resources:
- name: demo-destination
type: org.cloudfoundry.existing-service
parameters:
service-name: demo-destination
- name: businesspartner-uaa
type: org.cloudfoundry.managed-service
parameters:
path: ./xs-security.json
service: xsuaa
service-plan: application
config:
xsappname: businesspartner-${org}-${space}
tenant-mode: dedicated
We have to change the credentials to access the destination service in the package.json file.
"cds": {
"requires": {
"API_BUSINESS_PARTNER": {
"kind": "odata-v2",
"model": "srv/external/API_BUSINESS_PARTNER",
"[development]": {
"credentials": {
"url": "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER",
"headers": {
"APIKey": "<api-key>"
}
}
},
"[production]": {
"credentials": {
"destination": "API_BUSINESS_PARTNER"
}
}
},
"uaa": {
"kind": "xsuaa"
}
}
}
When you are going to access sandbox data for your local development you have to use “[development]” credentials.
Follow the below commands to deploy your cap project –
$ cds build/all
$ mbt build -t ./
$ cf deploy BusinessPartner_1.0.0.mtar
BusinessPartner_1.0.0.mtar, is generated after your MBT build command.
Now check in the BTP under the application section. The application has been deployed and it is has been Started.
Click on the application URL.
Click on A_BusinessPartner
Oops!! It was working fine in our BAS.
The reason is we are not passing APIkey to access the sandbox data. In package.json file we pass only destination name inside “[production]”, not the apikey.
There are two ways I found how to use API key for the production environment.
- Set the apikey in as an environment variable and use it in your code. In every request send it in as a header request.
- We can modify the destination configuration in our destination service.
Here I am we are going to follow the second approach.
Open the destination service and an additional parameter
URL.headers.apiKey – <api-key>
Now again click on A_BusinessPartner
Response is coming from the API hub sandbox.
Now you know how to access API Hub sandbox data from a cap application
NOTE on 3rd December 2022:
If you follow this blog you need to lower the version “@sap/cds”: “^5” in package.json file.
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK