Skip to Content

How to consume a external services.

Hello Cap Experts,

I've initialized a simple CAP node project and

I would like to know how to consume a services inside other service. I deployed my first service in Foundry, and It is works.

entity Location : managed {
  key ID  : Integer;
  country : String(111);
}
ID;country;
1;PERU
2;USA 

For this services I have been create a basic destination in my subaccount with not authentication with the name: DEST_LOCATION and instace service inside of my space with the name: dest_instance

The second services has to call the first one and add an extra field

entity Person : managed {
  key ID : Integer;
  name   : String(111);
  locationId: Integer;
}
ID;name;Country;
1;Peter;1
2;Jhon;2

With Person?&$expand=Location And the response of this serve must have this result:

"value": [
    {
      "ID": "1",
      "name": "Peter",
      "locationId": 1,
      "country": "PERU"
    },
    {
      "ID": "2",
      "name": "Jhon",
      "locationId": 2,
      "country": "USA"
    }
]  

This is my package.json

ID: person
_schema-version: "3.1"
version: 0.0.1
parameters:
  enable-parallel-deployments: true
modules:
  - name: person-db
    type: com.sap.xs.hdi
    path: gen/db
    build-parameters:
      ignore: ["node_modules/"]
    parameters:
      memory: 256M
      disk-quota: 256M
    requires:
      - name: DB_PERSON
  - name: person-srv
    type: nodejs
    path: gen/srv
    build-parameters:
      ignore: ["node_modules/"]
    parameters:
      memory: 512M
      disk-quota: 256M
    provides:
      - name: srv_api
        properties:
          url: ${default-url}
    requires:
      - name: DB_PERSON
      - name: uaa_des
      - name: dest_instance
      - name: srv_api
        group: destinations
        properties:
          name: DEST_LOCATION
          url: '~{url}'
          forwardAuthToken: true
resources:
  - name: DB_PERSON
    type: com.sap.xs.hdi-container
    properties:
      hdi-container-name: DB_PERSON
    parameters:
      config:
        schema: SCHEMA_PERSON
  - name: uaa_des
    type: org.cloudfoundry.existing-service
  - name: dest_instance
    parameters:
      service-plan: lite
      service: destination
    type: org.cloudfoundry.existing-service

And my package.json

{
  "name": "person",
  "version": "1.0.0",
  "description": "A simple CAP project.",
  "repository": "<Add your repository here>",
  "license": "ISC",
  "dependencies": {
    "@sap/audit-logging": "^3.1.0",
    "@sap/logging": "^5.1.0",
    "axios": "0.19.0",
    "express": "^4.17.1",
    "http-proxy-middleware": "^0.20.0",
    "@sap/cds-odata-v2-adapter-proxy": "latest",
    "@sap/cds": "^3",
    "@sap/xssec": "^2.2.4",
    "hdb": "^0.17.1",
    "passport": "^0.4.1",
   
  },
  "scripts": {
    "start": "npx cds run"
  },
  "cds": {
    "requires": {
      "db": {
        "kind": "hana",
        "model": [
          "db",
          "srv"
        ]
      },
      "externalService": {
        "kind": "rest/odata",
        "model": "/Address",
        "credentials": {
          "destination": "DEST_LOCATION",
        },
        "uaa": {
          "kind": "xsuaa"
        }
      }
    }
  }
}   

Now, I would like if anyone can help me the next steps. Should I extend de cds to add the field in country in person.cds from my db?.

In my services person.cds should include the extend? In my services person.js Should I call the destination to get the entinty

const srv = cds.connect.to('DEST_LOCATION')
const { Location } = srv.Location;

.....

I have tried many things, any help would be appreciated.

Thanks

Add a comment
10|10000 characters needed characters exceeded

  • Hi David and Gregor,

    I follow the official documentation and I saw your example Gregor and I thought is more complex than what I am looking for. I will start to retur just all the data from the external service, but I did not find the way to return the data.

    In the services admin.js from person services, I want to return all the data form location.

    const cds = require('@sap/cds')
    
    
    module.exports = srv => {
    	srv.on('READ', 'Location', async ( req) => {
    		try{
                     /* Here I want to return the extenal services data */
    		} catch (e) {
    		    debugger;
    			console.log("Error: " + e.message)
    		    console.log("Stack: " + e.stack)
    		}
    	})
    } 
    

    I upload all the project to this repo. https://github.com/mrjerte/consume-cap-service

    I will appreciate yours help.

    Thank you very much.

Related questions

3 Answers

  • Posted on Mar 12 at 07:28 PM

    Hi,

    Please have a look at the official documentation on how to consume services:

    https://cap.cloud.sap/docs/guides/consuming-services

    Best regards,
    David

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 12 at 08:54 PM

    I would think that my example app epmbp-consumer-app can guide you together with the documentation that David linked.

    Add a comment
    10|10000 characters needed characters exceeded

  • Posted on Mar 14 at 03:25 PM

    Hello,

    maybe you start with returning a static JSON that contains the fields of location entity.

    You're right, my example in service.js#L81 is a bit complex because I use the SAP Cloud SDK. But maybe the example controller-api is a better fit as it uses the CAP Service API. Check out controller-service.js#L19.

    Best regards
    Gregor

    Add a comment
    10|10000 characters needed characters exceeded

Before answering

You should only submit an answer when you are proposing a solution to the poster's problem. If you want the poster to clarify the question or provide more information, please leave a comment instead, requesting additional details. When answering, please include specifics, such as step-by-step instructions, context for the solution, and links to useful resources. Also, please make sure that you answer complies with our Rules of Engagement.
You must be Logged in to submit an answer.

Up to 10 attachments (including images) can be used with a maximum of 1.0 MB each and 10.5 MB total.