An Ephemeral data exchange using Google Apps Script caching

This came about as I was trying to figure out how to 
  • generate an html app from Excel
  • collect some data in that app
  • get it back into Excel
but actually it turned into a more general solution for an easy way to transfer ephemeral data between applications and platforms without getting involved in complex authorizations, APIS and hosting. 

Implementation

The idea is that a web app can talk to Google CacheService, and serves up results as JSON or JSONP. Anybody that wants to dump something there for  s short time  for someone else (or something else) to pick up does so via a simple JSON REST type approach. Here's the design principles.
  • Maximum 5MB payload
  • Maximum lifetime of 6 hours
  • You need an API key to write
  • To read, you just need to know the key of the item, and have been authorized via a list of readers by the owner of the item
  • To re-write, you need to know the key of the item, and be the item's owner, or been authorized in a list of writers by the owner of the item.
  • It's open, even anonymous, so if keys are shared around then data will be public
  • No tracking of any sort is done.
  • Small packages of data can be passed as uri parameter in a GET request to limit cross origin problems. POST is required for larger payloads.

Parameters

Here's the parameters
  • apikey - a code that allows writing to the exchange. Just ask me for one if you want to use my webapp. 
  • id - the id of the item 
  • writer - a key of the person writing. You can use any key you like, but better to let the exchange generate one for you
  • reader - a key to be able to read an item. Again, you can use any key you like, but better to let the exchange generate one for you.
  • writers - a comma separated list of writer keys who can overwrite a given item
  • readers - a comma separated list of reader keys who can read a given item
  • register - ask the exchange to generate a reader (and writer if apikey is given) for you.
  • callback - if specified then the response will be jsonp
  • data - the data to post
Although the writer and reader keys can be anything you like, it's best to have different reader and writer keys. Like that you can keep your writer key private and only share your reader key so others can share things with you. There's nothing special about reader or writer keys - they are just random strings. Changing them often is a good thing, and if you are not sharing with anyone else, you could easily just generate a new key every time you write something, or you could make up your own.

Examples

In each case, the response is JSON, but will be JSONP if callback is specified.

Get an obscure reader key
?register
{"ok": true,"id": "","reader": "8ddjxripeispchguqvww","writer": ""}

Get an obscure reader and writer key
?register&apikey=cex-rna-11fq0gb47
{"ok": true,"id": "","reader": "gpnwj6otyfiu0fwncuqt","writer": "g6dwvgfoi0uxzfomrpfg"}

Write a small amount of data, and generate reader and writer keys
?register&apikey=cex-rna-11fq0gb47&data=small amount of data
{"ok": true,"id": "z2psmxswwnuvvgtxufui","reader": "gwzixgizjruhkgkuuuu2","writer": "szhgcw2rihwvtquuvgku","readers": ["szhgcw2rihwvtquuvgku"],"writers": ["szhgcw2rihwvtquuvgku"]}

Read it back again
?reader=szhgcw2rihwvtquuvgku&id=z2psmxswwnuvvgtxufui
{"ok": true,"id": "z2psmxswwnuvvgtxufui","reader": "szhgcw2rihwvtquuvgku","writer": "","data": "small amount of data","created": 1481231239154}

Overwrite it with something else
?writer=szhgcw2rihwvtquuvgku&id=z2psmxswwnuvvgtxufui&apikey=cex-rna-11fq0gb47&data=different stuff
{"ok": true,"id": "z2psmxswwnuvvgtxufui","reader": "","writer": "szhgcw2rihwvtquuvgku","readers": ["szhgcw2rihwvtquuvgku"],"writers": ["szhgcw2rihwvtquuvgku"]}

Write something and allow others to read it
?writer=szhgcw2rihwvtquuvgku&apikey=cex-rna-11fq0gb47&data=shared stuff&readers=johnsreaderkey,tomsreaderkey
{"ok": true,"id": "ignxpaf6ibtmkvywfjmj","reader": "","writer": "szhgcw2rihwvtquuvgku","readers": ["johnsreaderkey","tomsreaderkey","szhgcw2rihwvtquuvgku"],"writers": ["szhgcw2rihwvtquuvgku"]}

Someone else reading it
?reader=tomsreaderkey&id=ignxpaf6ibtmkvywfjmj
{"ok": true,"id": "ignxpaf6ibtmkvywfjmj","reader": "tomsreaderkey","writer": "","data": "shared stuff","created": 1481232478683}

Write something and allow others to overwrite it
?writer=szhgcw2rihwvtquuvgku&apikey=cex-rna-11fq0gb47&data=shared stuff&writers=maryswriterkey
{"ok": true,"id": "o0ryhsvgwgkcrxeqigwv","reader": "","writer": "szhgcw2rihwvtquuvgku","readers": ["maryswriterkey","szhgcw2rihwvtquuvgku"],"writers": ["maryswriterkey","szhgcw2rihwvtquuvgku"]}

Someone else overwriting it. Note they are using their own apikey.
?writer=maryswriterkey&id=o0ryhsvgwgkcrxeqigwv&apikey=cex-gna-11tojsbck&data=marys stuff&readers=fredskey&writers=johnskey
{"ok": true,"id": "o0ryhsvgwgkcrxeqigwv","reader": "","writer": "maryswriterkey","readers": ["fredskey","johnskey","maryswriterkey"],"writers": ["johnskey","maryswriterkey"]}

Error messages

response.ok will be false if there are any errors, and there will be an error property describing the error, as in the example below
?writer=maryswriterkey&data=marys stuff&readers=fredskey&writers=johnskey
{"ok": false,"id": "","reader": "","writer": "maryswriterkey","error": "you need to specify an apikey & a writer - to generate a writer use a register parameter"}





For more like this, see Google Apps Scripts snippets. Why not join our forum, follow the blog or follow me on twitter to ensure you get updates when they are available.
Comments