Skip to content

zhubert/all-the-https

Repository files navigation

AllTheHTTPs

Why pick one HTTP client when you can use ALL of them?

Every long-lived Ruby application eventually accumulates every HTTP client library known to humankind. Your Gemfile.lock is a graveyard of good intentions: someone added faraday in 2019, then httparty showed up in a PR "just for this one service," and before you know it rest-client, http.rb, typhoeus, and excon are all living rent-free in your dependency tree.

AllTheHTTPs embraces this destiny. Instead of fighting the inevitable, it celebrates it. Each HTTP request randomly selects from whichever HTTP client libraries you have installed. You never know which one will answer the call.

🎲 AllTheHTTPs rolled... Typhoeus! (7/9 in pool)
🎲 AllTheHTTPs rolled... Net::HTTP! (7/9 in pool)
🎲 AllTheHTTPs rolled... HTTParty! (7/9 in pool)
🎲 AllTheHTTPs rolled... Faraday! (7/9 in pool)

Installation

Add it to your Gemfile alongside your existing collection:

gem "all_the_https"

# You probably already have most of these:
gem "faraday"
gem "httparty"
gem "rest-client"
gem "http"
gem "excon"
gem "typhoeus"
gem "httpclient"
# gem "patron"   # if you're feeling brave
# gem "curb"     # if you're feeling braver

Then:

$ bundle install

Usage

require "all_the_https"

# Simple GET — who knows which library will handle it!
response = AllTheHTTPs.get("https://api.example.com/users")
# => 🎲 AllTheHTTPs rolled... HTTP.rb! (8/9 in pool)

response.status       # => 200
response.body         # => '{"users": [...]}'
response.headers      # => {"content-type" => "application/json", ...}
response.adapter_used # => :http_gem
response.success?     # => true

# POST with a body
AllTheHTTPs.post("https://api.example.com/users",
  headers: { "Content-Type" => "application/json" },
  body: '{"name": "HTTP Maximalist"}'
)
# => 🎲 AllTheHTTPs rolled... RestClient! (8/9 in pool)

# All the verbs
AllTheHTTPs.put(url, headers: {}, body: "data")
AllTheHTTPs.patch(url, headers: {}, body: "data")
AllTheHTTPs.delete(url, headers: {})

Supported Adapters

AllTheHTTPs ships with adapters for 11 HTTP client libraries:

Adapter Gem Notes
Net::HTTP stdlib Always available. The cockroach of Ruby HTTP.
OpenURI stdlib GET only. It has boundaries.
Faraday faraday The adapter that itself uses adapters.
HTTParty httparty Making HTTP fun since 2008.
RestClient rest-client Sounds like a spa treatment.
HTTP.rb http Bold gem name. Respect.
Excon excon Sounds like a villain convention.
Typhoeus typhoeus Named after a Greek monster, like your deps.
HTTPClient httpclient The sensible one.
Patron patron Requires libcurl. How committed are you?
Curb curb C-level performance, Ruby-level chaos.

The gem auto-detects which libraries are installed. Only installed gems participate in the random pool. At minimum you'll always have Net::HTTP and OpenURI (stdlib).

The Response Object

Every adapter returns a normalized AllTheHTTPs::Response:

response.status       # Integer — HTTP status code
response.body         # String — response body
response.headers      # Hash — response headers
response.adapter_used # Symbol — which library handled the request
response.success?     # Boolean — true for 2xx status codes

FAQ

Q: Should I use this in production? A: Absolutely. What could go wrong?

Q: What if I only have Net::HTTP installed? A: Then every roll lands on Net::HTTP. Boring, but technically functional.

Q: Is this a joke? A: Yes. But it works. The best kind of joke.

Q: What's the performance like? A: Unpredictable, which is kind of the whole point.

Development

bundle install
bundle exec rspec

License

The gem is available as open source under the terms of the MIT License.

About

Embrace your destiny

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages