Continue internal shuffling...

This commit is contained in:
Steven Bazyl committed 2012-10-10 14:28:45 -06:00
1 parent f9c1ab67ba
commit fc45135fcd
13 files changed
+503 -385

No files matched your search

+14 -14
View File
@@ -40,8 +40,7 @@ module Google
# Creates a new batch request.
#
# @param [Hash] options
# Set of options for this request, the only important one being
# :connection, which specifies an HTTP connection to use.
# Set of options for this request
# @param [Proc] block
# Callback for every call's response. Won't be called if a call defined
# a callback of its own.
@@ -67,7 +66,7 @@ module Google
# automatically be generated, avoiding collisions. If duplicate call IDs
# are provided, an error will be thrown.
#
# @param [Hash, Google::APIClient::Reference] call: the call to be added.
# @param [Hash, Google::APIClient::Request] call: the call to be added.
# @param [String] call_id: the ID to be used for this call. Must be unique
# @param [Proc] block: callback for this call's response.
#
@@ -90,7 +89,7 @@ module Google
# Processes the HTTP response to the batch request, issuing callbacks.
#
# @param [Faraday::Response] response: the HTTP response.
def process_response(response)
def process_http_response(response)
content_type = find_header('Content-Type', response.headers)
boundary = /.*boundary=(.+)/.match(content_type)[1]
parts = response.body.split(/--#{Regexp.escape(boundary)}/)
@@ -212,21 +211,22 @@ module Google
#
# @return [StringIO] The request as a string in application/http format.
def serialize_call(call_id, call)
http_request = call.to_http_request
body = "#{http_request.method.to_s.upcase} #{http_request.path} HTTP/1.1"
http_request.headers.each do |header, value|
body << "\r\n%s: %s" % [header, value]
call.api_client = self.api_client
method, uri, headers, body = call.to_http_request
request = "#{method.to_s.upcase} #{Addressable::URI.parse(uri).path} HTTP/1.1"
headers.each do |header, value|
request << "\r\n%s: %s" % [header, value]
end
if http_request.body
if body
# TODO - CompositeIO if body is a stream
body << "\r\n\r\n"
if http_request.body.respond_to?(:read)
body << http_request.body.read
request << "\r\n\r\n"
if body.respond_to?(:read)
request << body.read
else
body << http_request.body.to_s
request << body.to_s
end
end
Faraday::UploadIO.new(StringIO.new(body), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
Faraday::UploadIO.new(StringIO.new(request), 'application/http', 'ruby-api-request', 'Content-ID' => id_to_header(call_id))
end
##
+4 -11
View File
@@ -222,21 +222,14 @@ module Google
# The HTTP connection to use.
#
# @return [Array] The generated HTTP request.
def generate_request(parameters={}, body='', headers=[], options={})
options[:connection] ||= Faraday.default_connection
def generate_request(parameters={}, body='', headers={}, options={})
if !headers.kind_of?(Array) && !headers.kind_of?(Hash)
raise TypeError, "Expected Hash or Array, got #{headers.class}."
end
method = self.http_method
method = self.http_method.to_s.downcase.to_sym
uri = self.generate_uri(parameters)
headers = headers.to_a if headers.kind_of?(Hash)
return options[:connection].build_request(
method.to_s.downcase.to_sym
) do |req|
req.url(Addressable::URI.parse(uri).to_s)
req.headers = Faraday::Utils::Headers.new(headers)
req.body = body
end
headers = Faraday::Utils::Headers.new(headers)
return [method, uri, headers, body]
end
+1 -1
View File
@@ -131,7 +131,7 @@ module Google
#
# @param [Faraday::Response] r
# Result of a chunk upload or range query
def process_response(response)
def process_http_response(response)
case response.status
when 200...299
@complete = true
+103 -104
View File
@@ -26,23 +26,23 @@ module Google
class Request
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
attr_reader :connection, :parameters, :api_method, :headers
attr_accessor :media, :authorization, :body
attr_reader :parameters, :headers
attr_accessor :api_client, :connection, :api_method, :version ,:media, :authorization, :authenticated, :body
def initialize(options={})
self.connection = options[:connection] || Faraday.default_connection
self.authorization = options[:authorization]
self.api_method = options[:api_method]
@parameters = Hash[options[:parameters] || {}]
@headers = Faraday::Utils::Headers.new
self.api_client = options[:api_client]
self.headers.merge!(options[:headers]) unless options[:headers].nil?
self.api_method = options[:api_method]
self.version = options[:version]
self.authenticated = options[:authenticated]
self.authorization = options[:authorization]
# These parameters are handled differently because they're not
# parameters to the API method, but rather to the API system.
self.parameters['key'] ||= options[:key] if options[:key]
self.parameters['userIp'] ||= options[:user_ip] if options[:user_ip]
@headers = Faraday::Utils::Headers.new
self.headers.merge!(options[:headers]) if options[:headers]
if options[:media]
self.initialize_media_upload(options)
@@ -58,12 +58,102 @@ module Google
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 upload_type
return self.parameters['uploadType'] || self.parameters['upload_type']
end
def http_method
return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
end
def http_method=(new_http_method)
if new_http_method.kind_of?(Symbol)
@http_method = new_http_method.to_s.downcase.to_sym
elsif new_http_method.respond_to?(:to_str)
@http_method = new_http_method.to_s.downcase.to_sym
else
raise TypeError,
"Expected String or Symbol, got #{new_http_method.class}."
end
end
def uri
return @uri ||= self.api_method.generate_uri(self.parameters)
end
def uri=(new_uri)
@uri = Addressable::URI.parse(new_uri)
@parameters.update(@uri.query_values) unless @uri.query_values.nil?
end
def send(connection)
response = connection.app.call(self.to_env(connection))
self.process_http_response(response)
end
def to_http_request
if self.api_client
self.headers['User-Agent'] ||= '' + self.api_client.user_agent unless self.api_client.user_agent.nil?
self.parameters['key'] ||= self.api_client.key unless self.api_client.key.nil?
self.parameters['userIp'] ||= self.api_client.user_ip unless self.api_client.user_ip.nil?
self.api_method = self.api_client.resolve_method(self.api_method, self.version) unless self.api_method.nil?
end
request = (
if self.uri
unless self.parameters.empty?
self.uri.query = Addressable::URI.form_encode(self.parameters)
end
[self.http_method, self.uri.to_s, self.headers, self.body]
else
self.api_method.generate_request(self.parameters, self.body, self.headers)
end)
end
def to_hash
options = {}
if self.api_method
options[:api_method] = self.api_method
options[:parameters] = self.parameters
else
options[:http_method] = self.http_method
options[:uri] = self.uri
end
options[:headers] = self.headers
options[:body] = self.body
options[:media] = self.media
unless self.authorization.nil?
options[:authorization] = self.authorization
end
return options
end
def to_env(connection)
method, uri, headers, body = self.to_http_request
http_request = connection.build_request(method) do |req|
req.url(uri)
req.headers.update(headers)
req.body = body
end
if self.authorization.respond_to?(:generate_authenticated_request)
http_request = self.authorization.generate_authenticated_request(
:request => http_request,
:connection => connection
)
end
request_env = http_request.to_env(connection)
end
def process_http_response(response)
Result.new(self, response)
end
protected
def initialize_media_upload(options)
self.media = options[:media]
case self.upload_type
@@ -109,97 +199,6 @@ module Google
'Must respond to :to_json or :to_hash.'
end
def upload_type
return self.parameters['uploadType'] || self.parameters['upload_type']
end
def connection=(new_connection)
if new_connection.kind_of?(Faraday::Connection)
@connection = new_connection
else
raise TypeError,
"Expected Faraday::Connection, got #{new_connection.class}."
end
end
def api_method=(new_api_method)
if new_api_method.kind_of?(Google::APIClient::Method) ||
new_api_method == nil
@api_method = new_api_method
else
raise TypeError,
"Expected Google::APIClient::Method, got #{new_api_method.class}."
end
end
def http_method
return @http_method ||= self.api_method.http_method.to_s.downcase.to_sym
end
def http_method=(new_http_method)
if new_http_method.kind_of?(Symbol)
@http_method = new_http_method.to_s.downcase.to_sym
elsif new_http_method.respond_to?(:to_str)
@http_method = new_http_method.to_s.downcase.to_sym
else
raise TypeError,
"Expected String or Symbol, got #{new_http_method.class}."
end
end
def uri
return @uri ||= self.api_method.generate_uri(self.parameters)
end
def uri=(new_uri)
@uri = Addressable::URI.parse(new_uri)
end
def to_http_request
request = (
if self.uri
self.connection.build_request(self.http_method) do |req|
req.url(self.uri.to_str)
req.headers.update(self.headers)
req.body = self.body
end
else
self.api_method.generate_request(
self.parameters, self.body, self.headers, :connection => self.connection
)
end)
if self.authorization.respond_to?(:generate_authenticated_request)
request = self.authorization.generate_authenticated_request(
:request => request,
:connection => self.connection
)
end
return request
end
def to_hash
options = {}
if self.api_method
options[:api_method] = self.api_method
options[:parameters] = self.parameters
else
options[:http_method] = self.http_method
options[:uri] = self.uri
end
options[:headers] = self.headers
options[:body] = self.body
options[:connection] = self.connection
options[:media] = self.media
unless self.authorization.nil?
options[:authorization] = self.authorization
end
return options
end
def process_response(response)
Result.new(self, response)
end
end
class Reference < Request