Appcelerator Blog

The Leading Resource for All Things Mobile

REST-to-REST API Optimization using the Appcelerator Node.ACS MBaaS

17 Flares 17 Flares ×

If my back end data source (e.g. Salesforce.com, SAP. Oracle, …) exposes its data with modern REST APIs, can I still leverage the Appcelerator MBaaS/Node.ACS?

There are several good reasons to leverage the Appcelerator MBaaS in your mobile architecture. What if the existing back end REST API exposes a lot more data than you need for your app? In this case, creating a mobile optimized API in the Appcelerator MBaaS can help improve the application performance and ease development, by exposing mobile-optimized APIs. This post will provide an example of this scenario using a publicly available API from Rotten Tomatoes, the movie and TV database.

Original API

You can access Rotten Tomatoes API at http://developer.rottentomatoes.com. You’ll need to register and get an API key to follow along.

An example HTTP call that returns a list of movies that have the search term “Zombie” in the title or related meta data is shown below:

http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=zombie&apikey=<API Key>

A truncated version of the JSON reply is shown below:

{
"total": 244,
"movies": [
  {
    "id": "770802740",
    "title": "Zombieland",
    "year": 2009,
    "mpaa_rating": "R",
    "runtime": 88,
    "critics_consensus": "",
    "release_dates": {
      "theater": "2009-10-02",
      "dvd": "2010-02-02"
    },
    "ratings": {
      "critics_rating": "Certified Fresh",
      "critics_score": 90,
      "audience_rating": "Upright",
      "audience_score": 86
    },
    "synopsis": "",
    "posters": {
      "thumbnail": "http://content6.flixster.com/movie/11/16/79/11167980_tmb.jpg",
      "profile": "http://content6.flixster.com/movie/11/16/79/11167980_tmb.jpg",
      "detailed": "http://content6.flixster.com/movie/11/16/79/11167980_tmb.jpg",
      "original": "http://content6.flixster.com/movie/11/16/79/11167980_tmb.jpg"
    },
    "abridged_cast": [
      {
        "name": "Woody Harrelson",
        "id": "162654250",
        "characters": [
          "Tallahassee"
        ]
      },
      {
        "name": "Jesse Eisenberg",
        "id": "162654393",
        "characters": [
          "Columbus"
        ]
      },
      {
        "name": "Emma Stone",
        "id": "770683296",
        "characters": [
          "Witchita"
        ]
      },
      {
        "name": "Abigail Breslin",
        "id": "162653854",
        "characters": [
          "Little Rock"
        ]
      },
      {
        "name": "Amber Heard",
        "id": "770673718",
        "characters": [
          "406"
        ]
      }
    ],
    "alternate_ids": {
      "imdb": "0847224"
    },
    "links": {
      "self": "http://api.rottentomatoes.com/api/public/v1.0/movies/770790880.json",
      "alternate": "http://www.rottentomatoes.com/m/zombie-girl-the-movie/",
      "cast": "http://api.rottentomatoes.com/api/public/v1.0/movies/770790880/cast.json",
      "reviews": "http://api.rottentomatoes.com/api/public/v1.0/movies/770790880/reviews.json",
      "similar": "http://api.rottentomatoes.com/api/public/v1.0/movies/770790880/similar.json"
    }
  }
],
"links": {
  "self": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=zombi&page_limit=30&page=1",
  "next": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q=zombi&page_limit=30&page=2"
},
"link_template": "http://api.rottentomatoes.com/api/public/v1.0/movies.json?q={search-term}&page_limit={results-per-page}&page={page-number}"
 }

The reply object above has the following structure as viewed in a JSON viewer:

For these screenshots I’m using http://jsonviewer.stack.hu/, but you could also use http://jsoneditoronline.org, or the JSONView browser extension.

REST to REST API Optimization

This is a large amount of data. But, what if I only want to display the movie title and year and then get more info if and when the user selects a specific movie, then we are downloading too much data in order to extract just two fields (title and movie).

Let’s use the Appcelerator MBaaS to build a middle tier API that optimizes the payload to return a list of movies with only the title and year.

The following Node.ACS method exposes an API called getMovies():

var https = require('http');

function getMovies(req, res) {
    var myreq = https.get('http://api.rottentomatoes.com/api/public/v1.0/movies.json?q='+req.query.searchText+'&apikey=<API KEY>', function(r) {
    var str="";
    var results=[];
    var response={};
    r.setEncoding('utf8');
     r.on('data', function (chunk) {
        str += chunk;
      });
      r.on('end', function () {
            var d = JSON.parse(str);
            var dlength = d.movies.length;
            for(var i=0; i

The API call now looks like this:

https://5f19432521855af5989ba3b91db8c2c5ae2d727f.cloudapp.appcelerator.com/getMovies?searchText=zombie

and the response looks like this:

{
  "movies": 
    [
        {
            "title": "Zombieland",
            "year": 2009
        },
        {
            "title": "Scouts vs. Zombies",
            "year": 2015
        },
        {
            "title": "Zombie Strippers",
            "year": 2008
        },
        {
            "title": "Zombie Beach Party (Enter... Zombie King!)",
            "year": 2003
        },
        {
            "title": "Zombie ja Kummitusjuna (Zombie and the Ghost Train)",
            "year": 1991
        },
        {
            "title": "Zombie Rampage,(Todd Sheets' Zombie Rampage)",
            "year": 1991
        },
        {
            "title": "Zombie Holocaust",
            "year": 1979
        },
        {
            "title": "Cockneys vs Zombies",
            "year": 2013
        }
    ]
}

The reply object above has the following structure as viewed in a JSON viewer:

As you can see the data set is greatly reduced. This data will be transmitted across the wireless network much faster, will require less memory on the device and will be parsed more quickly by the application, all resulting in an improvement in performance for the mobile application and a better user experience for the user.

In addition to simply reducing the data set, the developer can also do data manipulations, data formatting, create new fields, rename fields, etc… all for purpose of creating the exact data set and format for the application.

Also, note that in this example, the client side code to parse the response doesn’t change at all since the movies element is still available as movies[i].title and movies[i].year.

Following is a sample client side code snipet to parse the response that works for the original Rotten Tomatoes API as well as the Appcelerator MBaaS mobile optimized API, getMovies:

var reply = JSON.parse(e);

// some logic goes here

if(reply.movies.length>0){

_.each(reply.movies, function(item) {
        rows.push(Alloy.createController('movieRow', {
            name: item.title,
            year: item.year,
        }).getView());
});

// some logic goes here

So, we have seen that even if your back end data services are exposed as modern RESTful web services, they may not always be most suited for mobile applications where mobile optimized data sets are required. The Appcelerator MBaaS can easily expose APIs that are mobile optimized for each specific application need.

17 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 17 Email -- 17 Flares ×

1 Comment

  1. Joseoh Sachs

    This is a great tip…
    Similarly ACS added recently two params for Query & Search which are sel (select) & unsel (unselect) they does something similar to what you are achieving.

    Great optimization technique. Thanks for the post Leor, love optimization tips.

Comments are closed.

Sign up for updates!

Become a mobile leader. Take the first step to scale mobile innovation throughout your enterprise.
Get in touch
computer and tablet showing Appcelerator software
Start free, grow from there.
17 Flares Twitter 0 Facebook 0 Google+ 0 LinkedIn 17 Email -- 17 Flares ×