5

SAP CAP: Consume SAP API Hub Sandbox Data as an external service Part- 1

 1 year ago
source link: https://blogs.sap.com/2022/03/17/sap-cap-consume-sap-api-hub-sandbox-data-as-an-external-service-part-1/
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
March 17, 2022 2 minute read

SAP CAP: Consume SAP API Hub Sandbox Data as an external service Part- 1

0 3 845

What: 

This blog post is about how to consume external services from BTP after deploying your service/application. Here we take Business Partner API for this example.

How:

Let’s not going into detail how to create CAP project. I am starting from importing the API_BUSINESS_PARTNER from api.sap.com.

Download the EDMX file from api hub and copy it to the main folder.

   

3-13.png

Now run cds import command

$ cds import API_BUSINESS_PARTNER.edmx

The API_BUSINESS_PARTNER.edmx has been imported to the folder srv/external, also it generates API_BUSINESS_PARTNER.csn file. This CSN file used by the CDS framework.

 

3-15.png

It updates the package.json file in cds requires section with the external service.

Now lets create a business-service.cds file to define our Service.

using { API_BUSINESS_PARTNER as abp } from './external/API_BUSINESS_PARTNER.csn';

service BusinessService {  
    entity A_BusinessPartner as projection on abp.A_BusinessPartner {
        key BusinessPartner, Customer, BusinessPartnerFullName, BusinessPartnerGrouping, BusinessPartnerUUID, OrganizationBPName1 
    }
}

Create a custom handler business-service.js file to read our entity.

const cds = require('@sap/cds');
module.exports = cds.service.impl(async function() {
    const bp = await cds.connect.to('API_BUSINESS_PARTNER');    
    this.on('READ', 'A_BusinessPartner', async req => {        
        return bp.run(req.query);       
    });
});

For accessing Sandbox data from our project we need to provide the URL of the sandbox and do the changes in our package.json file

"cds": {
    "requires": {
      "API_BUSINESS_PARTNER": {
        "kind": "odata-v2",
        "model": "srv/external/API_BUSINESS_PARTNER",
        "credentials": {
            "url": "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER"
        }
      }
    }
  }

Let check our project –

$ cds watch
4-12.png
5-7.png

Click on A_BusinessPartner

6-9.png

Oops!! As we are accessing sandbox data. We need to provide the API key, which you will find in api.sap.com account. Which is unique api key. Copy the api key from api hub and make some changes in package.json file.

7-9.png
"credentials": {
            "url": "https://sandbox.api.sap.com/s4hanacloud/sap/opu/odata/sap/API_BUSINESS_PARTNER",
            "headers": {
                "APIKey": "<api-key>"
            }
        }

Now again check your service in browser

8-9.png

Data is coming from the sanbox api hub.

In this post we are able to get data from an external service sandbox api hub.

In Part- 2, i will deploy the app to btp and show you how to access sandbox data using destination service


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK