Faraday 2.16 ↑

Options classes are no longer Structs ↑

ConnectionOptions, RequestOptions, SSLOptions, ProxyOptions and Env now inherit from Faraday::BaseOptions. Before, they inherited from Faraday::Options, which is a Struct. The adapters and middleware listed in awesome-faraday work unchanged. Code that uses these classes like a Struct can break, so test your application with 2.16.0.beta1 first.

Am I affected? ↑

These calls work as before:

You are affected if your code does one of these with an options object or an env:

  • writes a value with []= on ConnectionOptions

  • reads values with each, keys, values or dig (except env.dig), or calls members on an object

  • compares two objects with ==

  • creates one with positional arguments

  • checks is_a?(Faraday::Options) or is_a?(Struct)

Replacing the removed methods ↑

Before After
‘conn_options = 5` ‘conn_options.timeout = 5`
‘options.each { |key, value| … }` ‘options.to_hash.each { |key, value| … }`
‘options.members` ‘options.class::MEMBERS`
‘options.values` ‘options.to_hash.values`
‘options.dig(:proxy, :uri)` ‘options.proxy&.uri`
‘options.delete(:timeout)` ‘options.timeout = nil`
‘options == other` ‘options.to_hash == other.to_hash`
‘Faraday::SSLOptions.new(true)` ‘Faraday::SSLOptions.new(verify: true)`
‘Faraday::Env.new(:get, body, url, …)` ‘Faraday::Env.from(method: :get, body: body, url: url, …)`
‘value.is_a?(Faraday::Options)` ‘value.is_a?(Faraday::OptionsLike)`

== now compares identity, so two options objects with the same values are no longer equal. This also applies to Faraday::Request#==.

Deprecated, to be removed in Faraday 3.0 ↑

  • [], []=, fetch and key? on RequestOptions, SSLOptions and ProxyOptions. Use the readers and writers instead: options.timeout for options[:timeout], options.timeout = 5 for options[:timeout] = 5, and options.verify.nil? ? true : options.verify for options.fetch(:verify, true). Env keeps its own [] and []=, which are not deprecated.

  • to_h on every options class and on Env. Use to_hash, which leaves out nil values and converts nested options objects to Hashes (except in Env). to_h keeps the old result: every member, nil included. Env#to_h leaves out custom members.

  • empty? on every options class. Use options.to_hash.empty?.

  • members on the options classes, such as Faraday::SSLOptions.members. Use Faraday::SSLOptions::MEMBERS, or Faraday::SSLOptions.method_defined?(:verify_hostname) to check for a member.

  • Faraday::Options.memoized. Write the reader yourself.

There is no runtime warning yet.

Other changes in behaviour ↑

  • nil no longer overwrites a value. For example, Faraday.new(url, headers: nil) keeps the headers from Faraday.default_connection_options. false still overwrites a value, except where it means “not set”: request, ssl and proxy in ConnectionOptions, and request, ssl and the headers in Env.

  • .from(options), merge and deep_dup return deep copies, which share no Hash or options object with the original. merge and merge! also deep-merge Hash values, such as RequestOptions#context. Env is the exception: Env.from(env) shares the bodies and the URL, and Env#merge! deep-merges only request, ssl and the headers.

  • More values are converted to options objects: proxy in ConnectionOptions and RequestOptions becomes a ProxyOptions, ProxyOptions#uri becomes a URI, and ssl in Env becomes an SSLOptions.

  • to_hash converts nested options objects to Hashes, except in Env.

  • inspect shows members that are false.

Your own options classes ↑

Subclasses of Faraday::Options work as before. To write a new options class, or to move an existing one off Struct, see Custom Options Classes.

Faraday 2.0 ↑

Adapters have moved! ↑

With this release, we’ve officially moved all adapters, except for the net_http one, out of Faraday. What that means, is that they won’t be available out-of-the-box anymore, and you’ll instead need to add them to your Gemfile.

NOTE: the net_http adapter was originally removed as well in version 2.0, but quickly reintroduced in version 2.0.1. We strongly suggest you to skip version 2.0 and instead use version 2.0.1 or greater.

Why was this decision made? ↑

We’ve taken this decision for the following technical reasons:

  • We wanted the Faraday gem to focus on providing a clean, standardized, middleware-stack-based API.

  • We wanted to free the core team from maintaining all the different adapters, relying more on the community to maintain them based on the broad interest. This will allow the core team to focus on implementing features focused on the Faraday API more quickly, without having to push it on all adapters immediately.

  • With the community creating more and more adapters, we wanted to avoid having first and second-class adapters by having some of them included with the gem and others available externally.

  • Moving adapters into separate gems solve the dependency issues once and for all. Faraday will remain a dependency-free gem, while adapter gems will be able to automatically pull any necessary dependency, without having to rely on the developer to do so.

So what will this mean for me? ↑

We did our best to make this transition as painless as possible for you, so here is what we did.

  • All adapters, except for the net_http one, have already been moved out and released as separate gems. They’ve then been re-added into Faraday’s v1 dependencies so that you wouldn’t notice. This means that immediately after v2.0 will be released, all the adapters that were previously available will be already compatible with Faraday 2.0!

  • We’ve setup an Awesome Faraday repository, where you can find and discover adapters. We also highlighted their unique features and level of compliance with Faraday’s features.

