Skip to main content

WeatherApp with MAF using MCS : Part 1

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 :
  1. Create SOAP Connector using MCS.
  2. Create custom API and do some minor modifications on retrieved data.
  3. Create Mobile Backend and associate the custom API.
 Login to MCS and go to Connectors :



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.
  1. Base URL
  2. Mobile Backend ID
  3. Anonymous Key
  4. 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

Popular posts from this blog

Chatbots and Oracle Cloud Services

Thanks to Oracle A-Team, I had a chance to work with Chatbots.
3 pure NodeJS applications, on couple of Oracle Cloud platforms and Facebook messenger, and my chatbot was running.

Let me explain, the architecture a bit. To start with, following is the simple representation of how it works.

Exception Handling in ADF

This blog will give you an overview on how you can successfully deal with unhandled Runtime exceptions in an ADF application.

This will give you an idea of:
How to catch the unhandled exceptions.Write a separate log file with stacktrace and thread dumps.Redirect the user to an static error page
#1. Catch unhandled exceptions : 

Create a class "MyExceptionHandler" which extends : oracle.adf.view.rich.context.ExceptionHandler. Override handleException() method.

public void handleException(FacesContext facesContext, Throwable throwable, PhaseId phaseId) throws Throwable {
        // this method is going to create a separate file with stacktrace and thread dumps
        writeException(throwable);
        // redirect to error page
        redirectToErrorPage(facesContext);
    }

 Create a folder "services" inside : ViewController\src\META-INF and then create a file named "oracle.adf.view.rich.context.ExceptionHandler".
In the file, add the absolute name of your custom e…

Secure your Node/Express REST APIs using Passport JS

Recently I have been involved in various discussions on how to make the REST APIs as secure as SOAP.
First of all, let me start with a very basic statement, about security, it doesn't depend on your Webservice type, be it REST or SOAP, your design decisions depicts whether they can be made secure or not.
In this example blog, I will use "Micro CRUD services for Oracle Database Cloud" APIs and implement (read attach) Passport's "local" authentication strategy, to make them secure.

Code in Github : LeasifyAPIs with Passport

What is Passport JS? (from Documentation)

Passport is authentication middleware for Node. It is designed to serve a singular purpose: authenticate requests. When writing modules, encapsulation is a virtue, so Passport delegates all other functionality to the application. This separation of concerns keeps code clean and maintainable, and makes Passport extremely easy to integrate into an application.

What are "strategies" in P…