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

Autoloading, dependency loading and middleware registry cleanup #1301

Merged
merged 2 commits into from
Aug 9, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,25 @@ We did our best to make this transition as painless as possible for you, so here
* We've setup an [Awesome Faraday](https://github.com/lostisland/awesome-faraday) repository, where you can find and discover adapters.
We also highlighted their unique features and level of compliance with Faraday's features.

### Autoloading and dependencies

Faraday has until now provided and relied on a complex dynamic dependencies system.
This would allow to reference classes and require dependencies only when needed (e.g. when running the first request) based
on the middleware/adapters used.
As part of Faraday v2.0, we've removed all external dependencies, which means we don't really need this anymore.
This change should not affect you directly, but if you're registering middleware then be aware of the new syntax:

```ruby
# `name` here can be anything you want.
# `klass` is your custom middleware class.
# This method can also be called on `Faraday::Adapter`, `Faraday::Request` and `Faraday::Response`
Faraday::Middleware.register_middleware(name: klass)
```

### Others

* Rename `Faraday::Request#method` to `#http_method`.
* Remove `Faraday::Response::Middleware`. You can now use the new `on_complete` callback provided by `Faraday::Middleware`.

## Faraday 1.0

Expand Down
9 changes: 1 addition & 8 deletions faraday.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

require_relative 'lib/faraday/version'

Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength
Gem::Specification.new do |spec|
olleolleolle marked this conversation as resolved.
Show resolved Hide resolved
spec.name = 'faraday'
spec.version = Faraday::VERSION

Expand All @@ -15,14 +15,7 @@ Gem::Specification.new do |spec| # rubocop:disable Metrics/BlockLength

spec.required_ruby_version = '>= 2.4'

spec.add_dependency 'faraday-em_http', '~> 1.0'
spec.add_dependency 'faraday-em_synchrony', '~> 1.0'
spec.add_dependency 'faraday-excon', '~> 1.1'
spec.add_dependency 'faraday-httpclient', '~> 1.0.1'
spec.add_dependency 'faraday-net_http', '~> 1.0'
spec.add_dependency 'faraday-net_http_persistent', '~> 1.1'
spec.add_dependency 'faraday-patron', '~> 1.0'
spec.add_dependency 'faraday-rack', '~> 1.0'
spec.add_dependency 'multipart-post', '>= 1.2', '< 3'
spec.add_dependency 'ruby2_keywords', '>= 0.0.4'

Expand Down
26 changes: 2 additions & 24 deletions lib/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@
require 'date'
require 'set'
require 'forwardable'
require 'faraday/middleware_registry'
require 'faraday/dependency_loader'

unless defined?(::Faraday::Timer)
require 'timeout'
::Faraday::Timer = Timeout
end

require 'faraday/version'
require 'faraday/methods'
require 'faraday/error'
require 'faraday/middleware_registry'
require 'faraday/utils'
require 'faraday/options'
require 'faraday/connection'
Expand All @@ -23,7 +17,6 @@
require 'faraday/adapter'
require 'faraday/request'
require 'faraday/response'
require 'faraday/error'
require 'faraday/file_part'
require 'faraday/param_part'

Expand Down Expand Up @@ -101,19 +94,6 @@ def new(url = nil, options = {}, &block)
Faraday::Connection.new(url, options, &block)
end

# @private
# Internal: Requires internal Faraday libraries.
#
# @param libs [Array] one or more relative String names to Faraday classes.
# @return [void]
def require_libs(*libs)
libs.each do |lib|
require "#{lib_path}/#{lib}"
end
end

alias require_lib require_libs

# Documented elsewhere, see default_adapter reader
def default_adapter=(adapter)
@default_connection = nil
Expand Down Expand Up @@ -169,6 +149,4 @@ def method_missing(name, *args, &block)
self.root_path = File.expand_path __dir__
self.lib_path = File.expand_path 'faraday', __dir__
self.default_adapter = :net_http

require_lib 'autoload' unless ENV['FARADAY_NO_AUTOLOAD']
olleolleolle marked this conversation as resolved.
Show resolved Hide resolved
end
5 changes: 0 additions & 5 deletions lib/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ module Faraday
# responsible for fulfilling a Faraday request.
class Adapter
extend MiddlewareRegistry
extend DependencyLoader

CONTENT_LENGTH = 'Content-Length'

register_middleware File.expand_path('adapter', __dir__),
test: [:Test, 'test'],
typhoeus: [:Typhoeus, 'typhoeus']

# This module marks an Adapter as supporting parallel requests.
module Parallelism
attr_writer :supports_parallel
Expand Down
2 changes: 2 additions & 0 deletions lib/faraday/adapter/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,5 @@ def call(env)
end
end
end

Faraday::Adapter.register_middleware(test: Faraday::Adapter::Test)
15 changes: 0 additions & 15 deletions lib/faraday/adapter/typhoeus.rb

This file was deleted.

87 changes: 0 additions & 87 deletions lib/faraday/autoload.rb

This file was deleted.

37 changes: 0 additions & 37 deletions lib/faraday/dependency_loader.rb

This file was deleted.

1 change: 1 addition & 0 deletions lib/faraday/logging/formatter.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'pp'

module Faraday
module Logging
# Serves as an integration point to customize logging
Expand Down
1 change: 0 additions & 1 deletion lib/faraday/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ module Faraday
# Middleware is the basic base class of any Faraday middleware.
class Middleware
extend MiddlewareRegistry
extend DependencyLoader

attr_reader :app, :options

Expand Down