HTTP Destination

In the article Introduction to SAP HANA XS we explained the basic overview of SAP HANA XS. In this article we will focus on a specific feature of HANA XS – HTTP Destination.

Introduction:

Suppose there is a service running on a different server and you want to access the data from HANA XS application.
This can be achieved by using HTTP destination and XSJS Outbound API.


Example:

There is a financial service by Yahoo on Internet (download.finance.yahoo.com) which latest value of a share price.
You can pass the company name as a parameter and get the latest share price. For example to get the share price of SAP, we can use below service:
http://download.finance.yahoo.com/d/quotes.csv?f=a&s=SAP

Now, suppose we want to call this service from HANA XS application and save the latest share value in a HANA table. We can do that by using HTTP destination.
  1. We first need to create an HTTP destination file
  2. In HTTP destination file we specify the service host, port, authentication type and other security details.

For example:

host = "download.finance.yahoo.com";
port = 80; 
description = "my stock-price checker";
useSSL = false;
pathPrefix = "/d/quotes.csv?f=a";
authType = none;
useProxy = false;
proxyHost = "";
proxyPort = 0;
timeout = 0;

3. We can use this HTTP destination to call the external resource directly from an XSJS application.

Now let us create a complete application to read data from this yahoo financial service and get the value of SAP share price.

Prerequisites:

Steps to create complete application:

1.Create HANA XS Project as mentioned in Create Your First HANA XS Application using HANA Studio
2. Right click and click on New à Others à XS HTTP Destination Configuration. Specify the file name as “YahooHttpDestination”.


Note that HTTP destination have the file extension .xshttpdest

3. Copy paste the below code.

host = "download.finance.yahoo.com";
port = 80; 
description = "my stock-price checker";
useSSL = false;
pathPrefix = "/d/quotes.csv?f=a";
authType = none;
useProxy = true;
proxyHost = "proxy";
proxyPort = 8080;
timeout = 0;

4. Create an XSJS file GetStockValue.xsjs and paste below code.

var stock = $.request.parameters.get("stock");
var amount = $.request.parameters.get("amount");
var dest = $.net.http.readDestination("http-destination", "YahooHttpDestination");
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.GET, "&s=" + stock);
client.request(req, dest);
var response = client.getResponse();
var co = [],
    he = [];
for (var c in response.cookies) {
    co.push(response.cookies[c]);
}
for (var c in response.headers) {
    he.push(response.headers[c]);
}
var body = undefined;
if (response.body) var body = response.body.asString();
$.response.contentType = "application/json";
var res = parseInt(response.body.asString()) * amount;
$.response.setBody(amount + " of your " + stock + " shares are worth: " + res);

5. Run the XSJS service and pass query parameter as: ?amount=100&stock=SAP


Download Full Source Code:

Click here to download the full source code.

Continue reading:

No comments:

Post a Comment