diff --git a/google-apis-core/OVERVIEW.md b/google-apis-core/OVERVIEW.md index d3a9f477d3f..4e164630e0a 100644 --- a/google-apis-core/OVERVIEW.md +++ b/google-apis-core/OVERVIEW.md @@ -22,6 +22,14 @@ should pin `google-apis-core` to a 0.x version in your Gemfile. For example: gem "google-apis-core", "~> 0.18" +The `faraday` integration ships with, and only tests against the default adapter, which wraps [net-http](https://github.com/ruby/net-http). You can change the adapter by setting the `:adapter` client option: + +```ruby +Google::Apis::ClientOptions.default.adapter = :net_http_persistent # or :httpx, or... +``` + +There a **caveat** though: downloading media requires a `faraday` adapter which [implements the `on_data` streaming API](https://lostisland.github.io/faraday/#/advanced/streaming-responses?id=streaming-responses). + ## Documentation More detailed descriptions of the Google legacy REST clients are available in two documents. diff --git a/google-apis-core/lib/google/apis/core/base_service.rb b/google-apis-core/lib/google/apis/core/base_service.rb index 5e68bd014b8..7a57cde05ac 100644 --- a/google-apis-core/lib/google/apis/core/base_service.rb +++ b/google-apis-core/lib/google/apis/core/base_service.rb @@ -567,6 +567,7 @@ def new_client Faraday.new options do |faraday| faraday.response :logger, Google::Apis.logger if client_options.log_http_requests faraday.response :follow_redirects_google_apis_core, limit: 5 + faraday.adapter client_options.adapter if client_options.adapter end end diff --git a/google-apis-core/lib/google/apis/options.rb b/google-apis-core/lib/google/apis/options.rb index 4445f7e90c4..4233c3aa7f7 100644 --- a/google-apis-core/lib/google/apis/options.rb +++ b/google-apis-core/lib/google/apis/options.rb @@ -23,7 +23,8 @@ module Apis :read_timeout_sec, :send_timeout_sec, :log_http_requests, - :transparent_gzip_decompression) + :transparent_gzip_decompression, + :adapter) RequestOptions = Struct.new( :authorization, @@ -50,6 +51,8 @@ class ClientOptions # @return [String] Name of the application, for identification in the User-Agent header # @!attribute [rw] application_version # @return [String] Version of the application, for identification in the User-Agent header + # @!attribute [rw] adapter + # @return [Symbol, Class] Faraday adapter to use (https://lostisland.github.io/faraday/#/adapters/index?id=adapters) # @!attribute [rw] proxy_url # @return [String] URL of a proxy server # @!attribute [rw] log_http_requests @@ -82,7 +85,7 @@ class RequestOptions # @!attribute [rw] max_interval # @return [Integer] The maximum interval in seconds that any individual retry can reach. # @!attribute [rw] multiplier - # @return [Numeric] Each successive interval grows by this factor. A multipler of 1.5 means the next interval + # @return [Numeric] Each successive interval grows by this factor. A multipler of 1.5 means the next interval # will be 1.5x the current interval. # @!attribute [rw] header # @return [Hash] Additional HTTP headers to include in requests. diff --git a/google-apis-core/spec/google/apis/core/service_spec.rb b/google-apis-core/spec/google/apis/core/service_spec.rb index 4a95115fb2b..fb082c5a11a 100644 --- a/google-apis-core/spec/google/apis/core/service_spec.rb +++ b/google-apis-core/spec/google/apis/core/service_spec.rb @@ -114,6 +114,18 @@ end end + context 'with adapter' do + after(:example) do + Google::Apis::ClientOptions.default.adapter = nil + end + + it 'should allow switching the adapter' do + Google::Apis::ClientOptions.default.adapter = Faraday::Adapter::Test + expect(service.client).to be_a(Faraday::Connection) + expect(service.client.builder.adapter).to eq(Faraday::Adapter::Test) + end + end + context 'when making simple commands' do let(:command) { service.send(:make_simple_command, :get, 'zoo/animals', authorization: 'foo') }