[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
This commit is contained in:
jethrodaniel 2023-02-02 19:59:52 -06:00 committed by GitHub
parent f6be077efb
commit a94ad37738
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 142 additions and 0 deletions

View File

@ -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}}

View File

@ -138,6 +138,7 @@
config.configure_middleware(conn)
yield(conn) if block_given?
conn.adapter(Faraday.default_adapter)
config.configure_connection(conn)
end
end

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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