Skip to Content

Last time I blogged here on SDN I said I was starting a series on REST. While this post is not exactly the continuation I had in mind it is a post about REST. In fact it is a post about accessing the SAP River system via the REST interface.

River is a new on demand platform that is being built out by SAP. If you need some background please go and read the blogs by Dick Hirsch, Juergen Schmerder and Thorsten Franz.

In this blog I am going to describe the REST system that you get for free when developing an application on River. To do that i am going to use a little PHP. Firstly because I am familiar with PHP and while you could achieve exactly the same with Java, Ruby, Python or even Perl all you need is to be able to make an http call and catch the return code and the cookies.

All you need in terms of documentation can be found at https://preview.cyrenea.com/api/docs/rest/

So lets go through the way to logon to the river server. If you want to follow along get a river beta account by sending a mail to basicedge_info@sap.com. As I said I am going to be doing my example in PHP and I am using the Http_Client from the Zend framework. It’s a nice object oriented framework which will make our code look nice any easy to understand what is going on.

We will go through this section by section. Firstly let’s set up a connection to the River server.

 // set up the include path to include Zend Framework
 include 'Zend/Http/Client.php';
 $riverServer = 'https://preview.cyrenea.com';
 $client = new Zend_Http_Client( $riverServer . '/api/security/login', array(
      'maxredirects' => 0,
      'timeout' => 30))

This is just setting up the HTTP Client connection to the River server and the login URL.

Next set up the cookie handler and the POST to login:

$client->setCookieJar();
$client->setParameterPost(array(
     'j_username' => 'username',
     'j_password' => 'Passw0rd',
     'openid_identifier' => '', // openid not working yet
     'longLogin' => 'yes', // sets a cookie for 14 days
));

The cookie jar is a convenient place to store the authentication cookie that River will send back to us. We set up the parameters used for the login POST. This HTTP Client class will make sure they are set as form encoded. You could put your username and password in a configuration file that is not reachable by the web. This would be more secure.

So lets execute that post and see what it gets back to us.

$response = $client->request('POST');
// 302 is successful
if ($response->isRedirect()) {
// something to do if successful
// if you want to look at cookies ...
     // $cookies = $client->getCookieJar()->getAllCookies(Zend_Http_CookieJar::COOKIE_STRING_ARRAY);
} else {
     // throw to a nice error page
}

As the documentation says a 302 response is a successful login and the authentication cookie will have been returned.

Now to use our connection and do something useful with River:

$restBase = '/api/rest';
//Now lets make another call to get something useful from river in json
$client->setUri( $riverServer . $restBase);
$client->setHeaders(array('x-accept' =>'application/json' )); // XML by default
$response = $client->request('GET');
$account = json_decode( $response->getBody() );
echo '\n';
print_r($account);
echo '\n';

By calling the base Rest URI we will get a list of the ‘accounts’ that we have in our account. By adding the header on line 4 we can get the response in JSON which if you are going to be sending it to a JavaScript program this could be useful. Otherwise we could leave it as XML and use the PHP SimpleXML class to process it.

I used the PHP JSON decoder to turn it into native PHP objects and arrays for easy of processing.

For me this returned:

stdClass Object ( [GetFolderItemsResponse] => stdClass Object ( [Item] => Array (
     [0] => stdClass Object ( [Name] => AppsComDemo [ItemType] => Document )
     [1] => stdClass Object ( [Name] => SAPMentorApplications [ItemType] => Document )
     [2] => stdClass Object ( [Name] => VideoDemo [ItemType] => Document ) ) ) )

This was a useful exercise to see how the JSON was parsed. I could now loop at the structure and make further REST calls:

echo '\n';
foreach ( $account->GetFolderItemsResponse->Item as $item ) {
     echo $item->Name . '\n';
     $client->setUri( $riverServer . $restBase . '/' . $item->Name);
     echo $client->request('GET')->getBody() . '\n';
}
echo '\n';

In this example I am looping over the all the accounts and obtaining the account name. I am adding this to the base REST call and making a GET request.

AppsComDemo {"GetFolderItemsResponse":{"Item":[
{"Name":"Test123","ItemType":"Document"},
{"Name":"System Integration","ItemType":"Document"},
{"Name":"System","ItemType":"Document"},
{"Name":"System Metrics","ItemType":"Document"}]}}
SAPMentorApplications {"GetFolderItemsResponse":{"Item":[
{"Name":"Fundraising Management","ItemType":"Document"},
{"Name":"SAP Mentors","ItemType":"Document"},
{"Name":"System Integration","ItemType":"Document"},
{"Name":"System","ItemType":"Document"},
{"Name":"System Metrics","ItemType":"Document"}]}} VideoDemo {"GetFolderItemsResponse":{"Item":[
{"Name":"HelloRiver","ItemType":"Document"},
{"Name":"System Integration","ItemType":"Document"},
{"Name":"System","ItemType":"Document"},
{"Name":"System Metrics","ItemType":"Document"}]}}

As you will see in the REST documentation the URI scheme for is rather simple:

https://river.rio.cyrenea.com/api/rest/{account}/{application}/{collection}/{record}

Today we have looked at retrieving the accounts and applications. To get to the collections and records is a simple extension of what I have outlined here.

As I stated before you don’t need to use PHP for this. PHP, in the Zend HTTP Client, has a very easy way to store the Authentication required and make the calls the REST api. There are many languages you could choose to replicate this example.

I trust that this has given you more insight into River REST api hopefully it will give you a few ideas on how you could use it.

To report this post you need to login first.

5 Comments

You must be Logged on to comment or reply to a post.

  1. Nigel James Post author

    I have just moved this blog and updated the code to the new code editor. I trust this is still a useful document for you.

    Cheers,
    Nigel

    (0) 

Leave a Reply