Technical Articles
CPI SOAP to REST Currency Converter with Groovy
Hello,
In this tutorial we’re gonna create a simple SOAP Currency Converter in CPI:
Using a REST API data source for the conversions, read more about it here: https://exchangeratesapi.io/
Basically in this FREE API, you need to pass the base currency on the URL query parameter, like this: https://api.ratesapi.io/api/latest?base=USD. The result:
{
"base": "USD",
"rates": {
"GBP": 0.8098223235,
"HKD": 7.7519817768,
"IDR": 14754.9977220957,
"ILS": 3.5118906606,
"DKK": 6.7934396355,
"INR": 75.5599088838,
"CHF": 0.9658314351,
"MXN": 22.150523918,
"CZK": 24.667881549,
"SGD": 1.4177676538,
"THB": 31.8797266515,
"HRK": 6.9102505695,
"EUR": 0.9111617312,
"MYR": 4.3634624146,
"NOK": 9.9264692483,
"CNY": 7.131571754,
"BGN": 1.7820501139,
"PHP": 50.4400911162,
"PLN": 4.0552164009,
"ZAR": 17.3712984055,
"CAD": 1.3835079727,
"ISK": 140.410022779,
"BRL": 5.3862414579,
"RON": 4.413667426,
"NZD": 1.6143052392,
"TRY": 6.7438724374,
"JPY": 107.444191344,
"RUB": 70.8422779043,
"KRW": 1232.1002277904,
"USD": 1.0,
"AUD": 1.5069703872,
"HUF": 318.5421412301,
"SEK": 9.6184965831
},
"date": "2020-05-26"
}
So basically we need 2 input fields (Currency from and Currency To) and 1 response field (Rate value). For that, i’ve created a simple WSDL file using Eclipse and used on the iFlow, you can get it here:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://www.example.org/CPI_DEMO/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="CPI_DEMO" targetNamespace="http://www.example.org/CPI_DEMO/">
<wsdl:types>
<xsd:schema targetNamespace="http://www.example.org/CPI_DEMO/">
<xsd:element name="GetCurrency">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FROM" type="xsd:string" />
<xsd:element name="TO" type="xsd:string"></xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
<xsd:element name="GetCurrencyResponse">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="VALUE" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
</wsdl:types>
<wsdl:message name="GetCurrencyRequest">
<wsdl:part element="tns:GetCurrency" name="parameters"/>
</wsdl:message>
<wsdl:message name="GetCurrencyResponse">
<wsdl:part element="tns:GetCurrencyResponse" name="parameters"/>
</wsdl:message>
<wsdl:portType name="CPI_DEMO">
<wsdl:operation name="GetCurrency">
<wsdl:input message="tns:GetCurrencyRequest"/>
<wsdl:output message="tns:GetCurrencyResponse"/>
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="CPI_DEMOSOAP" type="tns:CPI_DEMO">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="GetCurrency">
<soap:operation soapAction="http://www.example.org/CPI_DEMO/GetCurrency"/>
<wsdl:input>
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output>
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="CPI_DEMO">
<wsdl:port binding="tns:CPI_DEMOSOAP" name="CPI_DEMOSOAP">
<soap:address location="http://www.example.org/"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
So let’s do it! Create an iFlow, in the end it should look like these:
So create a SOAP sender connection, using the WSDL above:
Next, a Content Modifier to capture both input values:
Next, a Request Reply HTTP to the Receiver (API), passing the Query parameter like this:
Next, a Groovy Script to “map” the return value to the desired SOAP response (using JSON Slurper):
import com.sap.gateway.ip.core.customdev.util.Message;
import java.util.HashMap;
import groovy.json.*;
def Message processData(Message message) {
map = message.getProperties();
currto = map.get("CURRFROM");
def body = message.getBody(String.class);
def jsonSlurper = new JsonSlurper();
def list = jsonSlurper.parseText(body);
String val = list.rates[currto];
message.setBody(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<ns0:GetCurrencyResponse xmlns:ns0=\"http://www.example.org/CPI_DEMO/\"><VALUE>" +
val +
"</VALUE></ns0:GetCurrencyResponse> "
);
return message;
}
And that’s it, save and deploy!
To test it, i’m using SOAPUI.
Use the WSDL to generate the Request and place your iFlow URL to make the call, and you’ll get the desired currency, exemple below from BRL to EUR:
Enjoy!
Regards.
Awesome Jose...
This is really simple... I liked it … but if you can add more example within this scenario for Currency Convertor , many bloggers will think out of the box
Hello Yogananda Muthaiah , how are you? Thank's for the feedback.
That's the idea, make it a Blog series adding more complex features to it, will consider your suggestions.
Best regards.
Why a Request Reply HTTP adapter and not a SOAP adapter ?
Best regards,
Rolf
Hello Rolf Hoven , how are you?
Basically the goal here was to demonstrate a very simple "conversion" iFlow, that from a REST API Endpoint you can create a SOAP sender scenario (iFlow) + a simple Groovy Script. Already working on the next phases of the tutorials, and will consider your suggestion as well.
Thanks a lot.
Hi and thanks for a great blog !
I just started to learn CPI. We have an instant together with Cloud Connector and tons of good old SAP ECC systems... 🙂
I manage to call : http://currencyconverter.kowabunga.net/converter.asmx using my HTTP adapter:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<GetConversionRate xmlns="http://tempuri.org/">
<CurrencyFrom>USD</CurrencyFrom>
<CurrencyTo>NOK</CurrencyTo>
<RateDate>2020-06-02</RateDate>
</GetConversionRate>
</soap12:Body>
</soap12:Envelope>
....but not using the SOAP adapter, even if I used a wsdl-file. ( I removed all SOAP-tags in the Header and also in the Body )
Can you please explain the difference of the 2 adapters ?
Hello Rolf Hoven, what error are you getting using the SOAP Adapter?
Regarding the diferences between adapter, check this link.
Thanks.
Great Zé pr, keep on fire!
Let's go! Tks.
Hello Jose,
This is an amazing read. Hope to see many more articles on CPI in the near future.
There is a query i would like to address related to the usage of wsdl which i am finding quite confusing. The wsdl file used here comprises of a input output request response operation . Does this mean the soap adapter will take the input and return the output response from the web service? or does it only process the input message per the schema defined on the wsdl which is what seems to be happening here since the output response generates from the groovy script in this case and not from the wsdl.