net-http-ext
Safe defaults, persistent connections, thread safety, and basic logging for Ruby's Net::HTTP library.
A simple wrapper around net/http/persistent
in pure Ruby.
About 💡
A very simple wrapper around the net/http/persistent
library (which is a wrapper around net/http
) that provides a few extra features and safe defaults. It is designed to be a lite-batteries-included version of net/http/persistent
that is easy to use and configure.
Should you need anything more complex, check out faraday
+ araday-net_http_persistent
.
Benefits ⭐
- Reuse connections for multiple requests
- Thread safe
- Secure defaults
- Basic logging
- Automatically rebuild the connection if it is closed by the server
- Automatically retry requests on connection failures if
max_retries
is set to a value >1 - Easy to use and configure
Installation 💎
You can download this Gem from GitHub Packages or RubyGems
Via a Gemfile:
source "https://rubygems.org"
gem "net-http-ext", "~> X.X.X" # Replace X.X.X with the latest version
Via the CLI:
gem install net-http-ext
Usage 💻
Basic Usage
Here is an example using the library in its most basic form:
require "net/http/ext"
# initialize the client
client = Net::HTTP::Ext.new("httpbin.org")
# make a request
response = client.get("/")
puts response.code
puts response.body
Extended Usage
Here is a more detailed/full usage example:
require "net/http/ext"
# Initialize the client
client = Net::HTTP::Ext.new("httpbin.org")
response = client.get("/")
puts response.code
# GET request with headers and query parameters
response = client.get("/get?param1=value1¶m2=value2",
headers: { "Authorization" => "Bearer token123" }
)
puts response.body
# GET request using query parameters as a qwarg
response = client.get("/get",
params: { param1: "value1kwarg", param2: "value2kwarg" },
)
puts response.body
# POST request with JSON payload
response = client.post("/post",
payload: { name: "John Doe", email: "john@example.com" }
)
puts response.body
# POST request with JSON payload and custom content type
response = client.post("/post",
headers: { "Content-Type" => "application/json+custom" },
payload: { name: "John Doe", email: "john@example.com" }
)
puts response.body
# Custom timeouts
client = Net::HTTP::Ext.new("https://httpbin.org",
open_timeout: 5, # connection establishment timeout (seconds)
read_timeout: 10, # response read timeout (seconds)
idle_timeout: 30, # how long to keep idle connections open (seconds)
request_timeout: 15 # overall request timeout (seconds)
)
response = client.get("/delay/2") # Simulate a delay of 2 seconds
puts response.code
# Default headers on all requests
client = Net::HTTP::Ext.new("https://httpbin.org",
default_headers: {
"User-Agent" => "MyApp/1.0"
}
)
response = client.get("/headers")
puts response.body
# Make a get request and automatically parse it as JSON
response = client.get_json("/get?param1=value1¶m2=value2")
puts response["args"]["param1"] # => "value1"
See the full source code at
lib/net/http/ext.rb
for more details on the available options and methods.
Contributing 🤝
- Fork the repository
- Bootstrap the project with
script/bootstrap
- Create a new branch (
git checkout -b feature/your-feature
) - Write your code
- Run the tests (
script/test
) - Run the acceptance tests (
script/acceptance
) - Run the linter (
script/lint -A
) - Commit your changes (
git commit -m "Add some feature"
) - Push to the branch (
git push origin feature/your-feature
) - Create a new Pull Request
- Wait for an approval and passing ci tests
- 🎉