Introduction

This Wiki Article explores some of the capabilities of Azure Mobile Services API that might be of interest in some scenarios.

Note: this article assumes some familiarity with Azure Mobile Services.  For a great introduction see the Microsoft documentation.

Node.js

It is important to note that the API is a Node.js server up and running in the cloud; and it already supports basic http routing. This means we can pull in node.js modules and create more complex solutions than just basic CRUD operations against sql tables.  It also means the challenges of hosting a Node.js solution has been greatly simplified. 

Cool. 

Fabricated Scenario

Let's say our requirement is to expose some weather information to our mobile and win8 customers. Fortunately we have the data sourced from the xml service provided by the National Weather Service.  Let's also say that we need to perform some enrichment of the weather service response.  In the example below, this is shown as a comment so please use your imagination here :).

Basic API

First we will build a simple API called weatherwatch.

To make it easy to test, I made the get permissions.

The initial script simply calls an external webservice to retrieve some data.  Notice how we reference an additional library to make the web request.  Other libraries that are made available to our mobile API are listed here.

exports.get = function(request, response) {
     
    var httpRequest = require('request');
 
    uri += '?citiesLevel=12&format=24+hourly&numDays=7';
         
    httpRequest(uri, function(err, res, body) {           
           if (err || res.statusCode !== 200) {
                response.send(400, { 'message' : 'Failed to connect to weather.'});
           }
           else {
                // this is where the body content is processed and enriched
                response.send(statusCodes.OK, { 'message' : body });
           }
    });  
};

The basic API can then be retrieved using a browser. 


Extending the Basic API

So, it is not a big leap to move the call into a function:


exports.get = function(request, response) {
 
    response.send(statusCodes.OK, { 'message' : updateWeather() });
             
};
 
var updateWeather = function() {
    var httpRequest = require('request');
        uri += '?citiesLevel=12&format=24+hourly&numDays=7';
         
        httpRequest(uri, function(err, res, body) {           
            if (err || res.statusCode !== 200) {
                return 'Unable to connect to weather.';
            } else {
                // this is where the body content is processed and enriched        
                return body;
            }
        });
}

Now, what we want to achieve is to limit the number of times the body is retrieved from the national weather service and also limit the number of times our processing is performed.  I chose to introduce a global object to hold the result:

var weather = {
    lastUpdateDate: null,
    updatedCount: 0,
    data: {},       
};

This object is created when the service starts up so in our terms this is every time we update the script and hit save.

Now that the object is created, the result of a service call can be altered as follows:

var updateWeather = function() {
    var httpRequest = require('request');
        uri += '?citiesLevel=12&format=24+hourly&numDays=7';
         
        httpRequest(uri, function(err, res, body) {           
            if (err || res.statusCode !== 200) {
                weather.data = 'Unable to connect to weather.';
            } else {         
                weather.lastUpdateDate = new Date();
                weather.updatedCount += 1;
                weather.data = body;
            }
        });
}

So, this means our response can be returned as: 

response.send(statusCodes.OK, {
          'LastUpdated' : weather.lastUpdateDate,
          'weather' : weather.data
        });

I chose to initially populate our model when the service starts up and to refresh the model every 24 hours.  Again, this might make sense in certain scenarios.

The following is the full listing of the final custom API:

01.exports.get = function(request, response) {
02. 
03.    console.log('weatherwatch count=', weather.updatedCount);
04. 
05.    response.send(statusCodes.OK, {
06.          'LastUpdated' : weather.lastUpdateDate,
07.          'weather' : weather.data
08.        });
09.};
10. 
11.setTimeout(function() { updateWeather() }, 1000);
12.setInterval(function() { updateWeather() }, 86400000);
13. 
14.var weather = {
15.    lastUpdateDate: null,
16.    updatedCount: 0,
17.    data: {},       
18.};
19. 
20.var updateWeather = function() {
21.    var httpRequest = require('request');
23.        uri += '?citiesLevel=12&format=24+hourly&numDays=7';
24.         
25.        httpRequest(uri, function(err, res, body) {           
26.            if (err || res.statusCode !== 200) {
27.                weather.data = 'Unable to connect to weather.';
28.            } else {         
29.                weather.lastUpdateDate = new Date();
30.                weather.updatedCount += 1;
31.                weather.data = body;
32.            }
33.        });
34.}

See Also

Another important place to find a huge amount of Windows Phone related articles is the TechNet Wiki itself. The best entry point is Windows Phone Resources on the TechNet Wiki.