From 2eb6da99d33264aa1cdf95b6c6a848fe21c6e4ec Mon Sep 17 00:00:00 2001 From: Nikolay Mischenko Date: Wed, 15 Feb 2012 16:25:48 +0300 Subject: [PATCH 1/7] Fixed parameters encoding (should be urlencoded) --- lib/google/api_client/discovery/method.rb | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/google/api_client/discovery/method.rb b/lib/google/api_client/discovery/method.rb index a1ec1b455..8f0198507 100644 --- a/lib/google/api_client/discovery/method.rb +++ b/lib/google/api_client/discovery/method.rb @@ -172,12 +172,14 @@ module Google query_parameters = parameters.reject do |k, v| template_variables.include?(k) end - if query_parameters.size > 0 - uri.query_values = (uri.query_values || []) + query_parameters + # encode all non-template parameters + params = "" + unless query_parameters.empty? + params = "?" + Addressable::URI.form_encode(query_parameters) end # Normalization is necessary because of undesirable percent-escaping # during URI template expansion - return uri.normalize + return uri.normalize + params end ## From c50f92c50cb77b879ed247d789bd944f18e3f95e Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 03:23:03 -0500 Subject: [PATCH 2/7] change ApiClient's configurable host to a configurable baseURI, so that protocol, port, and base path may be overridden in addition to host --- lib/google/api_client.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 090dda26a..e75bb784d 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -47,8 +47,8 @@ module Google #
  • :oauth_1
  • #
  • :oauth_2
  • # - # @option options [String] :host ("www.googleapis.com") - # The API hostname used by the client. This rarely needs to be changed. + # @option options [String] :baseURI ("https://www.googleapis.com/discovery/v1") + # The base API URI used by the client. This rarely needs to be changed. # @option options [String] :application_name # The name of the application using the client. # @option options [String] :application_version @@ -63,8 +63,9 @@ module Google accu[key.to_s] = value accu end - # Almost all API usage will have a host of 'www.googleapis.com'. - self.host = options["host"] || 'www.googleapis.com' + # Almost all API usage will have this base URI + self.baseURI = options["baseURI"] || "https://www.googleapis.com/discovery/v1" + # Most developers will want to leave this value alone and use the # application_name option. application_string = ( @@ -165,7 +166,7 @@ module Google # # @return [String] # The API hostname. Should almost always be 'www.googleapis.com'. - attr_accessor :host + attr_accessor :baseURI ## # The user agent used by the client. From d59901bf1edb4e4cb620ed4bcb0ea5134e25bf31 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 28 Dec 2011 21:04:16 -0700 Subject: [PATCH 3/7] add method ApiClient#relative_uri which constructs a full uri from a given relative path, respecting specified protocol, host, and port; and allowing expanded parameters passed through Addressable::Template --- lib/google/api_client.rb | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index e75bb784d..fb5f53626 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -174,16 +174,17 @@ module Google # @return [String] # The user agent string used in the User-Agent header. attr_accessor :user_agent + + def relative_uri(path, expand={}) + Addressable::Template.new(baseURI+path).expand(expand) + end ## # Returns the URI for the directory document. # # @return [Addressable::URI] The URI of the directory document. def directory_uri - template = Addressable::Template.new( - "https://{host}/discovery/v1/apis" - ) - return template.expand({"host" => self.host}) + relative_uri('/apis') end ## @@ -209,15 +210,7 @@ module Google api = api.to_s version = version || 'v1' return @discovery_uris["#{api}:#{version}"] ||= (begin - template = Addressable::Template.new( - "https://{host}/discovery/v1/apis/" + - "{api}/{version}/rest" - ) - template.expand({ - "host" => self.host, - "api" => api, - "version" => version - }) + relative_uri("/apis/{api}/{version}/rest", 'api' => api, 'version' => version) end) end From bd1ce540d48505e5eb216cf47f4807946ffcffc3 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 03:24:22 -0500 Subject: [PATCH 4/7] if ApiClient receives :authorization => false, respect that, and only default to oauth2 if :authorization is omitted --- lib/google/api_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index fb5f53626..91aec235d 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -81,7 +81,7 @@ module Google ).strip # The writer method understands a few Symbols and will generate useful # default authentication mechanisms. - self.authorization = options["authorization"] || :oauth_2 + self.authorization = options.key?("authorization") ? options["authorization"] : :oauth_2 self.key = options["key"] self.user_ip = options["user_ip"] @discovery_uris = {} From b6ef9b00ad41bbc7e75f0f7245c0a7226152c6da Mon Sep 17 00:00:00 2001 From: Ethan Date: Sun, 8 Jan 2012 01:38:48 -0500 Subject: [PATCH 5/7] fix schema to not choke when '$ref' is specifying what the '$ref' property actually is, rather than invoking it, and is therefore not a string. this applies to the schema JsonSchema, specified in the document for the discovery service itself --- lib/google/api_client/discovery/schema.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/google/api_client/discovery/schema.rb b/lib/google/api_client/discovery/schema.rb index f3499ebdc..2b4d59833 100644 --- a/lib/google/api_client/discovery/schema.rb +++ b/lib/google/api_client/discovery/schema.rb @@ -47,7 +47,7 @@ module Google # and excess object creation, but this hopefully shouldn't be an # issue since it should only be called only once per schema per # process. - if data.kind_of?(Hash) && data['$ref'] + if data.kind_of?(Hash) && data['$ref'].is_a?(String) reference = data['$ref'] reference = '#' + reference if reference[0..0] != '#' data.merge({ From 80c41c99fc91c9818dbc7c5840b584de4d48ae23 Mon Sep 17 00:00:00 2001 From: Ethan Date: Wed, 25 Jan 2012 05:01:30 -0500 Subject: [PATCH 6/7] minor whitespace touchup gedit's syntax highlighting breaks on the previous --- lib/google/api_client.rb | 2 +- lib/google/api_client/discovery/api.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 91aec235d..94285acb8 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -590,7 +590,7 @@ module Google unless headers.kind_of?(Enumerable) # We need to use some Enumerable methods, relying on the presence of # the #each method. - class < Date: Mon, 30 Jan 2012 22:33:03 -0500 Subject: [PATCH 7/7] rescue any error invoking external commands attempting to determine OS_VERSION; just return RUBY_PLATFORM on failure. --- lib/google/api_client/environment.rb | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/lib/google/api_client/environment.rb b/lib/google/api_client/environment.rb index e5abe9334..9faa28f7d 100644 --- a/lib/google/api_client/environment.rb +++ b/lib/google/api_client/environment.rb @@ -16,15 +16,19 @@ module Google class APIClient module ENV - OS_VERSION = if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/ - # TODO(bobaman) - # Confirm that all of these Windows environments actually have access - # to the `ver` command. - `ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip - elsif RUBY_PLATFORM =~ /darwin/i - "Mac OS X/#{`sw_vers -productVersion`}" - else - `uname -sr`.sub(' ', '/') + OS_VERSION = begin + if RUBY_PLATFORM =~ /mswin|win32|mingw|bccwin|cygwin/ + # TODO(bobaman) + # Confirm that all of these Windows environments actually have access + # to the `ver` command. + `ver`.sub(/\s*\[Version\s*/, '/').sub(']', '').strip + elsif RUBY_PLATFORM =~ /darwin/i + "Mac OS X/#{`sw_vers -productVersion`}" + else + `uname -sr`.sub(' ', '/') + end + rescue Exception + RUBY_PLATFORM end end end