Using Google Apps Script as a wrapper for multiple APIs

Lately I’ve been posting about the Google Apps Script Content Service and how to use it as a ‘serverless’ Rest API service. In addition, I covered how to use the ScriptDB as a secure repository for oAuth credentials, and using the Content Service as a proxy for other APIs that need authentication.

Another thing I’ve been playing around with in recent posts is mashing up multiple APIs, for example doing some geocoding then passing the result of that off to another API.

So where are am I going with all these threads? Content Service, acting as a pseudo server, allows us to either combine, or preprocess API calls. That means that we can create a “completely new REST API” by combining multiple APIs, doing the necessary preprocessing and translations in Google Apps Script.

An Example using scraperWiki
In a couple of posts about scraperWiki, I showed how to get data out of public scraperwiki data tables. The scraper wiki REST API needs you to have

  • The shortname of the scraperwiki query is
  • The name of the data table
  • A knowledge of SQL

In getting data out of scraperwiki, I showed a way of avoiding that all by mashing up multiple api calls. Now we are going to expose that mash up through a completely new API call, this time served up by Google Apps Script Content service. You can see how this can then be combined with the oAuth proxying capability to simplify the whole business of chaining together multiple APIs

API call parameters
In summary, given a scraperWiki shortname, we will make multiple calls to the scraperwiki API to find out the default table name, and retrieve data in jSon format. You can also specify a maximum number of rows to return, as well as column names to filter on.

Parameter req/opt Description
shortname required the scraperWiki shortname
limit optional the maximum number of results to return
columna=x,columnb=y,… optional filtering by column names and values

For the example, we’ll use an interesting data table I found on scraperwiki – interpol_wanted_persons (apologies if it disappears – I just selected it at random). We’re going to limit this to 10 rows, and only return people with a particular nationality.

apiUrl?shortname=interpol_wanted_persions&limit=10&Nationality=Malaysia

apiURL is the Google Apps script URL – the very easy to remember

https://script.google.com/a/macros/mcpher.com/s/AKfycbwY-jmsNFu4tori8pxu_J4qmSu467yFLzlzKgG-Vfv7jOOLa5A/exec

Code
Each pseudo API you write is going to need a separate scritpt with a doGet(e) function. . This one happens to use functions I’ve used elsewhere in  the mcpher library, so you would need that included in your project too. Here’s the code for the scraperWikiQuery, below, in this gist, or on Google Apps Script

Loading ….

As a rest library entry
Since we’ve conjured up a brand new API out of existing ones, we can of course use it just like a real API. That means we can add it to the rest library, which in turn means that we can do mashups for both Google Apps Script and VBA – but only have to actually implement the Google Apps Script version. In other words, the mashup work will all be done by the google apps script version. Thinking of the urbarama mashup I showed the other day, It would be possible to simply expose the GAS mashup results and access it from VBA instead of implementing the mashup twice. I’m still pondering the implications of this and will post some more at a future date.

Directly from code
You can of course access this directly from code also – here’s the example above – pretty cool

function t() {
  var e = {parameter: 
            {shortname : “interpol_wanted_persons”,
             Nationality : “Malaysia”,
             limit: 10}
          };
  doGet(e);
}

For lots more stuff like this, see the Excel liberation site.
https://raw.github.com/moski/gist-Blogger/master/public/gistLoader.js

Author: bm082975

4 thoughts on “Using Google Apps Script as a wrapper for multiple APIs

  1. Hi Bruce,This is pretty interesting stuff! I have been looking around on both your sites, and am looking for some way to post to scriptDB using your rest library. Is it possible to make changes to the scriptDB using your rest library? I am looking to update a google spreadsheet from a chrome extension build with Sencha Extjs.CheersBastian

  2. HelloYes you could write a catcher in google apps script, as per the example above, except you write to instead of reading from the scriptDB. You mention updating a spreadsheet though, not the scriptdb. You can do this too using the google spreadsheet api.

  3. Am very much enjoying following the progression of your exploration of GAS and wonder if the intervening 6 weeks (or so) has brought any specific thoughts re:>>It would be possible to simply expose the GAS mashup results and access it from VBA instead of implementing the mashup twice. I'm still pondering the implications of this and will post some more at a future date.<<

Leave a Reply

Your email address will not be published. Required fields are marked *