rest-client-adapters 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +14 -0
- data/Gemfile +5 -0
- data/LICENSE +22 -0
- data/README.md +56 -0
- data/Rakefile +1 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rest-client-adapters.rb +3 -0
- data/lib/rest-client-adapters/configuration.rb +9 -0
- data/lib/rest-client-adapters/em_http_request.rb +114 -0
- data/lib/rest-client-adapters/request.rb +22 -0
- data/rest-client-adapters.gemspec +27 -0
- metadata +142 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: c0c1369c1493151a996a53f2469318d1a529285a94235c47b6bb6ef0cac9d72a
|
4
|
+
data.tar.gz: 1d0db1de1079149064d4d752c3ae747bb0da2d57ca9b711e098b02a6c698eb50
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e541a160ebe85b36d0641f28d274f819e330125aa0dd56a3c8d5dc7e7254727eca073372709cfefacb7b6013da146660839e44394eac7ee45b36a96ebfec9ac6
|
7
|
+
data.tar.gz: 414fab0835b57c0bbfa6fd66a6a81b0172f17fa8ab552c09b6bcf3bfc542ce6f2c7379f37682e2ad8c9667d172b888b9cac4b01f2b91aaafe67efb4d801bf800
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2016 Railsware (www.railsware.com)
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
# RestClient Adapters [![Build Status](https://travis-ci.org/railsware/rest-client-adapters.svg?branch=master)](https://travis-ci.org/railsware/rest-client-adapters)
|
2
|
+
|
3
|
+
|
4
|
+
This gem allows to use different HTTP implementation in RestClient with next adapters:
|
5
|
+
|
6
|
+
* `:net_http` - Net::HTTP (default, blocking I/O)
|
7
|
+
* `:em_http` - EventMachine::HttpRequest (non-blocking I/0)
|
8
|
+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'rest-client-adapters'
|
16
|
+
```
|
17
|
+
|
18
|
+
And then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
Or install it yourself as:
|
23
|
+
|
24
|
+
$ gem install rest-client-adapters
|
25
|
+
|
26
|
+
## Usage
|
27
|
+
|
28
|
+
|
29
|
+
You may specify adapter for each request:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
RestClient.get('https://www.google.com')
|
33
|
+
RestClient.get('https://www.google.com', adapter: :em_http)
|
34
|
+
```
|
35
|
+
|
36
|
+
Or you may specify adapter globaly:
|
37
|
+
|
38
|
+
```ruby
|
39
|
+
RestClient.adapter = :em_http
|
40
|
+
RestClient.get('https://www.google.com')
|
41
|
+
```
|
42
|
+
|
43
|
+
## Notes
|
44
|
+
|
45
|
+
When EventMachine is already running we assume that you are responsible for Fiber allocation.
|
46
|
+
You can add `Rack::FiberPool` to your application middleware and it automatically provides a Fiber for each incoming HTTP request. It creates pool of Fibers and re-use fiber for each incoming HTTP request. Also you can control the connection pool size.
|
47
|
+
|
48
|
+
## Authors
|
49
|
+
|
50
|
+
* [Andriy Yanko](http://ayanko.github.io)
|
51
|
+
|
52
|
+
## References
|
53
|
+
|
54
|
+
* https://github.com/rest-client/rest-client
|
55
|
+
* https://github.com/igrigorik/em-http-request
|
56
|
+
* https://github.com/alebsack/rack-fiber_pool
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "em/rest/client"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,114 @@
|
|
1
|
+
require 'em-http'
|
2
|
+
require 'fiber'
|
3
|
+
|
4
|
+
module RestClient
|
5
|
+
class EmHttpRequest < Request
|
6
|
+
|
7
|
+
def initialize(args)
|
8
|
+
raise ArgumentError, ':block_response option is not supported' if args[:block_response]
|
9
|
+
super(args)
|
10
|
+
end
|
11
|
+
|
12
|
+
def execute(&block)
|
13
|
+
uri = parse_url_with_auth!(url)
|
14
|
+
connection = build_connection(uri)
|
15
|
+
client = transmit(connection, &block)
|
16
|
+
raise client.error if client.error
|
17
|
+
net_http_response = build_net_http_response(client)
|
18
|
+
process_result(net_http_response, &block)
|
19
|
+
ensure
|
20
|
+
payload.close if payload
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
def transmit(connection, &block)
|
26
|
+
client = nil
|
27
|
+
if EM.reactor_running?
|
28
|
+
client = sync_transmit(connection, &block)
|
29
|
+
else
|
30
|
+
EM.run do
|
31
|
+
Fiber.new do
|
32
|
+
client = sync_transmit(connection, &block)
|
33
|
+
EM.stop
|
34
|
+
end.resume
|
35
|
+
end
|
36
|
+
end
|
37
|
+
client
|
38
|
+
end
|
39
|
+
|
40
|
+
def sync_transmit(connection, &block)
|
41
|
+
client = build_client(connection)
|
42
|
+
if client.error
|
43
|
+
client
|
44
|
+
else
|
45
|
+
fiber = Fiber.current
|
46
|
+
client.callback { fiber.resume(client) }
|
47
|
+
client.errback { fiber.resume(client) }
|
48
|
+
Fiber.yield
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def build_connection(uri)
|
53
|
+
uri = uri.dup
|
54
|
+
uri.user = nil
|
55
|
+
uri.password = nil
|
56
|
+
EM::HttpRequest.new(uri, build_connection_options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def build_client(connection)
|
60
|
+
connection.setup_request(method, build_request_options)
|
61
|
+
end
|
62
|
+
|
63
|
+
def build_connection_options
|
64
|
+
options = {}
|
65
|
+
|
66
|
+
if connect_timeout = open_timeout
|
67
|
+
options[:connect_timeout] = connect_timeout
|
68
|
+
end
|
69
|
+
|
70
|
+
if inactivity_timeout = read_timeout
|
71
|
+
options[:inactivity_timeout] = inactivity_timeout
|
72
|
+
end
|
73
|
+
|
74
|
+
if proxy_uri
|
75
|
+
options[:proxy] = {
|
76
|
+
host: proxy_uri.host,
|
77
|
+
port: proxy_uri.port,
|
78
|
+
authorization: [proxy_uri.user, proxy_uri.password]
|
79
|
+
}
|
80
|
+
end
|
81
|
+
|
82
|
+
options
|
83
|
+
end
|
84
|
+
|
85
|
+
def build_request_options
|
86
|
+
options = {}
|
87
|
+
options[:decoding] = false
|
88
|
+
options[:head] = processed_headers
|
89
|
+
if user
|
90
|
+
options[:head]['authorization'] = [user, password]
|
91
|
+
end
|
92
|
+
options[:body] = payload.to_s
|
93
|
+
options
|
94
|
+
end
|
95
|
+
|
96
|
+
def build_net_http_response(client)
|
97
|
+
klass = Net::HTTPResponse.send(
|
98
|
+
:response_class,
|
99
|
+
client.response_header.status.to_s
|
100
|
+
)
|
101
|
+
response = klass.new(
|
102
|
+
client.response_header.http_version,
|
103
|
+
client.response_header.status.to_s,
|
104
|
+
client.response_header.http_reason
|
105
|
+
)
|
106
|
+
client.response_header.raw.each do |k, v|
|
107
|
+
response.add_field(k, v)
|
108
|
+
end
|
109
|
+
response.body = client.response
|
110
|
+
response.instance_variable_set :@read, true
|
111
|
+
response
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module RestClient
|
2
|
+
class Request
|
3
|
+
class << self
|
4
|
+
def execute(args, &block)
|
5
|
+
adapter_name = args.fetch(:adapter, RestClient.adapter)
|
6
|
+
klass = get_adapter_klass(adapter_name)
|
7
|
+
klass.new(args).execute(&block)
|
8
|
+
end
|
9
|
+
|
10
|
+
def get_adapter_klass(name)
|
11
|
+
case name
|
12
|
+
when :net_http
|
13
|
+
Request
|
14
|
+
when :em_http
|
15
|
+
EmHttpRequest
|
16
|
+
else
|
17
|
+
raise NotImplementedError, "Unsupported adapter: #{adapter.inspect}"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "rest-client-adapters"
|
7
|
+
spec.version = "0.2.0"
|
8
|
+
spec.authors = ["Andriy Yanko"]
|
9
|
+
spec.email = ["andriy.yanko@gmail.com"]
|
10
|
+
|
11
|
+
spec.summary = %q{RestClient Adapters}
|
12
|
+
spec.homepage = "https://github.com/railsware/rest-client-adapters"
|
13
|
+
spec.license = "MIT"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_development_dependency "rest-client", ">= 2.0"
|
21
|
+
spec.add_development_dependency "em-http-request", "~> 1.1"
|
22
|
+
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.9"
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.4.0"
|
26
|
+
spec.add_development_dependency "webmock", "~> 3.0"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,142 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rest-client-adapters
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Andriy Yanko
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rest-client
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: em-http-request
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.1'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.1'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: bundler
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '1.9'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '1.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '10.0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '10.0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.4.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.4.0
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: webmock
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '3.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '3.0'
|
97
|
+
description:
|
98
|
+
email:
|
99
|
+
- andriy.yanko@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CHANGELOG.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- lib/rest-client-adapters.rb
|
115
|
+
- lib/rest-client-adapters/configuration.rb
|
116
|
+
- lib/rest-client-adapters/em_http_request.rb
|
117
|
+
- lib/rest-client-adapters/request.rb
|
118
|
+
- rest-client-adapters.gemspec
|
119
|
+
homepage: https://github.com/railsware/rest-client-adapters
|
120
|
+
licenses:
|
121
|
+
- MIT
|
122
|
+
metadata: {}
|
123
|
+
post_install_message:
|
124
|
+
rdoc_options: []
|
125
|
+
require_paths:
|
126
|
+
- lib
|
127
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - ">="
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
requirements: []
|
138
|
+
rubygems_version: 3.0.3
|
139
|
+
signing_key:
|
140
|
+
specification_version: 4
|
141
|
+
summary: RestClient Adapters
|
142
|
+
test_files: []
|