Skip to Content
Technical Articles

CPI OData Calculator: REST Source (Groovy as Response Mapping)

Hello,

As we’ve discussed on the previous Blog, SAP® “speaks” OData (Mobile, FIORI, etc.), so my goal here again is to demonstrate how the “OData conversion” works on CPI, creating a simple Calculator (multiply operation) iFlow:

Please go through the previous blog here.

We have a few options as the data source on the OData iFlow creation, this time i’m gonna use the REST one (in the same OData service created on the previous blog).

For this tutorial, we’re using the https://api.mathjs.org/ API. It’s really simple, if you want to multiply 2 numbers, just do it like these: https://api.mathjs.org/v4/?expr=2*2 and will return the result on plain text on your browser.

So let’s get to it, open the scenario created on the previous blog and let’s edit the OData Model Editor:

To create the selected lines. We’re basically creating a new Entity called MultiplyREST (with 3 fields as well, REST1, REST2 and RESTResult).

In the end it should look like these:

<edmx:Edmx
    xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"
    xmlns:sap="http://www.sap.com/Protocols/SAPData" Version="1.0">
    <edmx:DataServices
        xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="2.0">
        <Schema
            xmlns="http://schemas.microsoft.com/ado/2008/09/edm" Namespace="S1">
            <EntityContainer Name="EC1" m:IsDefaultEntityContainer="true">
                <EntitySet Name="MultiplySOAPSet" EntityType="S1.MultiplySOAP"></EntitySet>
                <EntitySet Name="MultiplyRESTSet" EntityType="S1.MultiplyREST"></EntitySet>
            </EntityContainer>
            <EntityType Name="MultiplySOAP">
                <Documentation/>
                <Key>
                    <PropertyRef Name="SOAP1"></PropertyRef>
                </Key>
                <Property Name="SOAP1" Type="Edm.String" Nullable="false"></Property>
                <Property Name="SOAP2" Type="Edm.String" Nullable="true"></Property>
                <Property Name="SOAPResult" Type="Edm.String" Nullable="true"></Property>
            </EntityType>
            <EntityType Name="MultiplyREST">
                <Documentation/>
                <Key>
                    <PropertyRef Name="REST1"></PropertyRef>
                </Key>
                <Property Name="REST1" Type="Edm.String" Nullable="false"></Property>
                <Property Name="REST2" Type="Edm.String" Nullable="true"></Property>
                <Property Name="RESTResult" Type="Edm.String" Nullable="true"></Property>
            </EntityType>
        </Schema>
    </edmx:DataServices>
</edmx:Edmx>

Switching back, change the new Entity to a REST data source:

And create a Bind for the Create operation as well. It creates a iFlow for your, but we’re doing some minor changes to it, in the end it should look like these:

First, let’s delete the first script and place a Content Modifier:

In there we’re creating properties for the 2 input fields (using XPath), as we need them to execute the Multiply operation in the API URL.

In the HTTP Request-Reply operation, the URL will look like these:

https://api.mathjs.org/v4/?expr=${property.REST1}*${property.REST2}

Don’t forget to change the call to GET and the Auth to None.

On the Response Mapping Groovy Script, insert the code below:

import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
def Message processData(Message message) {
    //Body 
       def body     = message.getBody(java.io.Reader);
       def string   = message.getBody(java.lang.String);
    //Properties 
       map = message.getProperties();
       REST1 = map.get("REST1");
       REST2 = map.get("REST2");
       message.setBody( 
    "<MultiplyRESTSet>" + 
    "<MultiplyREST>" + 
    "<REST1>" + REST1 + "</REST1>" + 
    "<REST2>" + REST2 + "</REST2>" + 
    "<RESTResult>" + string + "</RESTResult>" + 
    "</MultiplyREST>" + 
    "</MultiplyRESTSet>");
       return message;
}

We’re basically getting the response as String, also the 2 input fields from the request(Property), and creating a Entity Set response with the data on the desired format.

Save and deploy your iFlow. Now, let’s test it:

First, don’t forget to get the Token(X-CSRF-Token) from a simple Get to the OData root URL service. After that, let’s make a POST request using the payload below:

<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices">
 <content type="application/xml">
  <m:properties>
   <d:REST1>10</d:REST1>
   <d:REST2>275</d:REST2>
  </m:properties>
 </content>
</entry>

And Voilà:

Play around as you want!

Enjoy!

Regards.

Be the first to leave a comment
You must be Logged on to comment or reply to a post.