From 6a0170aabec325291261d6e1a8fe9f49a469084b Mon Sep 17 00:00:00 2001 From: Tim Pierce Date: Tue, 21 Jan 2014 19:19:49 -0500 Subject: [PATCH] Cache the API discovery doc for at least 24 hours (refs #1923) --- sdk/cli/bin/arv | 28 +++++++++++++++++++--------- sdk/ruby/lib/arvados.rb | 20 ++++++++++++++------ 2 files changed, 33 insertions(+), 15 deletions(-) diff --git a/sdk/cli/bin/arv b/sdk/cli/bin/arv index 3955c94576..62b22ca120 100755 --- a/sdk/cli/bin/arv +++ b/sdk/cli/bin/arv @@ -4,6 +4,8 @@ # # Ward Vandewege +require 'fileutils' + if RUBY_VERSION < '1.9.3' then abort <<-EOS #{$0.gsub(/^\.\//,'')} requires Ruby version 1.9.3 or higher. @@ -116,15 +118,23 @@ end class Google::APIClient def discovery_document(api, version) - api = api.to_s - return @discovery_documents["#{api}:#{version}"] ||= (begin - response = self.execute!( - :http_method => :get, - :uri => self.discovery_uri(api, version), - :authenticated => false - ) - response.body.class == String ? JSON.parse(response.body) : response.body - end) + api = api.to_s + return @discovery_documents["#{api}:#{version}"] ||= + begin + # fetch new API discovery doc if stale + cached_doc = File.expand_path '~/.cache/arvados/discovery_uri.json' + if not File.exist?(cached_doc) or (Time.now - File.mtime(cached_doc)) > 86400 + response = self.execute!(:http_method => :get, + :uri => self.discovery_uri(api, version), + :authenticated => false) + FileUtils.makedirs(File.dirname cached_doc) + File.open(cached_doc, 'w') do |f| + f.puts response.body + end + end + + File.open(cached_doc) { |f| JSON.load f } + end end end diff --git a/sdk/ruby/lib/arvados.rb b/sdk/ruby/lib/arvados.rb index 5be1fd939e..f8bb4c0e70 100644 --- a/sdk/ruby/lib/arvados.rb +++ b/sdk/ruby/lib/arvados.rb @@ -2,6 +2,7 @@ require 'rubygems' require 'google/api_client' require 'active_support/inflector' require 'json' +require 'fileutils' ActiveSupport::Inflector.inflections do |inflect| inflect.irregular 'specimen', 'specimens' @@ -109,12 +110,19 @@ class Arvados api = api.to_s return @discovery_documents["#{api}:#{version}"] ||= begin - response = self.execute!( - :http_method => :get, - :uri => self.discovery_uri(api, version), - :authenticated => false - ) - response.body.class == String ? JSON.parse(response.body) : response.body + # fetch new API discovery doc if stale + cached_doc = File.expand_path '~/.cache/arvados/discovery_uri.json' + if not File.exist?(cached_doc) or (Time.now - File.mtime(cached_doc)) > 86400 + response = self.execute!(:http_method => :get, + :uri => self.discovery_uri(api, version), + :authenticated => false) + FileUtils.makedirs(File.dirname cached_doc) + File.open(cached_doc, 'w') do |f| + f.puts response.body + end + end + + File.open(cached_doc) { |f| JSON.load f } end end end -- 2.39.5