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. Message Platform Server : Is a NodeJS application, deployed on Oracle Application Container cloud, acts as a channel between Facebook Messenger and the chatbot engine. It simply converts the incoming messages from Facebook and sends it to chatbot readable format. Also, when chatbot replies, it converts to Facebook readable formats and passes it to messenger. Chatbot Engine : Is a NodeJS application, which communicate with some REST APIs based on a conversation flow document and moves the flow of the conversation from one state to another. Flow JSON : Where we document, every state of a conversation and which APIs to call to generate a response. For example, at the beginning of the con...

Create Micro CRUD services for Oracle Database Cloud using NodeJS

I will try to explain, how you can use NodeJS to create mirco services for the tables in your Oracle Database Cloud or on-premise Database. Complete Github project : https://github.com/sohamda/LeasifyAPIs You need to do "npm install" to download the node_modules. Step by Step guide : 1. NodeJS : either 32 or 64 bit. If you already have NodeJS installed, please check whether it is 64 or 32. Use below command to figure that out : C:\>node > require('os').arch() If you get : 'ia32' , then it is 32 bit installation. 2. Install oracle-db node module .  This was a lengthy and time consuming installation for me, because for Windows, it has a lot of pre-requisites. If you are a Mac user, you are lucky. :) I followed : https://community.oracle.com/docs/DOC-931127 There is also a detailed one in github : https://github.com/oracle/node-oracledb/blob/master/INSTALL.md 3. Config your DB Cloud Create a user and couple of tables on which we...

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 "strateg...