PROFOUND LOGIC - Web API (Weather App) - Part 3


This blog is about making a web API called to openweathermap.org. 

Yes, many have already done this i.e. just from Node.JS console but I wanted to try the same from ProfoundUI screen.
I tried many open API's but felt weather app would be a good start. I'm planning to write one with ipconfig.io/json but that's for later. 

Create a account with openweather and use the key to access the free current API from program. While Profoundjs has different approach to handle web API but I could not get a grasp of it. I use the traditional Node.JS method to request data .


REQUEST module is most popular for making HTTP request. There is HTTP module as well but REQUEST module make it lot easier.


The below code submits a HTTP GET request to URL i.e to openweather API and it will return the data i.e. in this can we can opt for HTML or JSON data. 

In the below scenario, I have set the JSON:true.

The same request can be used for POST, PUT or DELETE.


request( URL , function( err,response, body) {

   console.log(body);
        });  

var screenfields = { };       

This is to define a empty JSON object. I have used this field to map return data from API to  screen fields. I'm having to click 'fetch' button twice to get the values to the screen. 






Code in IFS folder



var request = require("request");                                       
                                                              
function weather() {                                                    
  pjs.defineDisplay("weather.json");                                    
  var screenfields = { };                                                
                                                                        
 while (!exit) {                                                        
     pjs.setFields(screenfields);                                       
     weather.wthapp.execute();                                          
                                                                        
     var api    = 'http://api.openweathermap.org/data/2.5/weather?q=';  
     var apikey = '&units=metric&appid=1a85a1099a13518956b8ff528ff*****';
     var url = api + input.trim() + apikey;                             
     var len = input.trim().length;                                     

console.log(get);                                     
    if (get && len > 0) {                             
      console.log(url); 
                              
      var options =  {                                
          url: url,                                   
          json: true };
                                
     function callback (error, response, body) {      
      if (!error && response.statusCode === 200) {    
         screenfields = body.main;                    
                                               }      
                                                 }    
         request(options,callback);                   
                           }  //end-if                
                 }  // end-while                      
             }  //end-function                        
exports.run = weather; 

Popular posts from this blog

PF2XLS - Python

Stock Analysis - Example with Ashok Leyland

Python on IBM i