From 7724b20e9a9bfa9154f7242bffa08d8de5bea4ff Mon Sep 17 00:00:00 2001 From: Bob Aman Date: Thu, 22 Sep 2011 10:41:13 +0300 Subject: [PATCH] Changed discovery and directory methods to use the main code-path for generating requests. --- lib/google/api_client.rb | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/google/api_client.rb b/lib/google/api_client.rb index 2921425f8..e12a9d637 100644 --- a/lib/google/api_client.rb +++ b/lib/google/api_client.rb @@ -268,23 +268,30 @@ module Google # @return [Hash] The parsed JSON from the directory document. def directory_document return @directory_document ||= (begin - request_uri = self.directory_uri - request = ['GET', request_uri, [], []] + request = self.generate_request( + :http_method => 'GET', + :uri => self.directory_uri, + :authenticated => false + ) response = self.transmit(request) status, headers, body = response - if status == 200 # TODO(bobaman) Better status code handling? + if status >= 200 && status < 300 + # TODO(bobaman) Better status code handling? merged_body = body.inject(StringIO.new) do |accu, chunk| accu.write(chunk) accu end ::JSON.parse(merged_body.string) elsif status >= 400 && status < 500 + _, request_uri, _, _ = request raise ClientError, "Could not retrieve discovery document at: #{request_uri}" elsif status >= 500 && status < 600 + _, request_uri, _, _ = request raise ServerError, "Could not retrieve discovery document at: #{request_uri}" elsif status > 600 + _, request_uri, _, _ = request raise TransmissionError, "Could not retrieve discovery document at: #{request_uri}" end @@ -301,23 +308,30 @@ module Google api = api.to_s version = version || 'v1' return @discovery_documents["#{api}:#{version}"] ||= (begin - request_uri = self.discovery_uri(api, version) - request = ['GET', request_uri, [], []] + request = self.generate_request( + :http_method => 'GET', + :uri => self.discovery_uri(api, version), + :authenticated => false + ) response = self.transmit(request) status, headers, body = response - if status == 200 # TODO(bobaman) Better status code handling? + if status >= 200 && status < 300 + # TODO(bobaman) Better status code handling? merged_body = body.inject(StringIO.new) do |accu, chunk| accu.write(chunk) accu end ::JSON.parse(merged_body.string) elsif status >= 400 && status < 500 + _, request_uri, _, _ = request raise ClientError, "Could not retrieve discovery document at: #{request_uri}" elsif status >= 500 && status < 600 + _, request_uri, _, _ = request raise ServerError, "Could not retrieve discovery document at: #{request_uri}" elsif status > 600 + _, request_uri, _, _ = request raise TransmissionError, "Could not retrieve discovery document at: #{request_uri}" end