Skip to content

Commit ee9e031

Browse files
committed
Initial implementation of wrapper.
0 parents  commit ee9e031

File tree

12 files changed

+440
-0
lines changed

12 files changed

+440
-0
lines changed

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/.bundle/
2+
/.yardoc
3+
/Gemfile.lock
4+
/_yardoc/
5+
/coverage/
6+
/doc/
7+
/pkg/
8+
/spec/reports/
9+
/tmp/
10+
11+
# rspec failure tracking
12+
.rspec_status

.rspec

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
--format documentation
2+
--warnings
3+
--require spec_helper

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
language: ruby
2+
sudo: false
3+
dist: trusty
4+
cache: bundler
5+
6+
matrix:
7+
include:
8+
- rvm: 2.0
9+
- rvm: 2.1
10+
- rvm: 2.2
11+
- rvm: 2.3
12+
- rvm: 2.4
13+
- rvm: jruby-head
14+
env: JRUBY_OPTS="--debug -X+O"
15+
- rvm: ruby-head
16+
- rvm: rbx-3
17+
allow_failures:
18+
- rvm: ruby-head
19+
- rvm: jruby-head
20+
- rvm: rbx-3

Gemfile

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
source 'https://rubygems.org'
2+
3+
gemspec
4+
5+
group :test do
6+
gem 'simplecov'
7+
gem 'coveralls', require: false
8+
end

