Remove default Faraday.default_adapter value and add exception message with link to usage documentation. (#1354)

This commit is contained in:
Matt 2021-12-30 15:38:49 +00:00 committed by GitHub
parent 1c3e27616d
commit c9b8490bb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 28 additions and 27 deletions

View File

@ -87,6 +87,7 @@ For more details, see https://github.com/lostisland/faraday/pull/1306
* Drop `Faraday::UploadIO` in favour of `Faraday::FilePart`.
* `Faraday.default_connection_options` will now be deep-merged into new connections to avoid overriding them (e.g. headers).
* Retry middleware `retry_block` is not called if retry will not happen due to `max_interval`. (#1350)
* `Faraday::Builder#build` method is not exposed through `Faraday::Connection` anymore and does not reset the handlers if called multiple times. This method should be used internally only.
## Faraday 1.0

View File

@ -150,5 +150,4 @@ module Faraday
self.ignore_env_proxy = false
self.root_path = File.expand_path __dir__
self.lib_path = File.expand_path 'faraday', __dir__
self.default_adapter = :test
end

View File

@ -117,7 +117,7 @@ module Faraday
extend Forwardable
def_delegators :builder, :build, :use, :request, :response, :adapter, :app
def_delegators :builder, :use, :request, :response, :adapter, :app
# Closes the underlying resources and/or connections. In the case of
# persistent connections, this closes all currently open connections

View File

@ -58,22 +58,21 @@ module Faraday
end
end
def initialize(handlers = [], adapter = nil, &block)
@adapter = adapter
@handlers = handlers
if block
build(&block)
elsif @handlers.empty?
# default stack, if nothing else is configured
request :url_encoded
self.adapter Faraday.default_adapter
end
def initialize(&block)
@adapter = nil
@handlers = []
build(&block)
end
def build(options = {})
def initialize_dup(original)
super
@adapter = original.adapter
@handlers = original.handlers.dup
end
def build
raise_if_locked
@handlers.clear unless options[:keep]
yield(self) if block_given?
block_given? ? yield(self) : request(:url_encoded)
adapter(Faraday.default_adapter) unless @adapter
end
@ -109,7 +108,7 @@ module Faraday
end
ruby2_keywords def adapter(klass = NO_ARGUMENT, *args, &block)
return @adapter if klass == NO_ARGUMENT
return @adapter if klass == NO_ARGUMENT || klass.nil?
klass = Faraday::Adapter.lookup_middleware(klass) if klass.is_a?(Symbol)
@adapter = self.class::Handler.new(klass, *args, &block)
@ -164,6 +163,7 @@ module Faraday
def app
@app ||= begin
lock!
ensure_adapter!
to_app
end
end
@ -182,10 +182,6 @@ module Faraday
@adapter == other.adapter
end
def dup
self.class.new(@handlers.dup, @adapter.dup)
end
# ENV Keys
# :http_method - a symbolized request HTTP method (:get, :post)
# :body - the request body that will eventually be converted to a string.
@ -216,6 +212,9 @@ module Faraday
private
LOCK_ERR = "can't modify middleware stack after making a request"
MISSING_ADAPTER_ERROR = "An attempt to run a request with a Faraday::Connection without adapter has been made.\n" \
"Please set Faraday.default_adapter or provide one when initializing the connection.\n" \
'For more info, check https://lostisland.github.io/faraday/usage/.'
def raise_if_locked
raise StackLocked, LOCK_ERR if locked?
@ -227,6 +226,10 @@ module Faraday
raise 'Adapter should be set using the `adapter` method, not `use`'
end
def ensure_adapter!
raise MISSING_ADAPTER_ERROR unless @adapter
end
def adapter_set?
!@adapter.nil?
end

View File

@ -151,6 +151,9 @@ RSpec.describe Faraday::Connection do
end
describe '#close' do
before { Faraday.default_adapter = :test }
after { Faraday.default_adapter = nil }
it 'can close underlying app' do
expect(conn.app).to receive(:close)
conn.close

View File

@ -20,6 +20,8 @@ RSpec.describe Faraday::RackBuilder do
end
subject { conn.builder }
before { Faraday.default_adapter = :test }
after { Faraday.default_adapter = nil }
context 'with default stack' do
let(:conn) { Faraday::Connection.new }
@ -86,13 +88,6 @@ RSpec.describe Faraday::RackBuilder do
it { expect(subject.handlers).to eq([Apple]) }
it 'allows rebuilding' do
subject.build do |builder|
builder.use(Orange)
end
expect(subject.handlers).to eq([Orange])
end
it 'allows use' do
subject.use(Orange)
expect(subject.handlers).to eq([Apple, Orange])