Changed discovery and directory methods to use the main code-path for generating requests.

This commit is contained in:
Bob Aman 2011-09-22 10:41:13 +03:00
parent e399dd76b0
commit 4fa30fc051
1 changed files with 20 additions and 6 deletions

View File

@ -268,23 +268,30 @@ module Google
# @return [Hash] The parsed JSON from the directory document. # @return [Hash] The parsed JSON from the directory document.
def directory_document def directory_document
return @directory_document ||= (begin return @directory_document ||= (begin
request_uri = self.directory_uri request = self.generate_request(
request = ['GET', request_uri, [], []] :http_method => 'GET',
:uri => self.directory_uri,
:authenticated => false
)
response = self.transmit(request) response = self.transmit(request)
status, headers, body = response 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| merged_body = body.inject(StringIO.new) do |accu, chunk|
accu.write(chunk) accu.write(chunk)
accu accu
end end
::JSON.parse(merged_body.string) ::JSON.parse(merged_body.string)
elsif status >= 400 && status < 500 elsif status >= 400 && status < 500
_, request_uri, _, _ = request
raise ClientError, raise ClientError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
elsif status >= 500 && status < 600 elsif status >= 500 && status < 600
_, request_uri, _, _ = request
raise ServerError, raise ServerError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
elsif status > 600 elsif status > 600
_, request_uri, _, _ = request
raise TransmissionError, raise TransmissionError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
end end
@ -301,23 +308,30 @@ module Google
api = api.to_s api = api.to_s
version = version || 'v1' version = version || 'v1'
return @discovery_documents["#{api}:#{version}"] ||= (begin return @discovery_documents["#{api}:#{version}"] ||= (begin
request_uri = self.discovery_uri(api, version) request = self.generate_request(
request = ['GET', request_uri, [], []] :http_method => 'GET',
:uri => self.discovery_uri(api, version),
:authenticated => false
)
response = self.transmit(request) response = self.transmit(request)
status, headers, body = response 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| merged_body = body.inject(StringIO.new) do |accu, chunk|
accu.write(chunk) accu.write(chunk)
accu accu
end end
::JSON.parse(merged_body.string) ::JSON.parse(merged_body.string)
elsif status >= 400 && status < 500 elsif status >= 400 && status < 500
_, request_uri, _, _ = request
raise ClientError, raise ClientError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
elsif status >= 500 && status < 600 elsif status >= 500 && status < 600
_, request_uri, _, _ = request
raise ServerError, raise ServerError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
elsif status > 600 elsif status > 600
_, request_uri, _, _ = request
raise TransmissionError, raise TransmissionError,
"Could not retrieve discovery document at: #{request_uri}" "Could not retrieve discovery document at: #{request_uri}"
end end