Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove default default_adapter (yes, you read that right) #1354

Merged
merged 2 commits into from
Dec 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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?
[email protected]?
end
Expand Down
3 changes: 3 additions & 0 deletions spec/faraday/connection_spec.rb
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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