From 4682267161c434a9fa4645cebe04b6b194605c00 Mon Sep 17 00:00:00 2001 From: Steve Bazyl Date: Mon, 3 Apr 2017 11:10:54 -0700 Subject: [PATCH] Move timeouts from request to clientoptions, update migration/changelog --- CHANGELOG.md | 7 +++--- MIGRATING.md | 33 ++++++++++++++++++++++++++++ dl.rb | 0 lib/google/apis/core/base_service.rb | 19 ++++++++-------- lib/google/apis/core/download.rb | 8 ++++--- lib/google/apis/core/upload.rb | 14 ++++++++---- lib/google/apis/options.rb | 23 +++++++++---------- 7 files changed, 74 insertions(+), 30 deletions(-) create mode 100644 dl.rb diff --git a/CHANGELOG.md b/CHANGELOG.md index e68f6d94f..d90919c68 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,10 @@ # 0.11.0 -* *Breaking Change* - Fix handling of large numbers during code generation. Previously the +* *Breaking change* - Fix handling of large numbers during code generation. Previously the uint64/int64 formats were passed through as strings. They are now coerced to/from Fixnum/Bignum types -* *Breaking Change* - No longer normalize unicode strings in URI templates. Mostly affects +* *Breaking change* - No longer normalize unicode strings in URI templates. Mostly affects Cloud Storage, but other APIs with unicode strings in paths may be affected. Old behavior can be restored using the `normalize_unicode` request option. +* *Breaking change* -- Moved timeout options from `RequestOptions` to `ClientOptions` * Remove Hurley as dependency. May cause minor breaking changes if directly accessing the underlying client connection. * Drop compatibility with Rails 3.x since that is no longer supported by the Rails community. @@ -17,7 +18,7 @@ # 0.10.3 * Regenerate APIs * Enable additional API: - * `acceleratedmobilepageurl:v1`` + * `acceleratedmobilepageurl:v1` * `appengine:v1` * `clouderrorreporting:v1beta1` * `cloudfunctions:v1` diff --git a/MIGRATING.md b/MIGRATING.md index b0f99b2f4..96a5b5b9a 100644 --- a/MIGRATING.md +++ b/MIGRATING.md @@ -1,3 +1,36 @@ +# Migrating from version`0.10` to `0.11` + +## Unicode normalization + +The client no longer normalizes unicode strings in path parameters. This may affect +some applications using multibyte strings that were previously normalized.: +To restore the previous behavior, set the following option prior to creating a service. + +```ruby +ClientOptions.default.normalize_unicode = true +``` + +## Type change for large numbers + +Previously, types declared as 64 bit numbers were mapped to strings. These are now mapped to +`Fixednum`/`Bignum`. + +## Timeouts + +Timeout options have been moved from `RequestOptions` to `ClientOptions`. + +Old | New +----------------------------------|----------------- +`RequestOptions.open_timeout_sec` | `ClentOptions.open_timeout_sec` +`RequestOptions.timeout_sec` | `ClentOptions.read_timeout_sec` +`RequestOptions.timeout_sec` | `ClentOptions.send_timeout_sec` + +## Batch requests across services no longer supported + +It is no longer possible to combine multiple services (e.g. Gail & Drive) +in a batch request. If batching requests that span services, group +requests for each service in their own batch request. + # Migrating from version `0.9.x` to `0.10` Only one minor breaking change was introduced in the `to_json` method due to a version bump for the `representable` gem from `2.3` to `3.0`. If you used the `skip_undefined` in `to_json`, you should replace that with `user_options: { skip_undefined: true }`. diff --git a/dl.rb b/dl.rb new file mode 100644 index 000000000..e69de29bb diff --git a/lib/google/apis/core/base_service.rb b/lib/google/apis/core/base_service.rb index 81799bced..e7b9e806b 100644 --- a/lib/google/apis/core/base_service.rb +++ b/lib/google/apis/core/base_service.rb @@ -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 diff --git a/lib/google/apis/core/download.rb b/lib/google/apis/core/download.rb index 3cc23311b..de4f6bf9f 100644 --- a/lib/google/apis/core/download.rb +++ b/lib/google/apis/core/download.rb @@ -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 diff --git a/lib/google/apis/core/upload.rb b/lib/google/apis/core/upload.rb index 6aa88ec41..7ad5fed97 100644 --- a/lib/google/apis/core/upload.rb +++ b/lib/google/apis/core/upload.rb @@ -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 diff --git a/lib/google/apis/options.rb b/lib/google/apis/options.rb index 1287fef6d..83f6def9e 100644 --- a/lib/google/apis/options.rb +++ b/lib/google/apis/options.rb @@ -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