# limitations under the License.
require 'faraday'
-require 'faraday/utils'
-require 'multi_json'
+require 'faraday/request/multipart'
require 'compat/multi_json'
require 'addressable/uri'
require 'stringio'
# Represents an API request.
class Request
include Google::APIClient::Logging
-
+
MULTIPART_BOUNDARY = "-----------RubyApiMultipartPost".freeze
# @return [Hash] Request parameters
# @option options [String, Symbol] :http_method
# HTTP method when requesting a URI
def initialize(options={})
- @parameters = Hash[options[:parameters] || {}]
+ @parameters = Faraday::Utils::ParamsHash.new
@headers = Faraday::Utils::Headers.new
+
+ self.parameters.merge!(options[:parameters]) unless options[:parameters].nil?
self.headers.merge!(options[:headers]) unless options[:headers].nil?
self.api_method = options[:api_method]
self.authenticated = options[:authenticated]
#
# @param [Faraday::Connection] connection
# the connection to transmit with
+ # @param [TrueValue,FalseValue] is_retry
+ # True if request has been previous sent
#
# @return [Google::APIClient::Result]
# result of API request
- def send(connection)
+ def send(connection, is_retry = false)
+ self.body.rewind if is_retry && self.body.respond_to?(:rewind)
env = self.to_env(connection)
logger.debug { "#{self.class} Sending API request #{env[:method]} #{env[:url].to_s} #{env[:request_headers]}" }
http_response = connection.app.call(env)
# Resumamble slightly different than other upload protocols in that it requires at least
# 2 requests.
- if self.upload_type == 'resumable'
- upload = result.resumable_upload
+ if result.status == 200 && self.upload_type == 'resumable' && self.media
+ upload = result.resumable_upload
unless upload.complete?
logger.debug { "#{self.class} Sending upload body" }
result = upload.send(connection)
)
end
- request_env = http_request.to_env(connection)
+ http_request.to_env(connection)
end
##
raise ArgumentError, "Can not specify body & body object for simple uploads"
end
self.headers['Content-Type'] ||= self.media.content_type
+ self.headers['Content-Length'] ||= self.media.length.to_s
self.body = self.media
when "multipart"
unless options[:body_object]
# @param [String] boundary
# Boundary for separating each part of the message
def build_multipart(parts, mime_type = 'multipart/related', boundary = MULTIPART_BOUNDARY)
- env = {
- :request_headers => {'Content-Type' => "#{mime_type};boundary=#{boundary}"},
- :request => { :boundary => boundary }
- }
+ env = Faraday::Env.new
+ env.request = Faraday::RequestOptions.new
+ env.request.boundary = boundary
+ env.request_headers = {'Content-Type' => "#{mime_type};boundary=#{boundary}"}
multipart = Faraday::Request::Multipart.new
self.body = multipart.create_multipart(env, parts.map {|part| [nil, part]})
self.headers.update(env[:request_headers])