2011-07-29 22:07:04 +00:00
|
|
|
# Copyright 2010 Google Inc.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
2011-12-02 13:43:55 +00:00
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
module Google
|
|
|
|
class APIClient
|
|
|
|
##
|
|
|
|
# This class wraps a result returned by an API call.
|
|
|
|
class Result
|
2012-09-28 19:07:11 +00:00
|
|
|
extend Forwardable
|
|
|
|
|
|
|
|
##
|
|
|
|
# Init the result
|
|
|
|
#
|
|
|
|
# @param [Google::APIClient::Request] request
|
|
|
|
# The original request
|
|
|
|
# @param [Faraday::Response] response
|
|
|
|
# Raw HTTP Response
|
|
|
|
def initialize(request, response)
|
|
|
|
@request = request
|
2011-07-29 22:07:04 +00:00
|
|
|
@response = response
|
2012-09-11 18:04:21 +00:00
|
|
|
@media_upload = reference if reference.kind_of?(ResumableUpload)
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
|
2012-09-28 23:11:17 +00:00
|
|
|
# @return [Google::APIClient::Request] Original request object
|
2012-09-28 19:07:11 +00:00
|
|
|
attr_reader :request
|
2012-09-28 23:11:17 +00:00
|
|
|
# @return [Faraday::Response] HTTP response
|
2011-07-29 22:07:04 +00:00
|
|
|
attr_reader :response
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] reference
|
|
|
|
# @return [Google::APIClient::Request] Original request object
|
|
|
|
# @deprecated See {#request}
|
2012-09-28 19:07:11 +00:00
|
|
|
alias_method :reference, :request # For compatibility with pre-beta clients
|
2011-07-29 22:07:04 +00:00
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
# @!attribute [r] status
|
2012-09-28 23:29:51 +00:00
|
|
|
# @return [Fixnum] HTTP status code
|
2012-09-28 19:07:11 +00:00
|
|
|
# @!attribute [r] headers
|
|
|
|
# @return [Hash] HTTP response headers
|
|
|
|
# @!attribute [r] body
|
|
|
|
# @return [String] HTTP response body
|
|
|
|
def_delegators :@response, :status, :headers, :body
|
2011-07-29 22:07:04 +00:00
|
|
|
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] resumable_upload
|
|
|
|
# @return [Google::APIClient::ResumableUpload] For resuming media uploads
|
2012-09-11 18:04:21 +00:00
|
|
|
def resumable_upload
|
|
|
|
@media_upload ||= (
|
|
|
|
options = self.reference.to_hash.merge(
|
|
|
|
:uri => self.headers['location'],
|
|
|
|
:media => self.reference.media
|
|
|
|
)
|
|
|
|
Google::APIClient::ResumableUpload.new(options)
|
|
|
|
)
|
2012-03-02 01:23:36 +00:00
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Get the content type of the response
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] media_type
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [String]
|
|
|
|
# Value of content-type header
|
2012-03-02 01:23:36 +00:00
|
|
|
def media_type
|
|
|
|
_, content_type = self.headers.detect do |h, v|
|
|
|
|
h.downcase == 'Content-Type'.downcase
|
|
|
|
end
|
2012-10-05 01:17:54 +00:00
|
|
|
if content_type
|
|
|
|
return content_type[/^([^;]*);?.*$/, 1].strip.downcase
|
|
|
|
else
|
|
|
|
return nil
|
|
|
|
end
|
2012-03-02 01:23:36 +00:00
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Check if request failed
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] error?
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [TrueClass, FalseClass]
|
|
|
|
# true if result of operation is an error
|
2012-07-25 03:35:07 +00:00
|
|
|
def error?
|
|
|
|
return self.response.status >= 400
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Check if request was successful
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] success?
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [TrueClass, FalseClass]
|
|
|
|
# true if result of operation was successful
|
2012-07-25 03:35:07 +00:00
|
|
|
def success?
|
|
|
|
return !self.error?
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Extracts error messages from the response body
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] error_message
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [String]
|
|
|
|
# error message, if available
|
2012-07-25 03:35:07 +00:00
|
|
|
def error_message
|
|
|
|
if self.data?
|
|
|
|
if self.data.respond_to?(:error) &&
|
|
|
|
self.data.error.respond_to?(:message)
|
|
|
|
# You're going to get a terrible error message if the response isn't
|
|
|
|
# parsed successfully as an error.
|
|
|
|
return self.data.error.message
|
|
|
|
elsif self.data['error'] && self.data['error']['message']
|
|
|
|
return self.data['error']['message']
|
|
|
|
end
|
|
|
|
end
|
|
|
|
return self.body
|
|
|
|
end
|
2012-09-28 19:07:11 +00:00
|
|
|
|
|
|
|
##
|
|
|
|
# Check for parsable data in response
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] data?
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [TrueClass, FalseClass]
|
|
|
|
# true if body can be parsed
|
2012-03-02 01:23:36 +00:00
|
|
|
def data?
|
2012-10-05 01:17:54 +00:00
|
|
|
!(self.body.nil? || self.body.empty? || self.media_type != 'application/json')
|
2012-03-02 01:23:36 +00:00
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Return parsed version of the response body.
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] data
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [Object, Hash, String]
|
|
|
|
# Object if body parsable from API schema, Hash if JSON, raw body if unable to parse
|
2011-07-29 22:07:04 +00:00
|
|
|
def data
|
|
|
|
return @data ||= (begin
|
2012-10-05 01:17:54 +00:00
|
|
|
if self.data?
|
|
|
|
media_type = self.media_type
|
|
|
|
data = self.body
|
|
|
|
case media_type
|
|
|
|
when 'application/json'
|
|
|
|
data = MultiJson.load(data)
|
|
|
|
# Strip data wrapper, if present
|
|
|
|
data = data['data'] if data.has_key?('data')
|
|
|
|
else
|
|
|
|
raise ArgumentError,
|
|
|
|
"Content-Type not supported for parsing: #{media_type}"
|
|
|
|
end
|
|
|
|
if @request.api_method && @request.api_method.response_schema
|
|
|
|
# Automatically parse using the schema designated for the
|
|
|
|
# response of this API method.
|
|
|
|
data = @request.api_method.response_schema.new(data)
|
|
|
|
data
|
|
|
|
else
|
|
|
|
# Otherwise, return the raw unparsed value.
|
|
|
|
# This value must be indexable like a Hash.
|
|
|
|
data
|
|
|
|
end
|
2011-08-10 20:48:22 +00:00
|
|
|
end
|
2011-07-29 22:07:04 +00:00
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Get the token used for requesting the next page of data
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] next_page_token
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [String]
|
|
|
|
# next page token
|
2011-07-29 22:07:04 +00:00
|
|
|
def next_page_token
|
|
|
|
if self.data.respond_to?(:next_page_token)
|
|
|
|
return self.data.next_page_token
|
|
|
|
elsif self.data.respond_to?(:[])
|
|
|
|
return self.data["nextPageToken"]
|
|
|
|
else
|
|
|
|
raise TypeError, "Data object did not respond to #next_page_token."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Build a request for fetching the next page of data
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Request]
|
2014-12-17 04:57:12 +00:00
|
|
|
# API request for retrieving next page, nil if no page token available
|
2011-07-29 22:07:04 +00:00
|
|
|
def next_page
|
2014-12-17 04:57:12 +00:00
|
|
|
return nil unless self.next_page_token
|
2011-07-29 22:07:04 +00:00
|
|
|
merged_parameters = Hash[self.reference.parameters].merge({
|
|
|
|
self.page_token_param => self.next_page_token
|
|
|
|
})
|
2012-09-11 18:04:21 +00:00
|
|
|
# Because Requests can be coerced to Hashes, we can merge them,
|
2011-07-29 22:07:04 +00:00
|
|
|
# preserving all context except the API method parameters that we're
|
|
|
|
# using for pagination.
|
2012-09-11 18:04:21 +00:00
|
|
|
return Google::APIClient::Request.new(
|
2011-07-29 22:07:04 +00:00
|
|
|
Hash[self.reference].merge(:parameters => merged_parameters)
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Get the token used for requesting the previous page of data
|
|
|
|
#
|
2012-09-28 23:11:17 +00:00
|
|
|
# @!attribute [r] prev_page_token
|
2012-09-28 19:07:11 +00:00
|
|
|
# @return [String]
|
|
|
|
# previous page token
|
2011-07-29 22:07:04 +00:00
|
|
|
def prev_page_token
|
|
|
|
if self.data.respond_to?(:prev_page_token)
|
|
|
|
return self.data.prev_page_token
|
|
|
|
elsif self.data.respond_to?(:[])
|
|
|
|
return self.data["prevPageToken"]
|
|
|
|
else
|
|
|
|
raise TypeError, "Data object did not respond to #next_page_token."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-09-28 19:07:11 +00:00
|
|
|
##
|
|
|
|
# Build a request for fetching the previous page of data
|
|
|
|
#
|
|
|
|
# @return [Google::APIClient::Request]
|
2014-12-17 04:57:12 +00:00
|
|
|
# API request for retrieving previous page, nil if no page token available
|
2011-07-29 22:07:04 +00:00
|
|
|
def prev_page
|
2014-12-17 04:57:12 +00:00
|
|
|
return nil unless self.prev_page_token
|
2011-07-29 22:07:04 +00:00
|
|
|
merged_parameters = Hash[self.reference.parameters].merge({
|
|
|
|
self.page_token_param => self.prev_page_token
|
|
|
|
})
|
2012-09-11 18:04:21 +00:00
|
|
|
# Because Requests can be coerced to Hashes, we can merge them,
|
2011-07-29 22:07:04 +00:00
|
|
|
# preserving all context except the API method parameters that we're
|
|
|
|
# using for pagination.
|
2012-09-11 18:04:21 +00:00
|
|
|
return Google::APIClient::Request.new(
|
2011-07-29 22:07:04 +00:00
|
|
|
Hash[self.reference].merge(:parameters => merged_parameters)
|
|
|
|
)
|
|
|
|
end
|
2012-09-28 19:07:11 +00:00
|
|
|
|
2012-09-28 23:11:17 +00:00
|
|
|
##
|
|
|
|
# Pagination scheme used by this request/response
|
|
|
|
#
|
|
|
|
# @!attribute [r] pagination_type
|
|
|
|
# @return [Symbol]
|
|
|
|
# currently always :token
|
2012-09-28 19:07:11 +00:00
|
|
|
def pagination_type
|
|
|
|
return :token
|
|
|
|
end
|
|
|
|
|
2012-09-28 23:11:17 +00:00
|
|
|
##
|
|
|
|
# Name of the field that contains the pagination token
|
|
|
|
#
|
|
|
|
# @!attribute [r] page_token_param
|
|
|
|
# @return [String]
|
|
|
|
# currently always 'pageToken'
|
2012-09-28 19:07:11 +00:00
|
|
|
def page_token_param
|
|
|
|
return "pageToken"
|
|
|
|
end
|
|
|
|
|
2011-07-29 22:07:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|