Losing time to Faraday - The Code Whisperer

I just wanted an HTTP client.

I asked my Twitter community which HTTP client to use. Certainly not HTTParty, because cool people don’t use HTTParty anymore. One takes these risks when one falls out of the loop with the latest and greatest tools.


This is a companion discussion topic for the original entry at http://blog.thecodewhisperer.com/permalink/losing-time-to-faraday

I know convention over configuration is common in Ruby programming, but it strikes me that the API of the Faraday object being constructed does not *always* require an adapter as a strong dependency. Forgive my Java:

new Faraday(new FaradayDefaultAdapter()).addMiddleware(...)

I think this "defaulting" is leading you to some strong coupling to the Adapter, which we know can be a problem if you want to unit test your requests. The documentation shows you'll need to pass in a test Adapter:

test = Faraday.new do |builder|
builder.adapter :test
end

(there is also the now-classic problem of accidentally charging a real credit card during test runs: http://misko.hevery.com/200... describes it better than me)

Going back to Java, I'd have this in mind:

Faraday.withDefaultAdapter().addMiddleware(...)

or, perhaps even better, adding any other sensible defaults:

Faraday.withSensibleDefaultStack().addMiddleware(...)

I'd rather couple myself to Faraday by *intent* ("I want the default stack"), than by implementation details (knowing what the "default stack" includes and how to instantiate the pieces and in which order to apply them). If, later, I need those details, then I simply inline `withSensibleDefaultStack()` and I can see how it's done.

I love Misko's article. Warning people about singletons started my career. http://www.ibm.com/develope... I don't want a singleton: I want Faraday to hide some implementation details from me without making it impossible for me to gain access to them when, later, I need them. I think my proposal achieves that.

Eh, Faraday... this almost makes me want to share how I lost time to that library. Suffice to say it's not very good.

Try http://docs.python-requests... - or is that the wrong language?

Worked like a charm, thanks