Why use Faraday over Net::HTTP?
Asked Answered
C

1

14

Why would someone use the Faraday gem vs directly using Net::HTTP? What is the benefit of using a wrapper like this?

Creeper answered 2/1, 2014 at 18:55 Comment(2)
This is going to draw opinions, not facts.Decoy
There are objective reasons to use Faraday over Net::HTTP.Angeloangelology
E
14

Using the Net::HTTP adapter directly is fine, it's just binding your code to a particular interface which may differ from alternate tools. The main reason one would use Faraday is that it wraps multiple adapters, one of which is Net::HTTP. If you're using Faraday and start out with Net:HTTP, you can later decide to use Excon, Typhoeus, Patron or EventMachine without changing more than a line or two of configuration. Had you used Net::HTTP directly, you'd have to change a lot of implementation-specific code to switch to another adapter.

Engrail answered 2/1, 2014 at 20:19 Comment(3)
Good explanation. You should also mention middleware. For example, I use JSON middleware to automatically encode my request as JSON and decode the response from JSON.Angeloangelology
Faraday should also help normalize exceptions across all of the libraries as well, correct?Creeper
Yes @user3084728, Faraday will catch each adapter's exceptions and re-throw them as Faraday exceptions.Engrail

© 2022 - 2024 — McMap. All rights reserved.