This is the part 1 of : How to consume SOAP service using Oracle Mobile Cloud Service(MCS) into a MAF application.
This Part 1 talks about :
Select "New SOAP Connector" :
I am going to use a public SOAP service which publishes current weather based on cities.
http://www.webservicex.net/globalweather.asmx?WSDL
Once you press Create, it will take you to the configuration screen. Click on "Port" and you will see 2 exposed services from that WSDL
Click "Save and Close" .
And your connector is ready!!
Now we will create the custom API on top of it.
Click on "Development" on top right part of the page. And then click on "APIs"
Click on "New API" and enter details
On the configuration screen of this API, go to "Security" and switch on Anonymous access.
Then go to "Endpoints" and click on "New Resource". Add 2 new resources like below.
Now click on "Methods" for "getCities" and "Add Method" : GET
Add one parameter "country" and make it required.
Do the same actions for "getWeather", but for this add 2 parameters.
Save your work.
Now go to : "Implementations" and download the "Javascript Scaffold"
Unzip the downloaded file and open weatherapi.js in a text editor, add the following code in there :
I had to do some string manipulation on the REST response, as the SOAP service returns an XML as CDATA, which MCS doesn't convert to JSON. The manipulation is for the MAF application, so that the mobile app can use a SAX parser on the XML content of the JSON response.
==========================================
module.exports = function(service) {
/**
* The file samples.txt in the archive that this file was packaged with contains some example code.
*/
service.get('/mobile/custom/WeatherAPI/getweather', function(req,res) {
var optionsList={uri: '/mobile/connector/GlobalWeather/GetWeather'};
optionsList.headers={'content-type' : 'application/json;charset=UTF-8'};
// the details of the request are provided in a javascript object
var outgoingMessage = {Header: null, Body: {"GetWeather": {
"CityName": req.query.city,
"CountryName": req.query.country
}}};
optionsList.body=JSON.stringify(outgoingMessage);
var handler=function(error,response,body) {
var responseMessage=body;
if(error){
responseMessage=error.message;
}
var newLine = /(\\n)/g;
var escapeChar = /(\\")/g;
responseMessage = responseMessage.replace(newLine, '');
responseMessage = responseMessage.replace(escapeChar, '\'');
res.status(response.statusCode).send(responseMessage);
res.end();
};
var r=req.oracleMobile.rest.post(optionsList,handler);
});
service.get('/mobile/custom/WeatherAPI/getCities', function(req,res) {
var optionsList={uri: '/mobile/connector/GlobalWeather/GetCitiesByCountry'};
optionsList.headers={'content-type' : 'application/json;charset=UTF-8'};
// the details of the request are provided in a javascript object
var outgoingMessage = {Header: null, Body: {"GetCitiesByCountry": {
"CountryName": req.query.country
}}};
optionsList.body=JSON.stringify(outgoingMessage);
var handler=function(error,response,body) {
var responseMessage=body;
if(error){
responseMessage=error.message;
}
var newLine = /(\\n)/g;
responseMessage = responseMessage.replace(newLine, '');
res.status(response.statusCode).send(responseMessage);
res.end();
};
var r=req.oracleMobile.rest.post(optionsList,handler);
});
};
=======================================
Now zip the directory again. Navigate to the API on MCS and drop the zip file.
Your custom API is ready to use.
We'll now create a MCS backend to expose this APIs.
Goto Development > Mobile Backends and click on "New Mobile Backend".
After you create it, go to APIs by clicking on the left hand side link. Select "Select APIs". Search for your API there and click +
Now go back to the settings page. This page contains a few important info which the MAF application requires, in order to successfully talk to MCS.
Your MCS is ready to be consumed by any Mobile App now.
Part 2 : How to build MAF application for this MCS backend.
This Part 1 talks about :
- Create SOAP Connector using MCS.
- Create custom API and do some minor modifications on retrieved data.
- Create Mobile Backend and associate the custom API.
Select "New SOAP Connector" :
I am going to use a public SOAP service which publishes current weather based on cities.
http://www.webservicex.net/globalweather.asmx?WSDL
Once you press Create, it will take you to the configuration screen. Click on "Port" and you will see 2 exposed services from that WSDL
Click "Save and Close" .
And your connector is ready!!
Now we will create the custom API on top of it.
Click on "Development" on top right part of the page. And then click on "APIs"
Click on "New API" and enter details
On the configuration screen of this API, go to "Security" and switch on Anonymous access.
Then go to "Endpoints" and click on "New Resource". Add 2 new resources like below.
Now click on "Methods" for "getCities" and "Add Method" : GET
Add one parameter "country" and make it required.
Do the same actions for "getWeather", but for this add 2 parameters.
Save your work.
Now go to : "Implementations" and download the "Javascript Scaffold"
Unzip the downloaded file and open weatherapi.js in a text editor, add the following code in there :
I had to do some string manipulation on the REST response, as the SOAP service returns an XML as CDATA, which MCS doesn't convert to JSON. The manipulation is for the MAF application, so that the mobile app can use a SAX parser on the XML content of the JSON response.
==========================================
module.exports = function(service) {
/**
* The file samples.txt in the archive that this file was packaged with contains some example code.
*/
service.get('/mobile/custom/WeatherAPI/getweather', function(req,res) {
var optionsList={uri: '/mobile/connector/GlobalWeather/GetWeather'};
optionsList.headers={'content-type' : 'application/json;charset=UTF-8'};
// the details of the request are provided in a javascript object
var outgoingMessage = {Header: null, Body: {"GetWeather": {
"CityName": req.query.city,
"CountryName": req.query.country
}}};
optionsList.body=JSON.stringify(outgoingMessage);
var handler=function(error,response,body) {
var responseMessage=body;
if(error){
responseMessage=error.message;
}
var newLine = /(\\n)/g;
var escapeChar = /(\\")/g;
responseMessage = responseMessage.replace(newLine, '');
responseMessage = responseMessage.replace(escapeChar, '\'');
res.status(response.statusCode).send(responseMessage);
res.end();
};
var r=req.oracleMobile.rest.post(optionsList,handler);
});
service.get('/mobile/custom/WeatherAPI/getCities', function(req,res) {
var optionsList={uri: '/mobile/connector/GlobalWeather/GetCitiesByCountry'};
optionsList.headers={'content-type' : 'application/json;charset=UTF-8'};
// the details of the request are provided in a javascript object
var outgoingMessage = {Header: null, Body: {"GetCitiesByCountry": {
"CountryName": req.query.country
}}};
optionsList.body=JSON.stringify(outgoingMessage);
var handler=function(error,response,body) {
var responseMessage=body;
if(error){
responseMessage=error.message;
}
var newLine = /(\\n)/g;
responseMessage = responseMessage.replace(newLine, '');
res.status(response.statusCode).send(responseMessage);
res.end();
};
var r=req.oracleMobile.rest.post(optionsList,handler);
});
};
=======================================
Now zip the directory again. Navigate to the API on MCS and drop the zip file.
Your custom API is ready to use.
We'll now create a MCS backend to expose this APIs.
Goto Development > Mobile Backends and click on "New Mobile Backend".
After you create it, go to APIs by clicking on the left hand side link. Select "Select APIs". Search for your API there and click +
Now go back to the settings page. This page contains a few important info which the MAF application requires, in order to successfully talk to MCS.
- Base URL
- Mobile Backend ID
- Anonymous Key
- Application Key
Your MCS is ready to be consumed by any Mobile App now.
Part 2 : How to build MAF application for this MCS backend.
Comments
Post a Comment