{"id":1359,"date":"2013-12-04T21:27:27","date_gmt":"2013-12-05T03:27:27","guid":{"rendered":"http:\/\/www.mooreds.com\/wordpress\/?p=1359"},"modified":"2013-11-15T21:42:35","modified_gmt":"2013-11-16T03:42:35","slug":"google-spreadsheets-as-rest-api-sources","status":"publish","type":"post","link":"https:\/\/www.mooreds.com\/wordpress\/archives\/1359","title":{"rendered":"Google Spreadsheets as REST API sources"},"content":{"rendered":"<p>I recently built out a read only JSON API with a Google spreadsheet as the back end data source.<\/p>\n<p>Why do such a thing?  I didn&#8217;t need to modify the back end, but wanted to make the data available to other software.  The people who maintain this data are very comfortable using Google spreadsheets.  While I could have written a custom CRUD app, this didn&#8217;t seem like a good use of time when Google spreadsheets had served us well in the past.<\/p>\n<p>How did I do this?  I first of all created a sanitized spreadsheet, using <a href=\"https:\/\/support.google.com\/drive\/table\/25273?hl=en\">importrange<\/a> and regexreplace.  This level of indirection assures me that if the source data changes, I can adjust fairly easily.  If the user managing the spreadsheet wants to rearrange columns, I can adjust my sanitized spreadsheet easily.<\/p>\n<p>Then I created a Google apps script and <a href=\"https:\/\/developers.google.com\/apps-script\/guides\/html\/\">used doGet to respond to requests<\/a> and the <a href=\"https:\/\/developers.google.com\/apps-script\/reference\/spreadsheet\/\">spreadsheet service<\/a> to retrieve the data.  The <a href=\"https:\/\/developers.google.com\/apps-script\/guides\/content#serving_json_from_scripts\">content service<\/a> lets you serve JSON with the appropriate mime type.  I used <a href=\"https:\/\/github.com\/simula-innovation\/qunit\/tree\/gas\/gas\">qunit for Google apps script<\/a> (invaluable when you are working in the loosely typed javascript world and relying on cloud resources).  I also worked with the parameters to build the querying needed for our application.<\/p>\n<p>Then, in order to make it look like a normal API call, I fronted the script with <a href=\"https:\/\/www.varnish-cache.org\/\">Varnish<\/a> and did some regsub magic in my VCL file (as well as some light authentication).  <\/p>\n<p>This approach has the benefit of keeping everything in Google&#8217;s cloud, and allowing you to access the spreadsheet data easily.<\/p>\n<p>This approach has significant limitations, however.<\/p>\n<ul>\n<li>Google apps scripts calls are slow, especially when accessing spreadsheet data.  Using <a href=\"https:\/\/developers.google.com\/apps-script\/reference\/cache\/\">the cache service<\/a> can help.<\/li>\n<li>You cannot return anything other than a 200 response code.  None of the other response codes are available.<\/li>\n<li>The actual content is <a href=\"https:\/\/developers.google.com\/apps-script\/guides\/content#redirects\">served after a redirect<\/a>, so caching it at the Varnish level is difficult (<a href=\"http:\/\/microrants.blogspot.com\/2012\/11\/caching-301-redirects-in-varnish-while.html\">though possible<\/a>), and clients must be able to follow redirects.<\/li>\n<li>Google changes the ip of the server running the script.  This is not such a big problem, unless your version of Varnish only takes IP addresses in the VCL file, not hostnames.  Like ours.<\/li>\n<\/ul>\n<p>Was this a good idea?  Well, it let me build out the API relatively quickly without affecting the users managing the data or finding any other place to put it.  But we&#8217;ll probably move away from this due to the limitations listed above.  One we&#8217;ve found particularly painful is the IP address switching, which usually only shows up in our automated testing.  <\/p>\n<p>We&#8217;ll probably start pushing the data daily (it doesn&#8217;t change all that often) to a local JDBC database using the <a href=\"https:\/\/developers.google.com\/apps-script\/guides\/jdbc\">JDBC service<\/a> and use either <a href=\"http:\/\/restsql.org\/\">RestSQL<\/a> or <a href=\"http:\/\/dropwizard.io\/\">DropWizard<\/a> to generate an API for it.  (RestSQL is quicker, but DropWizard lets us maintain format compatibility.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I recently built out a read only JSON API with a Google spreadsheet as the back end data source. Why do such a thing? I didn&#8217;t need to modify the [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62,53,5,54],"tags":[],"class_list":["post-1359","post","type-post","status-publish","format-standard","hentry","category-apis","category-google-apps","category-java","category-javascript"],"_links":{"self":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1359","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1359"}],"version-history":[{"count":2,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1359\/revisions"}],"predecessor-version":[{"id":1361,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/posts\/1359\/revisions\/1361"}],"wp:attachment":[{"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1359"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1359"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.mooreds.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1359"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}