Skip to content

Commit

Permalink
Remove default Faraday.default_adapter value and add exception messag…
Browse files Browse the repository at this point in the history
…e with link to usage documentation. (#1354)
  • Loading branch information
iMacTia committed Dec 30, 2021
1 parent 1c3e276 commit c9b8490
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
1 change: 1 addition & 0 deletions UPGRADING.md
Expand Up @@ -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

Expand Down
1 change: 0 additions & 1 deletion lib/faraday.rb
Expand Up @@ -150,5 +150,4 @@ def method_missing(name, *args, &block)
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
2 changes: 1 addition & 1 deletion lib/faraday/connection.rb
Expand Up @@ -117,7 +117,7 @@ def headers=(hash)

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
Expand Down
39 changes: 21 additions & 18 deletions lib/faraday/rack_builder.rb
Expand Up @@ -58,22 +58,21 @@ def build(app = nil)
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 initialize_dup(original)
super
@adapter = original.adapter
@handlers = original.handlers.dup
end

def build(options = {})
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

Expand Down Expand Up @@ -109,7 +108,7 @@ def locked?
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)
Expand Down Expand Up @@ -164,6 +163,7 @@ def build_response(connection, request)
def app
@app ||= begin
lock!
ensure_adapter!
to_app
end
end
Expand All @@ -182,10 +182,6 @@ def ==(other)
@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.
Expand Down Expand Up @@ -216,6 +212,9 @@ def build_env(connection, request)
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?
Expand All @@ -227,6 +226,10 @@ def raise_if_adapter(klass)
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
Expand Down
3 changes: 3 additions & 0 deletions spec/faraday/connection_spec.rb
Expand Up @@ -151,6 +151,9 @@ def decode(params)
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
Expand Down
9 changes: 2 additions & 7 deletions spec/faraday/rack_builder_spec.rb
Expand Up @@ -20,6 +20,8 @@ class Banana < Handler
end

subject { conn.builder }
before { Faraday.default_adapter = :test }
after { Faraday.default_adapter = nil }

context 'with default stack' do
let(:conn) { Faraday::Connection.new }
Expand Down Expand Up @@ -86,13 +88,6 @@ class Banana < Handler

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])
Expand Down

0 comments on commit c9b8490

Please sign in to comment.