That’s great! What should I change in my code immediately after upgrading? ↑

  • If you just use the default net_http adapter, then you don’t need to do anything!

  • Otherwise, add the corresponding adapter gem to your Gemfile (e.g. faraday-net_http_persistent). Then, simply require them after you require faraday. “‘ruby # Gemfile gem ’faraday’ gem ‘faraday-net_http_persistent’

# Code require ‘faraday’ require ‘faraday/net_http_persistent’ “‘

Faraday Middleware Deprecation ↑

In case you never used it, Faraday Middleware is a handy collection of middleware that have so far been maintained by the Faraday core team. With Faraday 2.0 focusing on becoming an ecosystem, rather than an out-of-the-box solution, it only made sense to take the same approach for middleware as we did for adapters. For this reason, faraday_middleware will not be updated to support Faraday 2.0. Instead, we’ll support the transition from centralised-repo collection of middleware to individual middleware gems, effectively replicating what we did with adapters. Each middleware will have its own repository and gem, and it will allow developers to only add the ones they require to their Gemfile.

So don’t fret yet! We’re doing our best to support our faraday_middleware users out there, so here are the steps we’ve taken to make this work: * We’ve promoted the highly popular JSON middleware (both request encoding and response parsing) to a core middleware and it will now be shipped together with Faraday. We expect many faraday_middleware users will be able to just stop using the extra dependency thanks to this. * We’ve created a faraday-middleware-template repository that will make creating a new middleware gem simple and straightforward. * We’ve added a middleware section to the awesome-faraday repo, so you can easily find available middleware when you need it.

It will obviously take time for some of the middleware in faraday_middleware to make its way into a separate gem, so we appreciate this might be an upgrade obstacle for some. However this is part of an effort to focus the core team resources tackling the most requested features. We’ll be listening to the community and prioritize middleware that are most used, and will be welcoming contributors who want to become owners of the middleware when these become separate from the faraday_middleware repo.

Bundled middleware moved out ↑

Moving middleware into its own gem makes sense not only for faraday_middleware, but also for middleware bundled with Faraday. As of v2.0, the retry and multipart middleware have been moved to separate faraday-retry and faraday-multipart gems. These have been identified as good candidates due to their complexity and external dependencies. Thanks to this change, we were able to make Faraday 2.0 completely dependency free 🎉 (the only exception being ruby2_keywords, which will be necessary only while we keep supporting Ruby 2.6).

So what should I do if I currently use the retry or multipart middleware? ↑

Upgrading is pretty simple, because the middleware was simply moved out to external gems. All you need to do is to add them to your gemfile (either faraday-retry or faraday-multipart and require them before usage:

# Gemfile
gem 'faraday-multipart'
gem 'faraday-retry'

# Connection initializer
require 'faraday/retry'
require 'faraday/multipart'

conn = Faraday.new(url) do |f|
  f.request :multipart
  f.request :retry
  # ...
end

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:

# `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)

The register_middleware method also previously allowed to provide a symbol, string, proc, or array but this has been removed from the v2.0 release to simplify the interface. (EDIT: symbol/string/proc have subsequently been reintroduced in v2.2, but not the array).

Authentication helper methods in Connection have been removed ↑

You were previously able to call authorization, basic_auth and token_auth against the Connection object, but this helper methods have now been dropped. Instead, you should be using the equivalent middleware, as explained in this page.

For more details, see github.com/lostisland/faraday/pull/1306

The dependency method in middlewares has been removed ↑

In Faraday 1, a deferred require was used with the dependency method.

In Faraday 2, that method has been removed. In your middleware gems, use a regular require at the top of the file,

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.default_connection_options will now be deep-merged into new connections to avoid overriding them (e.g. headers).

  • 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 ↑

Errors ↑

Custom adapters ↑

If you have written a custom adapter, please be aware that env.body is now an alias to the two new properties request_body and response_body. This should work without you noticing if your adapter inherits from Faraday::Adapter and calls save_response, but if it doesn’t, then please ensure you set the status BEFORE the body while processing the response.

Others ↑

  • Dropped support for jruby and Rubinius.

  • Officially supports Ruby 2.4+

  • In order to specify the adapter you now MUST use the adapter method on the connection builder. If you don’t do so and your adapter inherits from Faraday::Adapter then Faraday will raise an exception. Otherwise, Faraday will automatically push the default adapter at the end of the stack causing your request to be executed twice.

    “‘ruby class OfficialAdapter < Faraday::Adapter … end

class MyAdapter … end

This will raise an exception ↑

conn = Faraday.new(...) do |f| f.use OfficialAdapter end

This will cause Faraday inserting the default adapter at the end of the stack ↑

conn = Faraday.new(...) do |f| f.use MyAdapter end

You MUST use adapter method ↑

conn = Faraday.new(...) do |f| f.adapter AnyAdapter end

“‘