Move timeouts from request to clientoptions, update migration/changelog

This commit is contained in:
Steve Bazyl committed 2017-04-03 11:10:54 -07:00
1 parent 97ff7f4d9a
commit 4682267161
7 files changed
+74 -30

No files matched your search

+10 -9
View File
@@ -380,23 +380,24 @@ module Google
client = ::HTTPClient.new
client.transparent_gzip_decompression = true
client.proxy = client_options.proxy_url if client_options.proxy_url
if request_options.timeout_sec
client.connect_timeout = request_options.timeout_sec
client.receive_timeout = request_options.timeout_sec
client.send_timeout = request_options.timeout_sec
if client_options.open_timeout_sec
client.connect_timeout = client_options.open_timeout_sec
end
if request_options.open_timeout_sec
client.connect_timeout = request_options.open_timeout_sec
client.send_timeout = request_options.open_timeout_sec
if client_options.read_timeout_sec
client.receive_timeout = client_options.read_timeout_sec
end
if client_options.send_timeout_sec
client.send_timeout = client_options.send_timeout_sec
end
client.follow_redirect_count = 5
client.default_header = { 'User-Agent' => user_agent }
client.debug_dev = logger if logger.level == Logger::DEBUG
client.debug_dev = logger if client_options.log_http_requests
client
end
+5 -3
View File
@@ -87,13 +87,14 @@ module Google
@download_io.rewind
check_if_rewind_needed = false
end
logger.debug { sprintf('Writing chunk (%d bytes)', chunk.length) }
@offset += chunk.length
# logger.debug { sprintf('Writing chunk (%d bytes, %d total)', chunk.length, bytes_read) }
@download_io.write(chunk)
@download_io.flush
@offset += chunk.length
end
end
@download_io.flush
if @close_io_on_finish
result = nil
else
@@ -102,6 +103,7 @@ module Google
check_status(http_res.status.to_i, http_res.header, http_res.body)
success(result, &block)
rescue => e
@download_io.flush
error(e, rethrow: true, &block)
end
end
+10 -4
View File
@@ -29,6 +29,7 @@ module Google
UPLOAD_PROTOCOL_HEADER = 'X-Goog-Upload-Protocol'
UPLOAD_CONTENT_TYPE_HEADER = 'X-Goog-Upload-Header-Content-Type'
UPLOAD_CONTENT_LENGTH = 'X-Goog-Upload-Header-Content-Length'
CONTENT_TYPE_HEADER = 'Content-Type'
# File name or IO containing the content to upload
# @return [String, File, #read]
@@ -51,16 +52,20 @@ module Google
if streamable?(upload_source)
self.upload_io = upload_source
@close_io_on_finish = false
elsif upload_source.is_a?(String)
elsif self.upload_source.is_a?(String)
self.upload_io = File.new(upload_source, 'r')
if upload_content_type.nil?
if self.upload_content_type.nil?
type = MIME::Types.of(upload_source)
upload_content_type = type.first.content_type unless type.nil? || type.empty?
self.upload_content_type = type.first.content_type unless type.nil? || type.empty?
end
@close_io_on_finish = true
else
fail Google::Apis::ClientError, 'Invalid upload source'
end
if self.upload_content_type.nil? || self.upload_content_type.empty?
self.upload_content_type = 'application/octet-stream'
end
puts self.upload_content_type.inspect
end
# Close IO stream when command done. Only closes the stream if it was opened by the command.
@@ -198,7 +203,7 @@ module Google
apply_request_options(request_header)
request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND
client.post(@upload_url, header: request_header, follow_redirect: true)
client.post(@upload_url, body: '', header: request_header, follow_redirect: true)
end
@@ -219,6 +224,7 @@ module Google
request_header[UPLOAD_COMMAND_HEADER] = QUERY_COMMAND
request_header[UPLOAD_COMMAND_HEADER] = UPLOAD_COMMAND
request_header[UPLOAD_OFFSET_HEADER] = @offset.to_s
request_header[CONTENT_TYPE_HEADER] = upload_content_type
client.post(@upload_url, body: content, header: request_header, follow_redirect: true)
end
+12 -11
View File
@@ -15,18 +15,19 @@
module Google
module Apis
# General options for API requests
ClientOptions = Struct.new(
ClientOptions = Struct.new(
:application_name,
:application_version,
:proxy_url,
:use_net_http)
:open_timeout_sec,
:read_timeout_sec,
:send_timeout_sec,
:log_http_requests)
RequestOptions = Struct.new(
:authorization,
:retries,
:header,
:timeout_sec,
:open_timeout_sec,
:normalize_unicode,
:skip_serialization,
:skip_deserialization)
@@ -39,7 +40,12 @@ module Google
# @return [String] Version of the application, for identification in the User-Agent header
# @!attribute [rw] proxy_url
# @return [String] URL of a proxy server
# @!attribute [rw] log_http_requests
# @return [Boolean] True if raw HTTP requests should be logged
# @!attribute [rw] open_timeout_sec
# @return [Fixnum] How long, in seconds, before failed connections time out
# @!attribute [rw] read_timeout_sec
# @return [Fixnum] How long, in seconds, before requests time out
# Get the default options
# @return [Google::Apis::ClientOptions]
def self.default
@@ -53,10 +59,6 @@ module Google
# @return [Signet::OAuth2::Client, #apply(Hash)] OAuth2 credentials
# @!attribute [rw] retries
# @return [Fixnum] Number of times to retry requests on server error
# @!attribute [rw] timeout_sec
# @return [Fixnum] How long, in seconds, before requests time out
# @!attribute [rw] open_timeout_sec
# @return [Fixnum] How long, in seconds, before failed connections time out
# @!attribute [rw] header
# @return [Hash<String,String] Additional HTTP headers to include in requests
# @!attribute [rw] normalize_unicode
@@ -84,11 +86,10 @@ module Google
end
end
ClientOptions.default.use_net_http = false
ClientOptions.default.log_http_requests = false
ClientOptions.default.application_name = 'unknown'
ClientOptions.default.application_version = '0.0.0'
RequestOptions.default.retries = 0
RequestOptions.default.open_timeout_sec = 20
RequestOptions.default.normalize_unicode = false
RequestOptions.default.skip_serialization = false
RequestOptions.default.skip_deserialization = false