Skip to content

Commit

Permalink
Merge pull request #1 from umbrellio/add-http-3-support
Browse files Browse the repository at this point in the history
add http3 support
  • Loading branch information
tycooon authored Mar 15, 2018
2 parents a64dab2 + 1eaa594 commit e30d725
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 7 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
/tmp/
.rspec_status
Gemfile.lock
/gemfiles/*.lock
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ AllCops:
TargetRubyVersion: 2.5
Exclude:
- vendor/**/*
- gemfiles/**/*
4 changes: 4 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ rvm:
- 2.5
- ruby-head

gemfile:
- gemfiles/http3.gemfile
- gemfiles/http4.gemfile

before_install: gem install bundler

env: SUITE="rspec"
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
EzClient is [HTTP gem](https://github.com/httprb/http) wrapper for easy persistent connections and more.

## Installation
Add this lines to your application's Gemfile:
Add this line to your application's Gemfile:

```ruby
gem "ezclient"
gem "http", github: "httprb/http" # version 3 is not supported for now
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion ezclient.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Gem::Specification.new do |spec|
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^spec/}) }
spec.require_paths = ["lib"]

spec.add_runtime_dependency "http", ">= 4.0.0dev"
spec.add_runtime_dependency "http", ">= 3"

spec.add_development_dependency "bundler"
spec.add_development_dependency "coveralls"
Expand Down
6 changes: 6 additions & 0 deletions gemfiles/http3.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec path: ".."

gem "http", "~> 3.0"
6 changes: 6 additions & 0 deletions gemfiles/http4.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

source "https://rubygems.org"
gemspec path: ".."

gem "http", github: "httprb/http"
1 change: 1 addition & 0 deletions lib/ezclient.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "http"
require "ezclient/http_patch"
require "ezclient/version"
require "ezclient/client"
require "ezclient/request"
Expand Down
32 changes: 32 additions & 0 deletions lib/ezclient/http_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# frozen_string_literal: true

unless HTTP::Client.instance_methods.include?(:build_request)
# Backport of https://github.com/httprb/http/pull/458 for HTTP v3
class HTTP::Client
def build_request(verb, uri, opts = {})
opts = @default_options.merge(opts)
uri = make_request_uri(uri, opts)
headers = make_request_headers(opts)
body = make_request_body(opts, headers)
proxy = opts.proxy

HTTP::Request.new(
verb: verb,
uri: uri,
headers: headers,
proxy: proxy,
body: body,
auto_deflate: opts.feature(:auto_deflate),
)
end
end
end

unless HTTP::Request::Body.instance_methods.include?(:source)
# Backport for HTTP v3
class HTTP::Request::Body
def source
@body
end
end
end
16 changes: 15 additions & 1 deletion lib/ezclient/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ def http_client
# Only used to build proper HTTP::Request and HTTP::Options instances
@http_client ||= begin
http_client = client.dup
http_client = http_client.timeout(timeout) if timeout
http_client = set_timeout(http_client)
http_client = http_client.basic_auth(basic_auth) if basic_auth
http_client
end
Expand Down Expand Up @@ -149,6 +149,20 @@ def prepare_headers(headers)
headers
end

def set_timeout(client)
return client unless timeout

timeout_args =
# for HTTP v3
if HTTP::Timeout::Global.ancestors.include?(HTTP::Timeout::PerOperation)
[:global, read: timeout]
else
[timeout]
end

client.timeout(*timeout_args)
end

def basic_auth
@basic_auth ||= begin
case options[:basic_auth]
Expand Down
4 changes: 2 additions & 2 deletions spec/ezclient_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -258,15 +258,15 @@ def self.sign!(*); end

it "uses it for request" do
expect(opts.timeout_class).to eq(HTTP::Timeout::Global)
expect(opts.timeout_options).to eq(global_timeout: 10)
expect(opts.timeout_options.values).to eq([10]) # Keys are different in HTTP v3 and v4
end

context "when timeout request option is provided as well" do
let(:request_options) { Hash[timeout: "15"] }

it "uses request option for request" do
expect(opts.timeout_class).to eq(HTTP::Timeout::Global)
expect(opts.timeout_options).to eq(global_timeout: 15)
expect(opts.timeout_options.values).to eq([15]) # Keys are different in HTTP v3 and v4
end
end
end
Expand Down
1 change: 0 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
Coveralls::SimpleCov::Formatter,
])

SimpleCov.minimum_coverage(100)
SimpleCov.start

require "webmock/rspec"
Expand Down

0 comments on commit e30d725

Please sign in to comment.