From a94ad37738e2e87762214666df8a609c4ca47b88 Mon Sep 17 00:00:00 2001 From: jethrodaniel Date: Thu, 2 Feb 2023 19:59:52 -0600 Subject: [PATCH] [Ruby] Allow general purpose faraday connection configuration (#14423) * allow general purpose faraday connection configuration * add spec for Configuration#configure_faraday_connection * run generators * limit changes to ruby client generated using faraday library --- .../resources/ruby-client/README.mustache | 4 ++ .../api_client_faraday_partial.mustache | 1 + .../ruby-client/configuration.mustache | 27 ++++++++++++ .../ruby-client/configuration_spec.mustache | 42 +++++++++++++++++++ .../ruby-faraday/lib/petstore/api_client.rb | 1 + .../lib/petstore/configuration.rb | 27 ++++++++++++ .../ruby-faraday/spec/configuration_spec.rb | 40 ++++++++++++++++++ 7 files changed, 142 insertions(+) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/README.mustache b/modules/openapi-generator/src/main/resources/ruby-client/README.mustache index 0445ee3d959..7ee777216f4 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/README.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/README.mustache @@ -81,6 +81,10 @@ require '{{{gemName}}}' config.access_token = 'YOUR ACCESS TOKEN' # Configure a proc to get access tokens in lieu of the static access_token configuration config.access_token_getter = -> { 'YOUR TOKEN GETTER PROC' } {{/isOAuth}} + {{#isFaraday}} + # Configure faraday connection + config.configure_faraday_connection { |connection| 'YOUR CONNECTION CONFIG PROC' } + {{/isFaraday}} {{/authMethods}}end {{/hasAuthMethods}} diff --git a/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache b/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache index 8327a3492fa..6929ad0496f 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/api_client_faraday_partial.mustache @@ -138,6 +138,7 @@ config.configure_middleware(conn) yield(conn) if block_given? conn.adapter(Faraday.default_adapter) + config.configure_connection(conn) end end diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache index 5439dd2fa80..d63f0c429f3 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration.mustache @@ -117,6 +117,7 @@ module {{moduleName}} @ssl_client_cert = nil @ssl_client_key = nil @middlewares = Hash.new { |h, k| h[k] = [] } + @configure_connection_blocks = [] @timeout = 60 # return data as binary instead of file @return_binary_data = false @@ -357,6 +358,32 @@ module {{moduleName}} end {{#isFaraday}} + # Configure Faraday connection directly. + # + # ``` + # c.configure_faraday_connection do |conn| + # conn.use Faraday::HttpCache, shared_cache: false, logger: logger + # conn.response :logger, nil, headers: true, bodies: true, log_level: :debug do |logger| + # logger.filter(/(Authorization: )(.*)/, '\1[REDACTED]') + # end + # end + # + # c.configure_faraday_connection do |conn| + # conn.adapter :typhoeus + # end + # ``` + # + # @param block [Proc] `#call`able object that takes one arg, the connection + def configure_faraday_connection(&block) + @configure_connection_blocks << block + end + + def configure_connection(conn) + @configure_connection_blocks.each do |block| + block.call(conn) + end + end + # Adds middleware to the stack def use(*middleware) set_faraday_middleware(:use, *middleware) diff --git a/modules/openapi-generator/src/main/resources/ruby-client/configuration_spec.mustache b/modules/openapi-generator/src/main/resources/ruby-client/configuration_spec.mustache index 22a113e8e32..fa1006253f1 100644 --- a/modules/openapi-generator/src/main/resources/ruby-client/configuration_spec.mustache +++ b/modules/openapi-generator/src/main/resources/ruby-client/configuration_spec.mustache @@ -31,4 +31,46 @@ describe {{moduleName}}::Configuration do end end end +{{#isFaraday}} + + describe '#configure_faraday_connection' do + let(:faraday_connection) { Faraday::Connection.new } + + before do + stub_const('CustomAdapter', Class.new(Faraday::Adapter)) + stub_const('AnotherCustomAdapter', Class.new(Faraday::Adapter)) + + config.configure_faraday_connection do |conn| + conn.adapter CustomAdapter + conn.response :logger, nil, headers: true, bodies: true, log_level: :debug do |logger| + logger.filter(/(Authorization: )(.*)/, '\1[REDACTED]') + end + end + end + + it 'adds a block that will be used to configure the connection' do + expect(faraday_connection.adapter).to eq(Faraday::Adapter::NetHttp) + expect(faraday_connection.builder.handlers).to_not include(Faraday::Response::Logger) + + config.configure_connection(faraday_connection) + + expect(faraday_connection.adapter).to eq(CustomAdapter) + expect(faraday_connection.builder.handlers).to include(Faraday::Response::Logger) + end + + it 'supports multiple configuration blocks' do + config.configure_faraday_connection do |conn| + conn.adapter AnotherCustomAdapter + end + + expect(faraday_connection.adapter).to eq(Faraday::Adapter::NetHttp) + expect(faraday_connection.builder.handlers).to_not include(Faraday::Response::Logger) + + config.configure_connection(faraday_connection) + + expect(faraday_connection.adapter).to eq(AnotherCustomAdapter) + expect(faraday_connection.builder.handlers).to include(Faraday::Response::Logger) + end + end +{{/isFaraday}} end diff --git a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb index d5059e611c9..c193a0b1226 100644 --- a/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb +++ b/samples/client/petstore/ruby-faraday/lib/petstore/api_client.rb @@ -183,6 +183,7 @@ module Petstore config.configure_middleware(conn) yield(conn) if block_given? conn.adapter(Faraday.default_adapter) + config.configure_connection(conn) end end diff --git a/samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb b/samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb index fa87bd5e605..fd790900bdf 100644 --- a/samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb +++ b/samples/client/petstore/ruby-faraday/lib/petstore/configuration.rb @@ -159,6 +159,7 @@ module Petstore @ssl_client_cert = nil @ssl_client_key = nil @middlewares = Hash.new { |h, k| h[k] = [] } + @configure_connection_blocks = [] @timeout = 60 # return data as binary instead of file @return_binary_data = false @@ -421,6 +422,32 @@ module Petstore url end + # Configure Faraday connection directly. + # + # ``` + # c.configure_faraday_connection do |conn| + # conn.use Faraday::HttpCache, shared_cache: false, logger: logger + # conn.response :logger, nil, headers: true, bodies: true, log_level: :debug do |logger| + # logger.filter(/(Authorization: )(.*)/, '\1[REDACTED]') + # end + # end + # + # c.configure_faraday_connection do |conn| + # conn.adapter :typhoeus + # end + # ``` + # + # @param block [Proc] `#call`able object that takes one arg, the connection + def configure_faraday_connection(&block) + @configure_connection_blocks << block + end + + def configure_connection(conn) + @configure_connection_blocks.each do |block| + block.call(conn) + end + end + # Adds middleware to the stack def use(*middleware) set_faraday_middleware(:use, *middleware) diff --git a/samples/client/petstore/ruby-faraday/spec/configuration_spec.rb b/samples/client/petstore/ruby-faraday/spec/configuration_spec.rb index c5a3d622b16..9dbaad916cb 100644 --- a/samples/client/petstore/ruby-faraday/spec/configuration_spec.rb +++ b/samples/client/petstore/ruby-faraday/spec/configuration_spec.rb @@ -39,4 +39,44 @@ describe Petstore::Configuration do end end end + + describe '#configure_faraday_connection' do + let(:faraday_connection) { Faraday::Connection.new } + + before do + stub_const('CustomAdapter', Class.new(Faraday::Adapter)) + stub_const('AnotherCustomAdapter', Class.new(Faraday::Adapter)) + + config.configure_faraday_connection do |conn| + conn.adapter CustomAdapter + conn.response :logger, nil, headers: true, bodies: true, log_level: :debug do |logger| + logger.filter(/(Authorization: )(.*)/, '\1[REDACTED]') + end + end + end + + it 'adds a block that will be used to configure the connection' do + expect(faraday_connection.adapter).to eq(Faraday::Adapter::NetHttp) + expect(faraday_connection.builder.handlers).to_not include(Faraday::Response::Logger) + + config.configure_connection(faraday_connection) + + expect(faraday_connection.adapter).to eq(CustomAdapter) + expect(faraday_connection.builder.handlers).to include(Faraday::Response::Logger) + end + + it 'supports multiple configuration blocks' do + config.configure_faraday_connection do |conn| + conn.adapter AnotherCustomAdapter + end + + expect(faraday_connection.adapter).to eq(Faraday::Adapter::NetHttp) + expect(faraday_connection.builder.handlers).to_not include(Faraday::Response::Logger) + + config.configure_connection(faraday_connection) + + expect(faraday_connection.adapter).to eq(AnotherCustomAdapter) + expect(faraday_connection.builder.handlers).to include(Faraday::Response::Logger) + end + end end