Begin consolidation of request building in reference. Further changes coming to simplify batch + media handling
This commit is contained in:
parent
9f51031a00
commit
5d2a6d4842
|
@ -464,6 +464,31 @@ module Google
|
||||||
return nil
|
return nil
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def normalize_api_method(options)
|
||||||
|
method = options[:api_method]
|
||||||
|
version = options[:version]
|
||||||
|
if method.kind_of?(Google::APIClient::Method) || method == nil
|
||||||
|
return method
|
||||||
|
elsif method.respond_to?(:to_str) || method.kind_of?(Symbol)
|
||||||
|
# This method of guessing the API is unreliable. This will fail for
|
||||||
|
# APIs where the first segment of the RPC name does not match the
|
||||||
|
# service name. However, this is a fallback mechanism anyway.
|
||||||
|
# Developers should be passing in a reference to the method, rather
|
||||||
|
# than passing in a string or symbol. This should raise an error
|
||||||
|
# in the case of a mismatch.
|
||||||
|
method = method.to_s
|
||||||
|
api = method[/^([^.]+)\./, 1]
|
||||||
|
api_method = self.discovered_method(method, api, version)
|
||||||
|
if api_method.nil?
|
||||||
|
raise ArgumentError, "API method could not be found."
|
||||||
|
end
|
||||||
|
return api_method
|
||||||
|
else
|
||||||
|
raise TypeError,
|
||||||
|
"Expected Google::APIClient::Method, got #{new_api_method.class}."
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
# Generates a request.
|
# Generates a request.
|
||||||
#
|
#
|
||||||
|
@ -482,7 +507,7 @@ module Google
|
||||||
# `true` if the request must be signed or somehow
|
# `true` if the request must be signed or somehow
|
||||||
# authenticated, `false` otherwise.
|
# authenticated, `false` otherwise.
|
||||||
#
|
#
|
||||||
# @return [Faraday::Request] The generated request.
|
# @return [Google::APIClient::Reference] The generated request.
|
||||||
#
|
#
|
||||||
# @example
|
# @example
|
||||||
# request = client.generate_request(
|
# request = client.generate_request(
|
||||||
|
@ -502,35 +527,8 @@ module Google
|
||||||
:connection => Faraday.default_connection
|
:connection => Faraday.default_connection
|
||||||
}.merge(options)
|
}.merge(options)
|
||||||
|
|
||||||
# The Reference object is going to need this to do method ID lookups.
|
options[:api_method] = self.normalize_api_method(options)
|
||||||
options[:client] = self
|
return Google::APIClient::Reference.new(options)
|
||||||
# The default value for the :authenticated option depends on whether an
|
|
||||||
# authorization mechanism has been set.
|
|
||||||
if options[:authorization]
|
|
||||||
options = {:authenticated => true}.merge(options)
|
|
||||||
else
|
|
||||||
options = {:authenticated => false}.merge(options)
|
|
||||||
end
|
|
||||||
reference = Google::APIClient::Reference.new(options)
|
|
||||||
request = reference.to_request
|
|
||||||
if options[:authenticated] && options[:authorization].respond_to?(:generate_authenticated_request)
|
|
||||||
request = options[:authorization].generate_authenticated_request(
|
|
||||||
:request => request,
|
|
||||||
:connection => options[:connection]
|
|
||||||
)
|
|
||||||
end
|
|
||||||
return request
|
|
||||||
end
|
|
||||||
|
|
||||||
##
|
|
||||||
# Signs a request using the current authorization mechanism.
|
|
||||||
#
|
|
||||||
# @param [Hash] options a customizable set of options
|
|
||||||
#
|
|
||||||
# @return [Faraday::Request] The signed or otherwise authenticated request.
|
|
||||||
# @deprecated No longer used internally
|
|
||||||
def generate_authenticated_request(options={})
|
|
||||||
return authorization.generate_authenticated_request(options)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
##
|
##
|
||||||
|
@ -538,81 +536,14 @@ module Google
|
||||||
#
|
#
|
||||||
# @option options [Array, Faraday::Request] :request
|
# @option options [Array, Faraday::Request] :request
|
||||||
# The HTTP request to transmit.
|
# The HTTP request to transmit.
|
||||||
# @option options [String, Symbol] :method
|
|
||||||
# The method for the HTTP request.
|
|
||||||
# @option options [String, Addressable::URI] :uri
|
|
||||||
# The URI for the HTTP request.
|
|
||||||
# @option options [Array, Hash] :headers
|
|
||||||
# The headers for the HTTP request.
|
|
||||||
# @option options [String] :body
|
|
||||||
# The body for the HTTP request.
|
|
||||||
# @option options [Faraday::Connection] :connection
|
# @option options [Faraday::Connection] :connection
|
||||||
# The HTTP connection to use.
|
# The HTTP connection to use.
|
||||||
#
|
#
|
||||||
# @return [Faraday::Response] The response from the server.
|
# @return [Faraday::Response] The response from the server.
|
||||||
def transmit(options={})
|
def transmit(options={})
|
||||||
options[:connection] ||= Faraday.default_connection
|
options[:connection] ||= Faraday.default_connection
|
||||||
if options[:request]
|
request = options[:request]
|
||||||
if options[:request].kind_of?(Array)
|
request['User-Agent'] ||= '' + self.user_agent unless self.user_agent.nil?
|
||||||
method, uri, headers, body = options[:request]
|
|
||||||
elsif options[:request].kind_of?(Faraday::Request)
|
|
||||||
unless options[:connection]
|
|
||||||
raise ArgumentError,
|
|
||||||
"Faraday::Request used, requires a connection to be provided."
|
|
||||||
end
|
|
||||||
method = options[:request].method.to_s.downcase.to_sym
|
|
||||||
uri = options[:connection].build_url(
|
|
||||||
options[:request].path, options[:request].params
|
|
||||||
)
|
|
||||||
headers = options[:request].headers || {}
|
|
||||||
body = options[:request].body || ''
|
|
||||||
end
|
|
||||||
else
|
|
||||||
method = options[:method] || :get
|
|
||||||
uri = options[:uri]
|
|
||||||
headers = options[:headers] || []
|
|
||||||
body = options[:body] || ''
|
|
||||||
end
|
|
||||||
headers = headers.to_a if headers.kind_of?(Hash)
|
|
||||||
request_components = {
|
|
||||||
:method => method,
|
|
||||||
:uri => uri,
|
|
||||||
:headers => headers,
|
|
||||||
:body => body
|
|
||||||
}
|
|
||||||
# Verify that we have all pieces required to transmit an HTTP request
|
|
||||||
request_components.each do |(key, value)|
|
|
||||||
unless value
|
|
||||||
raise ArgumentError, "Missing :#{key} parameter."
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if self.user_agent != nil
|
|
||||||
# If there's no User-Agent header, set one.
|
|
||||||
unless headers.kind_of?(Enumerable)
|
|
||||||
# We need to use some Enumerable methods, relying on the presence of
|
|
||||||
# the #each method.
|
|
||||||
class << headers
|
|
||||||
include Enumerable
|
|
||||||
end
|
|
||||||
end
|
|
||||||
if self.user_agent.kind_of?(String)
|
|
||||||
unless headers.any? { |k, v| k.downcase == 'User-Agent'.downcase }
|
|
||||||
headers = headers.to_a.insert(0, ['User-Agent', self.user_agent])
|
|
||||||
end
|
|
||||||
elsif self.user_agent != nil
|
|
||||||
raise TypeError,
|
|
||||||
"Expected User-Agent to be String, got #{self.user_agent.class}"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
request = options[:connection].build_request(
|
|
||||||
method.to_s.downcase.to_sym
|
|
||||||
) do |req|
|
|
||||||
req.url(Addressable::URI.parse(uri).normalize.to_s)
|
|
||||||
req.headers = Faraday::Utils::Headers.new(headers)
|
|
||||||
req.body = body
|
|
||||||
end
|
|
||||||
request_env = request.to_env(options[:connection])
|
request_env = request.to_env(options[:connection])
|
||||||
response = options[:connection].app.call(request_env)
|
response = options[:connection].app.call(request_env)
|
||||||
return response
|
return response
|
||||||
|
@ -662,37 +593,14 @@ module Google
|
||||||
params.size == 1
|
params.size == 1
|
||||||
batch = params.pop
|
batch = params.pop
|
||||||
options = batch.options
|
options = batch.options
|
||||||
options[:connection] ||= Faraday.default_connection
|
method, uri, headers, body = batch.to_http_request
|
||||||
http_request = batch.to_http_request
|
reference = self.generate_request({
|
||||||
request = nil
|
:uri => uri,
|
||||||
|
:http_method => method,
|
||||||
if @authorization
|
:headers => headers,
|
||||||
method, uri, headers, body = http_request
|
:body => body
|
||||||
method = method.to_s.downcase.to_sym
|
}.merge(options))
|
||||||
|
response = self.transmit(:request => reference.to_http_request, :connection => options[:connection])
|
||||||
faraday_request = options[:connection].build_request(
|
|
||||||
method.to_s.downcase.to_sym
|
|
||||||
) do |req|
|
|
||||||
req.url(Addressable::URI.parse(uri).normalize.to_s)
|
|
||||||
req.headers = Faraday::Utils::Headers.new(headers)
|
|
||||||
req.body = body
|
|
||||||
end
|
|
||||||
|
|
||||||
request = {
|
|
||||||
:request => self.generate_authenticated_request(
|
|
||||||
:request => faraday_request,
|
|
||||||
:connection => options[:connection]
|
|
||||||
),
|
|
||||||
:connection => options[:connection]
|
|
||||||
}
|
|
||||||
else
|
|
||||||
request = {
|
|
||||||
:request => http_request,
|
|
||||||
:connection => options[:connection]
|
|
||||||
}
|
|
||||||
end
|
|
||||||
|
|
||||||
response = self.transmit(request)
|
|
||||||
batch.process_response(response)
|
batch.process_response(response)
|
||||||
return nil
|
return nil
|
||||||
else
|
else
|
||||||
|
@ -711,14 +619,13 @@ module Google
|
||||||
options[:body] = params.shift if params.size > 0
|
options[:body] = params.shift if params.size > 0
|
||||||
options[:headers] = params.shift if params.size > 0
|
options[:headers] = params.shift if params.size > 0
|
||||||
options[:client] = self
|
options[:client] = self
|
||||||
options[:connection] ||= Faraday.default_connection
|
reference = self.generate_request(options)
|
||||||
reference = Google::APIClient::Reference.new(options)
|
|
||||||
request = self.generate_request(reference)
|
|
||||||
response = self.transmit(
|
response = self.transmit(
|
||||||
:request => request,
|
:request => reference.to_http_request,
|
||||||
:connection => options[:connection]
|
:connection => options[:connection]
|
||||||
)
|
)
|
||||||
return Google::APIClient::Result.new(reference, request, response)
|
result = Google::APIClient::Result.new(reference, response)
|
||||||
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -121,7 +121,7 @@ module Google
|
||||||
call_response = deserialize_call_response(part)
|
call_response = deserialize_call_response(part)
|
||||||
callback = @callbacks[call_response.call_id]
|
callback = @callbacks[call_response.call_id]
|
||||||
call = @calls[call_response.call_id]
|
call = @calls[call_response.call_id]
|
||||||
result = Google::APIClient::Result.new(call, nil, call_response)
|
result = Google::APIClient::Result.new(call, call_response)
|
||||||
callback.call(result) if callback
|
callback.call(result) if callback
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -196,7 +196,7 @@ module Google
|
||||||
#
|
#
|
||||||
# @return [String] The request as a string in application/http format.
|
# @return [String] The request as a string in application/http format.
|
||||||
def serialize_call(call)
|
def serialize_call(call)
|
||||||
http_request = call.to_request
|
http_request = call.to_http_request
|
||||||
method = http_request.method.to_s.upcase
|
method = http_request.method.to_s.upcase
|
||||||
path = http_request.path.to_s
|
path = http_request.path.to_s
|
||||||
status_line = method + " " + path + " HTTP/1.1"
|
status_line = method + " " + path + " HTTP/1.1"
|
||||||
|
|
|
@ -224,13 +224,6 @@ module Google
|
||||||
# @return [Array] The generated HTTP request.
|
# @return [Array] The generated HTTP request.
|
||||||
def generate_request(parameters={}, body='', headers=[], options={})
|
def generate_request(parameters={}, body='', headers=[], options={})
|
||||||
options[:connection] ||= Faraday.default_connection
|
options[:connection] ||= Faraday.default_connection
|
||||||
if body.respond_to?(:string)
|
|
||||||
body = body.string
|
|
||||||
elsif body.respond_to?(:to_str)
|
|
||||||
body = body.to_str
|
|
||||||
else
|
|
||||||
raise TypeError, "Expected String or StringIO, got #{body.class}."
|
|
||||||
end
|
|
||||||
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
|
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
|
||||||
raise TypeError, "Expected Hash or Array, got #{headers.class}."
|
raise TypeError, "Expected Hash or Array, got #{headers.class}."
|
||||||
end
|
end
|
||||||
|
|
|
@ -12,7 +12,6 @@
|
||||||
# See the License for the specific language governing permissions and
|
# See the License for the specific language governing permissions and
|
||||||
# limitations under the License.
|
# limitations under the License.
|
||||||
|
|
||||||
|
|
||||||
require 'faraday'
|
require 'faraday'
|
||||||
require 'faraday/utils'
|
require 'faraday/utils'
|
||||||
require 'multi_json'
|
require 'multi_json'
|
||||||
|
@ -29,69 +28,58 @@ module Google
|
||||||
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
|
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
|
||||||
|
|
||||||
def initialize(options={})
|
def initialize(options={})
|
||||||
# We only need this to do lookups on method ID String values
|
|
||||||
# It's optional, but method ID lookups will fail if the client is
|
|
||||||
# omitted.
|
|
||||||
@client = options[:client]
|
|
||||||
@version = options[:version] || 'v1'
|
|
||||||
|
|
||||||
self.connection = options[:connection] || Faraday.default_connection
|
self.connection = options[:connection] || Faraday.default_connection
|
||||||
self.authorization = options[:authorization]
|
self.authorization = options[:authorization]
|
||||||
self.api_method = options[:api_method]
|
self.api_method = options[:api_method]
|
||||||
|
|
||||||
self.parameters = options[:parameters] || {}
|
self.parameters = options[:parameters] || {}
|
||||||
# These parameters are handled differently because they're not
|
# These parameters are handled differently because they're not
|
||||||
# parameters to the API method, but rather to the API system.
|
# parameters to the API method, but rather to the API system.
|
||||||
if self.parameters.kind_of?(Array)
|
|
||||||
if options[:key]
|
|
||||||
self.parameters.reject! { |k, _| k == 'key' }
|
|
||||||
self.parameters << ['key', options[:key]]
|
|
||||||
end
|
|
||||||
if options[:user_ip]
|
|
||||||
self.parameters.reject! { |k, _| k == 'userIp' }
|
|
||||||
self.parameters << ['userIp', options[:user_ip]]
|
|
||||||
end
|
|
||||||
elsif self.parameters.kind_of?(Hash)
|
|
||||||
self.parameters['key'] ||= options[:key] if options[:key]
|
self.parameters['key'] ||= options[:key] if options[:key]
|
||||||
self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
|
self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
|
||||||
# Convert to Array, because they're easier to work with when
|
|
||||||
# repeated parameters are an issue.
|
|
||||||
self.parameters = self.parameters.to_a
|
|
||||||
else
|
|
||||||
raise TypeError,
|
|
||||||
"Expected Array or Hash, got #{self.parameters.class}."
|
|
||||||
end
|
|
||||||
self.headers = options[:headers] || {}
|
self.headers = options[:headers] || {}
|
||||||
if options[:media]
|
if options[:media]
|
||||||
|
self.initialize_media_upload
|
||||||
|
elsif options[:body]
|
||||||
|
self.body = options[:body]
|
||||||
|
elsif options[:body_object]
|
||||||
|
self.headers['Content-Type'] ||= 'application/json'
|
||||||
|
self.body = serialize_body(options[:body_object])
|
||||||
|
else
|
||||||
|
self.body = ''
|
||||||
|
end
|
||||||
|
unless self.api_method
|
||||||
|
self.http_method = options[:http_method] || 'GET'
|
||||||
|
self.uri = options[:uri]
|
||||||
|
unless self.parameters.empty?
|
||||||
|
self.uri.query = Addressable::URI.form_encode(self.parameters)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
def initialize_media_upload
|
||||||
self.media = options[:media]
|
self.media = options[:media]
|
||||||
upload_type = self.parameters.find { |(k, _)| ['uploadType', 'upload_type'].include?(k) }.last
|
case self.upload_type
|
||||||
case upload_type
|
|
||||||
when "media"
|
when "media"
|
||||||
if options[:body] || options[:body_object]
|
if options[:body] || options[:body_object]
|
||||||
raise ArgumentError,
|
raise ArgumentError, "Can not specify body & body object for simple uploads"
|
||||||
"Can not specify body & body object for simple uploads."
|
|
||||||
end
|
end
|
||||||
self.headers['Content-Type'] ||= self.media.content_type
|
self.headers['Content-Type'] ||= self.media.content_type
|
||||||
self.body = self.media
|
self.body = self.media
|
||||||
when "multipart"
|
when "multipart"
|
||||||
unless options[:body_object]
|
unless options[:body_object]
|
||||||
raise ArgumentError, "Multipart requested but no body object."
|
raise ArgumentError, "Multipart requested but no body object"
|
||||||
end
|
end
|
||||||
# This is all a bit of a hack due to Signet requiring body to be a
|
|
||||||
# string. Ideally, update Signet to delay serialization so we can
|
|
||||||
# just pass streams all the way down through to the HTTP library.
|
|
||||||
metadata = StringIO.new(serialize_body(options[:body_object]))
|
metadata = StringIO.new(serialize_body(options[:body_object]))
|
||||||
env = {
|
env = {
|
||||||
:request_headers => {
|
:request_headers => {'Content-Type' => "multipart/related;boundary=#{MULTIPART_BOUNDARY}"},
|
||||||
'Content-Type' =>
|
|
||||||
"multipart/related;boundary=#{MULTIPART_BOUNDARY}"
|
|
||||||
},
|
|
||||||
:request => { :boundary => MULTIPART_BOUNDARY }
|
:request => { :boundary => MULTIPART_BOUNDARY }
|
||||||
}
|
}
|
||||||
multipart = Faraday::Request::Multipart.new
|
multipart = Faraday::Request::Multipart.new
|
||||||
self.body = multipart.create_multipart(env, [
|
self.body = multipart.create_multipart(env, [
|
||||||
[nil, Faraday::UploadIO.new(
|
[nil,Faraday::UploadIO.new(metadata, 'application/json', 'file.json')],
|
||||||
metadata, 'application/json', 'file.json'
|
|
||||||
)],
|
|
||||||
[nil, self.media]])
|
[nil, self.media]])
|
||||||
self.headers.update(env[:request_headers])
|
self.headers.update(env[:request_headers])
|
||||||
when "resumable"
|
when "resumable"
|
||||||
|
@ -105,26 +93,7 @@ module Google
|
||||||
self.body = ''
|
self.body = ''
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
raise ArgumentError, "Invalid uploadType for media."
|
raise ArgumentError, "Invalid uploadType for media"
|
||||||
end
|
|
||||||
elsif options[:body]
|
|
||||||
self.body = options[:body]
|
|
||||||
elsif options[:body_object]
|
|
||||||
self.headers['Content-Type'] ||= 'application/json'
|
|
||||||
self.body = serialize_body(options[:body_object])
|
|
||||||
else
|
|
||||||
self.body = ''
|
|
||||||
end
|
|
||||||
unless self.api_method
|
|
||||||
self.http_method = options[:http_method] || 'GET'
|
|
||||||
self.uri = options[:uri]
|
|
||||||
unless self.parameters.empty?
|
|
||||||
query_values = (self.uri.query_values(Array) || [])
|
|
||||||
self.uri.query = Addressable::URI.form_encode(
|
|
||||||
(query_values + self.parameters).sort
|
|
||||||
)
|
|
||||||
self.uri.query = nil if self.uri.query == ""
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -143,6 +112,10 @@ module Google
|
||||||
@media = (media)
|
@media = (media)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def upload_type
|
||||||
|
return self.parameters['uploadType'] || self.parameters['upload_type']
|
||||||
|
end
|
||||||
|
|
||||||
def authorization
|
def authorization
|
||||||
return @authorization
|
return @authorization
|
||||||
end
|
end
|
||||||
|
@ -172,29 +145,6 @@ module Google
|
||||||
if new_api_method.kind_of?(Google::APIClient::Method) ||
|
if new_api_method.kind_of?(Google::APIClient::Method) ||
|
||||||
new_api_method == nil
|
new_api_method == nil
|
||||||
@api_method = new_api_method
|
@api_method = new_api_method
|
||||||
elsif new_api_method.respond_to?(:to_str) ||
|
|
||||||
new_api_method.kind_of?(Symbol)
|
|
||||||
unless @client
|
|
||||||
raise ArgumentError,
|
|
||||||
"API method lookup impossible without client instance."
|
|
||||||
end
|
|
||||||
new_api_method = new_api_method.to_s
|
|
||||||
# This method of guessing the API is unreliable. This will fail for
|
|
||||||
# APIs where the first segment of the RPC name does not match the
|
|
||||||
# service name. However, this is a fallback mechanism anyway.
|
|
||||||
# Developers should be passing in a reference to the method, rather
|
|
||||||
# than passing in a string or symbol. This should raise an error
|
|
||||||
# in the case of a mismatch.
|
|
||||||
api = new_api_method[/^([^.]+)\./, 1]
|
|
||||||
@api_method = @client.discovered_method(
|
|
||||||
new_api_method, api, @version
|
|
||||||
)
|
|
||||||
if @api_method
|
|
||||||
# Ditch the client reference, we won't need it again.
|
|
||||||
@client = nil
|
|
||||||
else
|
|
||||||
raise ArgumentError, "API method could not be found."
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
raise TypeError,
|
raise TypeError,
|
||||||
"Expected Google::APIClient::Method, got #{new_api_method.class}."
|
"Expected Google::APIClient::Method, got #{new_api_method.class}."
|
||||||
|
@ -206,8 +156,7 @@ module Google
|
||||||
end
|
end
|
||||||
|
|
||||||
def parameters=(new_parameters)
|
def parameters=(new_parameters)
|
||||||
# No type-checking needed, the Method class handles this.
|
@parameters = Hash[new_parameters]
|
||||||
@parameters = new_parameters
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def body
|
def body
|
||||||
|
@ -215,19 +164,7 @@ module Google
|
||||||
end
|
end
|
||||||
|
|
||||||
def body=(new_body)
|
def body=(new_body)
|
||||||
if new_body.respond_to?(:to_str)
|
@body = new_body
|
||||||
@body = new_body.to_str
|
|
||||||
elsif new_body.respond_to?(:read)
|
|
||||||
@body = new_body.read()
|
|
||||||
elsif new_body.respond_to?(:inject)
|
|
||||||
@body = (new_body.inject(StringIO.new) do |accu, chunk|
|
|
||||||
accu.write(chunk)
|
|
||||||
accu
|
|
||||||
end).string
|
|
||||||
else
|
|
||||||
raise TypeError,
|
|
||||||
"Expected body to be String, IO, or Enumerable chunks."
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def headers
|
def headers
|
||||||
|
@ -248,9 +185,9 @@ module Google
|
||||||
|
|
||||||
def http_method=(new_http_method)
|
def http_method=(new_http_method)
|
||||||
if new_http_method.kind_of?(Symbol)
|
if new_http_method.kind_of?(Symbol)
|
||||||
@http_method = new_http_method.to_s.upcase
|
@http_method = new_http_method.to_s.downcase.to_sym
|
||||||
elsif new_http_method.respond_to?(:to_str)
|
elsif new_http_method.respond_to?(:to_str)
|
||||||
@http_method = new_http_method.to_str.upcase
|
@http_method = new_http_method.to_s.downcase.to_sym
|
||||||
else
|
else
|
||||||
raise TypeError,
|
raise TypeError,
|
||||||
"Expected String or Symbol, got #{new_http_method.class}."
|
"Expected String or Symbol, got #{new_http_method.class}."
|
||||||
|
@ -265,21 +202,27 @@ module Google
|
||||||
@uri = Addressable::URI.parse(new_uri)
|
@uri = Addressable::URI.parse(new_uri)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_request
|
def to_http_request
|
||||||
|
request = (
|
||||||
if self.api_method
|
if self.api_method
|
||||||
return self.api_method.generate_request(
|
self.api_method.generate_request(
|
||||||
self.parameters, self.body, self.headers,
|
self.parameters, self.body, self.headers, :connection => self.connection
|
||||||
:connection => self.connection
|
|
||||||
)
|
)
|
||||||
else
|
else
|
||||||
return self.connection.build_request(
|
self.connection.build_request(self.http_method) do |req|
|
||||||
self.http_method.to_s.downcase.to_sym
|
req.url(self.uri.to_str)
|
||||||
) do |req|
|
req.headers.update(self.headers)
|
||||||
req.url(Addressable::URI.parse(self.uri).normalize.to_s)
|
|
||||||
req.headers = Faraday::Utils::Headers.new(self.headers)
|
|
||||||
req.body = self.body
|
req.body = self.body
|
||||||
end
|
end
|
||||||
|
end)
|
||||||
|
|
||||||
|
if self.authorization.respond_to?(:generate_authenticated_request)
|
||||||
|
request = self.authorization.generate_authenticated_request(
|
||||||
|
:request => request,
|
||||||
|
:connection => self.connection
|
||||||
|
)
|
||||||
end
|
end
|
||||||
|
return request
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_hash
|
def to_hash
|
||||||
|
|
|
@ -18,16 +18,13 @@ module Google
|
||||||
##
|
##
|
||||||
# This class wraps a result returned by an API call.
|
# This class wraps a result returned by an API call.
|
||||||
class Result
|
class Result
|
||||||
def initialize(reference, request, response)
|
def initialize(reference, response)
|
||||||
@reference = reference
|
@reference = reference
|
||||||
@request = request
|
|
||||||
@response = response
|
@response = response
|
||||||
end
|
end
|
||||||
|
|
||||||
attr_reader :reference
|
attr_reader :reference
|
||||||
|
|
||||||
attr_reader :request
|
|
||||||
|
|
||||||
attr_reader :response
|
attr_reader :response
|
||||||
|
|
||||||
def status
|
def status
|
||||||
|
|
|
@ -91,7 +91,7 @@ describe Google::APIClient do
|
||||||
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
||||||
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
|
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
|
||||||
'?userIp=127.0.0.1'
|
'?userIp=127.0.0.1'
|
||||||
)
|
)
|
||||||
|
@ -104,7 +104,7 @@ describe Google::APIClient do
|
||||||
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
:uri => CLIENT.discovery_uri('prediction', 'v1.2'),
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
||||||
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
|
'https://www.googleapis.com/discovery/v1/apis/prediction/v1.2/rest' +
|
||||||
'?key=qwerty'
|
'?key=qwerty'
|
||||||
)
|
)
|
||||||
|
@ -119,7 +119,7 @@ describe Google::APIClient do
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
Addressable::URI.parse(
|
Addressable::URI.parse(
|
||||||
request.to_env(Faraday.default_connection)[:url]
|
request.to_http_request.to_env(Faraday.default_connection)[:url]
|
||||||
).query_values.should == {
|
).query_values.should == {
|
||||||
'key' => 'qwerty',
|
'key' => 'qwerty',
|
||||||
'userIp' => '127.0.0.1'
|
'userIp' => '127.0.0.1'
|
||||||
|
@ -183,8 +183,8 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => {'data' => '12345'}
|
:parameters => {'data' => '12345'}
|
||||||
)
|
)
|
||||||
request.method.should == :post
|
request.to_http_request.method.should == :post
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
||||||
request.headers.should be_empty
|
request.headers.should be_empty
|
||||||
request.body.should == ''
|
request.body.should == ''
|
||||||
|
@ -196,8 +196,8 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => [['data', '1'], ['data','2']]
|
:parameters => [['data', '1'], ['data','2']]
|
||||||
)
|
)
|
||||||
request.method.should == :post
|
request.to_http_request.method.should == :post
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
|
'https://www.googleapis.com/prediction/v1.2/training?data=1&data=2'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -206,7 +206,7 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => {'data' => '12345'}
|
:parameters => {'data' => '12345'}
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -215,7 +215,7 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => {'data' => '12345'}
|
:parameters => {'data' => '12345'}
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
'https://www.googleapis.com/prediction/v1.2/training?data=12345'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -229,7 +229,7 @@ describe Google::APIClient do
|
||||||
:api_method => prediction_rebase.training.insert,
|
:api_method => prediction_rebase.training.insert,
|
||||||
:parameters => {'data' => '123'}
|
:parameters => {'data' => '123'}
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
||||||
'https://testing-domain.example.com/' +
|
'https://testing-domain.example.com/' +
|
||||||
'prediction/v1.2/training?data=123'
|
'prediction/v1.2/training?data=123'
|
||||||
)
|
)
|
||||||
|
@ -243,8 +243,9 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => {'data' => '12345'}
|
:parameters => {'data' => '12345'}
|
||||||
)
|
)
|
||||||
request.headers.should have_key('Authorization')
|
http_request = request.to_http_request
|
||||||
request.headers['Authorization'].should =~ /^OAuth/
|
http_request.headers.should have_key('Authorization')
|
||||||
|
http_request.headers['Authorization'].should =~ /^OAuth/
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should generate OAuth 2 requests' do
|
it 'should generate OAuth 2 requests' do
|
||||||
|
@ -254,8 +255,9 @@ describe Google::APIClient do
|
||||||
:api_method => @prediction.training.insert,
|
:api_method => @prediction.training.insert,
|
||||||
:parameters => {'data' => '12345'}
|
:parameters => {'data' => '12345'}
|
||||||
)
|
)
|
||||||
request.headers.should have_key('Authorization')
|
http_request = request.to_http_request
|
||||||
request.headers['Authorization'].should =~ /^Bearer/
|
http_request.headers.should have_key('Authorization')
|
||||||
|
http_request.headers['Authorization'].should =~ /^Bearer/
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not be able to execute improperly authorized requests' do
|
it 'should not be able to execute improperly authorized requests' do
|
||||||
|
@ -311,7 +313,7 @@ describe Google::APIClient do
|
||||||
MultiJson.dump({"id" => "bucket/object"}),
|
MultiJson.dump({"id" => "bucket/object"}),
|
||||||
{'Content-Type' => 'application/json'}
|
{'Content-Type' => 'application/json'}
|
||||||
)
|
)
|
||||||
result.request.headers['Content-Type'].should == 'application/json'
|
result.reference.to_http_request.headers['Content-Type'].should == 'application/json'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -358,7 +360,7 @@ describe Google::APIClient do
|
||||||
},
|
},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should === (
|
||||||
'https://www.googleapis.com/plus/v1/' +
|
'https://www.googleapis.com/plus/v1/' +
|
||||||
'people/107807692475771887386/activities/public'
|
'people/107807692475771887386/activities/public'
|
||||||
)
|
)
|
||||||
|
@ -370,7 +372,7 @@ describe Google::APIClient do
|
||||||
:api_method => @plus.activities.list,
|
:api_method => @plus.activities.list,
|
||||||
:parameters => {'alt' => 'json'},
|
:parameters => {'alt' => 'json'},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should raise_error(ArgumentError)
|
end).should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -382,7 +384,7 @@ describe Google::APIClient do
|
||||||
'userId' => '107807692475771887386', 'collection' => 'bogus'
|
'userId' => '107807692475771887386', 'collection' => 'bogus'
|
||||||
},
|
},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should raise_error(ArgumentError)
|
end).should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
@ -429,7 +431,7 @@ describe Google::APIClient do
|
||||||
:api_method => 'latitude.currentLocation.get',
|
:api_method => 'latitude.currentLocation.get',
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/latitude/v1/currentLocation'
|
'https://www.googleapis.com/latitude/v1/currentLocation'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -438,7 +440,7 @@ describe Google::APIClient do
|
||||||
:api_method => @latitude.current_location.get,
|
:api_method => @latitude.current_location.get,
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/latitude/v1/currentLocation'
|
'https://www.googleapis.com/latitude/v1/currentLocation'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -493,7 +495,7 @@ describe Google::APIClient do
|
||||||
:api_method => 'moderator.profiles.get',
|
:api_method => 'moderator.profiles.get',
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/moderator/v1/profiles/@me'
|
'https://www.googleapis.com/moderator/v1/profiles/@me'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -502,7 +504,7 @@ describe Google::APIClient do
|
||||||
:api_method => @moderator.profiles.get,
|
:api_method => @moderator.profiles.get,
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/moderator/v1/profiles/@me'
|
'https://www.googleapis.com/moderator/v1/profiles/@me'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -553,7 +555,7 @@ describe Google::APIClient do
|
||||||
:api_method => 'adsense.adclients.list',
|
:api_method => 'adsense.adclients.list',
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/adsense/v1/adclients'
|
'https://www.googleapis.com/adsense/v1/adclients'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -562,7 +564,7 @@ describe Google::APIClient do
|
||||||
:api_method => @adsense.adclients.list,
|
:api_method => @adsense.adclients.list,
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
)
|
||||||
request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
request.to_http_request.to_env(Faraday.default_connection)[:url].to_s.should ===
|
||||||
'https://www.googleapis.com/adsense/v1/adclients'
|
'https://www.googleapis.com/adsense/v1/adclients'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -579,7 +581,7 @@ describe Google::APIClient do
|
||||||
CLIENT.generate_request(
|
CLIENT.generate_request(
|
||||||
:api_method => @adsense.reports.generate,
|
:api_method => @adsense.reports.generate,
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should raise_error(ArgumentError)
|
end).should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -594,7 +596,7 @@ describe Google::APIClient do
|
||||||
'metric' => 'PAGE_VIEWS'
|
'metric' => 'PAGE_VIEWS'
|
||||||
},
|
},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should_not raise_error
|
end).should_not raise_error
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -609,7 +611,7 @@ describe Google::APIClient do
|
||||||
'metric' => 'PAGE_VIEWS'
|
'metric' => 'PAGE_VIEWS'
|
||||||
},
|
},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should raise_error(ArgumentError)
|
end).should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -639,7 +641,7 @@ describe Google::APIClient do
|
||||||
'metric' => ['PAGE_VIEWS', 'CLICKS']
|
'metric' => ['PAGE_VIEWS', 'CLICKS']
|
||||||
},
|
},
|
||||||
:authenticated => false
|
:authenticated => false
|
||||||
)
|
).to_http_request
|
||||||
end).should raise_error(ArgumentError)
|
end).should raise_error(ArgumentError)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -32,7 +32,7 @@ describe Google::APIClient::Result do
|
||||||
'maxResults' => 20
|
'maxResults' => 20
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@request = @reference.to_request
|
@request = @reference.to_http_request
|
||||||
|
|
||||||
# Response stub
|
# Response stub
|
||||||
@response = stub("response")
|
@response = stub("response")
|
||||||
|
@ -66,7 +66,7 @@ describe Google::APIClient::Result do
|
||||||
}
|
}
|
||||||
END_OF_STRING
|
END_OF_STRING
|
||||||
)
|
)
|
||||||
@result = Google::APIClient::Result.new(@reference, @request, @response)
|
@result = Google::APIClient::Result.new(@reference, @response)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should indicate a successful response' do
|
it 'should indicate a successful response' do
|
||||||
|
@ -82,7 +82,7 @@ describe Google::APIClient::Result do
|
||||||
reference = @result.next_page
|
reference = @result.next_page
|
||||||
Hash[reference.parameters].should include('pageToken')
|
Hash[reference.parameters].should include('pageToken')
|
||||||
Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
|
Hash[reference.parameters]['pageToken'].should == 'NEXT+PAGE+TOKEN'
|
||||||
url = reference.to_request.to_env(Faraday.default_connection)[:url]
|
url = reference.to_http_request.to_env(Faraday.default_connection)[:url]
|
||||||
url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
|
url.to_s.should include('pageToken=NEXT%2BPAGE%2BTOKEN')
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -124,7 +124,7 @@ describe Google::APIClient::Result do
|
||||||
}
|
}
|
||||||
END_OF_STRING
|
END_OF_STRING
|
||||||
)
|
)
|
||||||
@result = Google::APIClient::Result.new(@reference, @request, @response)
|
@result = Google::APIClient::Result.new(@reference, @response)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should not return a next page token' do
|
it 'should not return a next page token' do
|
||||||
|
@ -170,7 +170,7 @@ describe Google::APIClient::Result do
|
||||||
END_OF_STRING
|
END_OF_STRING
|
||||||
)
|
)
|
||||||
@response.stub(:status).and_return(400)
|
@response.stub(:status).and_return(400)
|
||||||
@result = Google::APIClient::Result.new(@reference, @request, @response)
|
@result = Google::APIClient::Result.new(@reference, @response)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'should return error status correctly' do
|
it 'should return error status correctly' do
|
||||||
|
|
|
@ -42,9 +42,7 @@ shared_examples_for 'configurable user agent' do
|
||||||
|
|
||||||
it 'should transmit a User-Agent header when sending requests' do
|
it 'should transmit a User-Agent header when sending requests' do
|
||||||
client.user_agent = 'Custom User Agent/1.2.3'
|
client.user_agent = 'Custom User Agent/1.2.3'
|
||||||
request = Faraday::Request.new(:get) do |req|
|
|
||||||
req.url('http://www.google.com/')
|
|
||||||
end
|
|
||||||
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
stubs = Faraday::Adapter::Test::Stubs.new do |stub|
|
||||||
stub.get('/') do |env|
|
stub.get('/') do |env|
|
||||||
headers = env[:request_headers]
|
headers = env[:request_headers]
|
||||||
|
@ -56,6 +54,9 @@ shared_examples_for 'configurable user agent' do
|
||||||
connection = Faraday.new(:url => 'https://www.google.com') do |builder|
|
connection = Faraday.new(:url => 'https://www.google.com') do |builder|
|
||||||
builder.adapter(:test, stubs)
|
builder.adapter(:test, stubs)
|
||||||
end
|
end
|
||||||
|
request = connection.build_request(:get) do |req|
|
||||||
|
req.url('http://www.google.com/')
|
||||||
|
end
|
||||||
client.transmit(:request => request, :connection => connection)
|
client.transmit(:request => request, :connection => connection)
|
||||||
stubs.verify_stubbed_calls
|
stubs.verify_stubbed_calls
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue