Skip to Content
0

Consume Http destination in xsjs file in XSA Advanced Application

Mar 13 at 02:56 PM

136

avatar image

Hello all!

I'm developing an xsa advanced application and I would like to consume an API through an xsjs file, but I find myself unable to find the right way to do it.

I read that it is possible to create an http destination, but I can't make it to work.

Does anybody know how to consume an API through an xsjs file?

I'm using Hana express edition with its webIDE.

Thanks a lot!

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Steven De Saeger Mar 19 at 04:14 PM
2

Hi,

A similar functionality is part of the openSAP HANA XSA SPS02 course excercises ... in there an HTTP call is made via xsjs ...

Basically you need to register an user provided service with the HTTP settings, define the resource in the mta and require it in your xsjs module. Then you can access it in your code:

var dest = $.net.http.readDestination("sap.hana.democontent.epm.services", "images.SDS");     
var client = new $.net.http.Client();     
var req = new $.web.WebRequest($.net.http.GET, search);     
client.request(req, dest);     
var response = client.getResponse();
...

The exercises can be found on GitHub at HANA XSA SPS02 - have a look at the ZIP file sap.hana.democontent.epm.services.zip and in there at the file 'outboundTest.xsjs'.

Show 4 Share
10 |10000 characters needed characters left characters exceeded

Thank you for the answer!

I had already created the xshttpdest file and required it in the xsjs, but I don't know how to register the service, some places say I have to register an http destination via de xs admin tools, but I can't find it.

Also, when you talk about defining the resource in the mta, how should I do that?

I'm using the Hana express edition.

Thank you very very much!

0

Hi David,

Registration can be done via the command line tool - make sure you are in your own space

eg

xs cups <your user provided service name> -p '{ "host": "<url>", "port": "<port>", "tags": ["xshttpdest"] }'

Do a search on the xs cups command and you will find the details.

Once that ran the service should be visible when you run xs services

Declaring the resource can be done via the webide - just open the mta file and go to the resources tab.

Do a search on XSA tutorials and you will find plenty of examples on how to configure the mta yaml file ... you will need to do this for every resource you want to use when developing in the XSA MTA model.

Kr,

Steven

1

Hello again Steven,

Sorry for asking so many questions, but I'm still a bit lost with this and I can't get to find any documentation about this.

I already created the user provided service via the xs cups command and added the new resource to the mta file and required it in the js module (also im a bit confused about which parameters should I used for the the http resource, since i read the service-name is optional, but the config is required).

So at this point, my question is: do I need an xshttpdest file? If so, I guess I need to use the readDestination command, but wouldn't it be redundant to create a file an a user-provided service which bot contain the same info?

And if I don't need to create that file, how do i require it inside the xsjs file? In the readDestination command I must specify a package and a file name...

Last question would be, do you happen to have direct links to any kind of info that would help me out with this? Because, to be honest, it's starting to drive me completely insane.

Thank you very much again for your help

0

Hi David,

I had a look into the openSAP content and there is indeed an xshttpdest file as well ( with redundant info ). I am not sure either why both are required ... but ofc the XSA provides XSJS backward compatibility so probably both the service and the file need to exist to keep both CloudFoundry and XSJS happy (or at least that is my guess ).

Have a look at the following GIT branch ... it contains all the code of the openSAP course ...

SAP GIT

Under '/core_js/lib/sap/hana/democontent/epm/services/' you can find the destination file and the xsjs 'outboundtest' which perform the external HTTP call

Also the mta.yaml file in the root will show you which parameters to fill in ...

Hope this helps,

Kr,

Steven

0
Steven De Saeger Apr 16 at 07:07 AM
0

Hi David,

Did you manage to solve the problem with the HTTP destination ?

I stumbled upon the following documentation within the 'readme' documentation of the standard sap node js 'xsjs' module - so bascially it confirms that the UP service properties are used first - with a fallback towards the design time file.

## Destinations support

#### Via user provided services
By default the compatibility layer supports destinations configuration via user provided services. The destination name (the repo resource id, e.g. package + '.' + xshttpdest name) is matched to service name.

Example content of VCAP_SERVICES:

```js
"VCAP_SERVICES": {
  "user-provided": [
    {
      "label": "user-provided",
      "name": "foobar.httpdest.mydest",
      "credentials": {
        "host": "some.host",
        "port": 8088,
        "username": "user",
        "password": "secret"
      }
    }
  ]
}
```
Example usage in XSJS code:
```js
var destination = $.net.http.readDestination('foobar.httpdest', 'mydest');
```
If there is no service in VCAP_SERVICES with same name as the destination requested, an exception is thrown.

When destination is read the content of the design time descriptor is merged with the properties provided in the user provided service. Property values of the UP service override DT descriptor values.

Share
10 |10000 characters needed characters left characters exceeded