README.md

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Async::HTTP::Faraday
2+
3+
Provides an adaptor for [Faraday] to perform async HTTP requests. If you are designing a new library, you should probably just use `Async::HTTP::Client` directly.
4+
5+
[![Build Status](https://secure.travis-ci.org/socketry/async-http-faraday.svg)](http://travis-ci.org/socketry/async-http-faraday)
6+
[![Code Climate](https://codeclimate.com/github/socketry/async-http-faraday.svg)](https://codeclimate.com/github/socketry/async-http-faraday)
7+
[![Coverage Status](https://coveralls.io/repos/socketry/async-http-faraday/badge.svg)](https://coveralls.io/r/socketry/async-http-faraday)
8+
9+
[async]: https://github.com/socketry/async
10+
[async-io]: https://github.com/socketry/async-io
11+
[Faraday]: https://github.com/lostisland/faraday
12+
13+
## Installation
14+
15+
Add this line to your application's Gemfile:
16+
17+
```ruby
18+
gem 'async-http-faraday'
19+
```
20+
21+
And then execute:
22+
23+
$ bundle
24+
25+
Or install it yourself as:
26+
27+
$ gem install async-http-faraday
28+
29+
## Usage
30+
31+
Here is how you set faraday to use `Async::HTTP`:
32+
33+
```ruby
34+
require 'async/http/faraday'
35+
36+
# Make it the global default:
37+
Faraday.default_adapter = :async_http
38+
39+
# Per connection:
40+
conn = Faraday.new(...) do |faraday|
41+
faraday.adapter :async_http
42+
end
43+
```
44+
45+
Here is how you make a request:
46+
47+
```ruby
48+
Async::Reactor.run do
49+
response = conn.get("/index")
50+
end
51+
```
52+
53+
## Contributing
54+
55+
1. Fork it
56+
2. Create your feature branch (`git checkout -b my-new-feature`)
57+
3. Commit your changes (`git commit -am 'Add some feature'`)
58+
4. Push to the branch (`git push origin my-new-feature`)
59+
5. Create new Pull Request
60+
61+
## License
62+
63+
Released under the MIT license.
64+
65+
Copyright, 2015, by [Samuel G. D. Williams](http://www.codeotaku.com/samuel-williams).
66+
67+
Permission is hereby granted, free of charge, to any person obtaining a copy
68+
of this software and associated documentation files (the "Software"), to deal
69+
in the Software without restriction, including without limitation the rights
70+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
71+
copies of the Software, and to permit persons to whom the Software is
72+
furnished to do so, subject to the following conditions:
73+
74+
The above copyright notice and this permission notice shall be included in
75+
all copies or substantial portions of the Software.
76+
77+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
78+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
79+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
80+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
81+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
82+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
83+
THE SOFTWARE.

Rakefile

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
require "bundler/gem_tasks"
2+
require "rspec/core/rake_task"
3+
4+
RSpec::Core::RakeTask.new(:test)
5+
6+
task :default => :test
7+
8+
task :server do
9+
require 'async/reactor'
10+
require 'async/http/server'
11+
12+
app = lambda do |env|
13+
[200, {}, ["Hello World"]]
14+
end
15+
16+
server = Async::HTTP::Server.new([
17+
Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
18+
], app)
19+
20+
Async::Reactor.run do
21+
server.run
22+
end
23+
end
24+
25+
task :client do
26+
require 'async/reactor'
27+
require 'async/http/client'
28+
29+
client = Async::HTTP::Client.new([
30+
Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
31+
])
32+
33+
Async::Reactor.run do
34+
response = client.get("/")
35+
36+
puts response.inspect
37+
end
38+
end
39+
40+
task :wrk do
41+
require 'async/reactor'
42+
require 'async/http/server'
43+
44+
app = lambda do |env|
45+
[200, {}, ["Hello World"]]
46+
end
47+
48+
server = Async::HTTP::Server.new([
49+
Async::IO::Endpoint.tcp('127.0.0.1', 9294, reuse_port: true)
50+
], app)
51+
52+
process_count = Etc.nprocessors
53+
54+
pids = process_count.times.collect do
55+
fork do
56+
Async::Reactor.run do
57+
server.run
58+
end
59+
end
60+
end
61+
62+
url = "http://127.0.0.1:9294/"
63+
64+
connections = process_count
65+
system("wrk", "-c", connections.to_s, "-d", "2", "-t", connections.to_s, url)
66+
67+
pids.each do |pid|
68+
Process.kill(:KILL, pid)
69+
Process.wait pid
70+
end
71+
end

async-http-faraday.gemspec

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
2+
require_relative 'lib/async/http/faraday/version'
3+
4+
Gem::Specification.new do |spec|
5+
spec.name = "async-http-faraday"
6+
spec.version = Async::HTTP::Faraday::VERSION
7+
spec.authors = ["Samuel Williams"]
8+
spec.email = ["samuel.williams@oriontransfer.co.nz"]
9+
10+
spec.summary = "Provides an adaptor between async-http and faraday."
11+
spec.homepage = "https://github.com/socketry/async-http"
12+
13+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
14+
f.match(%r{^(test|spec|features)/})
15+
end
16+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17+
spec.require_paths = ["lib"]
18+
19+
spec.add_dependency("async-http")
20+
spec.add_dependency("faraday")
21+
22+
spec.add_development_dependency "async-rspec", "~> 1.2"
23+
24+
spec.add_development_dependency "bundler", "~> 1.3"
25+
spec.add_development_dependency "rspec", "~> 3.6"
26+
spec.add_development_dependency "rake"
27+
end

lib/async/http/faraday.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
require_relative "faraday/version"
22+
require_relative "faraday/adapter"

lib/async/http/faraday/adapter.rb

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
require 'faraday'
22+
require 'faraday/adapter'
23+
require 'async/http/client'
24+
25+
module Async
26+
module HTTP
27+
module Faraday
28+
class Adapter < ::Faraday::Adapter
29+
def call(env)
30+
super
31+
32+
client = HTTP::Client.new(endpoints_for(env).to_a)
33+
34+
response = client.send(env[:method], env[:url].request_uri, env[:request_headers], env[:body] || [])
35+
36+
puts response.inspect
37+
save_response(env, response.status, response.body, response.headers, response.reason)
38+
39+
@app.call env
40+
end
41+
42+
def endpoints_for(env)
43+
return to_enum(:endpoints_for, env) unless block_given?
44+
45+
if url = env[:url]
46+
port = url.port
47+
port ||= url.scheme == 'https' ? 443 : 80
48+
49+
endpoint = Async::IO::Endpoint.tcp(url.hostname, port)
50+
51+
if url.scheme == 'https'
52+
yield SecureEndpoint.new(endpoint, ssl_context: ssl_context_for(env[:ssl]))
53+
else
54+
yield endpoint
55+
end
56+
end
57+
end
58+
59+
def ssl_context_for(options)
60+
OpenSSL::SSL::SSLContext.new.tap do |context|
61+
context.set_params(
62+
verify_mode: options.fetch(:verify, true) ? OpenSSL::SSL::VERIFY_PEER : OpenSSL::SSL::VERIFY_NONE,
63+
# cert_store: ssl_cert_store(ssl),
64+
# cert: options[:client_cert],
65+
# key: options[:client_key],
66+
# ca_file: ssl[:ca_file],
67+
# ca_path: ssl[:ca_path],
68+
# verify_depth: ssl[:verify_depth],
69+
# version: ssl[:version],
70+
)
71+
end
72+
end
73+
end
74+
end
75+
end
76+
end
77+
78+
module Faraday
79+
class Adapter
80+
register_middleware :async_http => Async::HTTP::Faraday::Adapter
81+
end
82+
end

lib/async/http/faraday/version.rb

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Copyright, 2018, by Samuel G. D. Williams. <http://www.codeotaku.com>
2+
#
3+
# Permission is hereby granted, free of charge, to any person obtaining a copy
4+
# of this software and associated documentation files (the "Software"), to deal
5+
# in the Software without restriction, including without limitation the rights
6+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
# copies of the Software, and to permit persons to whom the Software is
8+
# furnished to do so, subject to the following conditions:
9+
#
10+
# The above copyright notice and this permission notice shall be included in
11+
# all copies or substantial portions of the Software.
12+
#
13+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
# THE SOFTWARE.
20+
21+
module Async
22+
module HTTP
23+
module Faraday
24+
VERSION = "0.1.0"
25+
end
26+
end
27+
end

0 commit comments

Comments
 (0)