DataHandler REST API


Since we now have a common handler layer as described in Database abstraction with google apps script, it's just a small step to expose that as an API through a Google Apps Script API. This means that anything that can do POST or GET can access any database for which we have created a driver. An example use might be writing to a Google Sheet directly from Excel, or a javascript App that needs access to scriptDB (scriptDB API for JavaScript already does this of course, but it is scriptDB specific). 

This section is largely superseded by JSON API for data abstraction classes and VBA API using the Google Apps Script dbab library, but the library discussed below is used by both of these

As a web app, you would access via a POST or a GET, with parameters specified either as url parameters, in the post body of your request, or a mix of both. For example

webappurl?driver=scriptdb&query={"region":"Asia"}&siloid=play

Library


This API uses another library, through which you can access the DataHandlerRest class. 
  • McxgTjMRcbw1FnIo94Nml5Ki_d-phDA33 
Although it uses the libraries mentioned in Database abstraction with google apps script, there is no need to include them explicitly.

Setting it up

Almost everything you need is already in the DataHandlerRest class, but you'll need to set up your own version of the web app, so that you can access your own sheets, credentials and databases as yourself. Here's what you need to do.
  • Take a copy of https://script.google.com/d/1ObQiAzexnPIvciNrgaQ5llrosbN4our-fLTxGPFOhO7GiEsHAfi_jPyU/edit?usp=sharing
  • Check resources - you should be set up to access the cDataHandlerRest library.
  • If you are using parse.com make sure you know your keys. You can either set them up in your user properties, as default parameters or pass them as url parameters or in HTTP post data.
  • If you are using a spreadsheet, set it up and create a sheet to use to store data. Set up the ssid of that spreadsheet as default if you are not planning on having to pass it to your web app as a parameter.
  • Check the default parameters function. You shouldn't need to change it, except perhaps to change the path of your scriptDb or to tweak how you get your parse parameters if you use them. You can also set up defaults for your siloId and so on if necessary. These defaults are used when they are missing from the url parameters.

This is the type of response you will get from the API. Note that if you specify the callback parameter, JSONP will be returned, otherwise you'll get plain JSON.

{"handleCode":1,"handleError":"(Datahandler says:CACHE) this came from cache",
"data":[{"country":"Canada","region":"North America","dc":1},
{"country":"Scotland","region":"Europe","dc":44},
{"country":"United States","region":"North America","dc":1},
{"country":"England","region":"Europe","dc":44},
{"country":"China","region":"Asia","dc":86},
{"country":"Japan","region":"Asia","dc":81}]}






For help and more information join our forum,follow the blog or follow me on twitter .


Comments