ostinelli / net-http2

NetHttp2 is an HTTP/2 client for Ruby.
MIT License
140 stars 31 forks source link
client gem http2

Code Climate Gem Version

NetHttp2

NetHttp2 is an HTTP/2 client for Ruby.

Installation

Just install the gem:

$ gem install net-http2

Or add it to your Gemfile:

gem 'net-http2'

Usage

NetHttp2 can perform sync and async calls. Sync calls are very similar to the HTTP/1 calls, while async calls take advantage of the streaming properties of HTTP/2.

To perform a sync call:

require 'net-http2'

# create a client
client = NetHttp2::Client.new("http://nghttp2.org")

# send request
response = client.call(:get, '/')

# read the response
response.ok?      # => true
response.status   # => '200'
response.headers  # => {":status"=>"200"}
response.body     # => "A body"

# close the connection
client.close

To perform an async call:

require 'net-http2'

# create a client
client = NetHttp2::Client.new("http://nghttp2.org")

# prepare request
request = client.prepare_request(:get, '/')
request.on(:headers) { |headers| p headers }
request.on(:body_chunk) { |chunk| p chunk }
request.on(:close) { puts "request completed!" }

# send
client.call_async(request)

# Wait for all outgoing stream to be closed
client.join(timeout: 5)

# close the connection
client.close

Objects

NetHttp2::Client

Methods

Allows to set a callback for the client. The only available event is :error, which allows to set a callback when an error is raised at socket level, hence in the underlying socket thread.

client.on(:error) { |exception| puts "Exception has been raised: #{exception}" }

It is RECOMMENDED to set the :error callback: if none is defined, the underlying socket thread may raise an error in the main thread at unexpected execution times.

Blocking calls

These behave similarly to HTTP/1 calls.

Non-blocking calls

The real benefit of HTTP/2 is being able to receive body and header streams. Instead of buffering the whole response, you might want to react immediately upon receiving those streams. This is what non-blocking calls are for.

NetHttp2::Request

Methods

NetHttp2::Response

Methods

Thread-Safety

NetHttp2 is thread-safe. However, some caution is imperative:

Contributing

So you want to contribute? That's great! Please follow the guidelines below. It will make it easier to get merged in.

Before implementing a new feature, please submit a ticket to discuss what you intend to do. Your feature might already be in the works, or an alternative implementation might have already been discussed.

Do not commit to master in your fork. Provide a clean branch without merge commits. Every pull request should have its own topic branch. In this way, every additional adjustments to the original pull request might be done easily, and squashed with git rebase -i. The updated branch will be visible in the same pull request, so there will be no need to open new pull requests when there are changes to be applied.

Ensure to include proper testing. To run tests you simply have to be in the project's root directory and run:

$ rake