ezclient 0.5.0 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -0
- data/lib/ezclient/client.rb +54 -0
- data/lib/ezclient/errors.rb +11 -0
- data/lib/ezclient/request.rb +10 -0
- data/lib/ezclient/version.rb +2 -2
- data/lib/ezclient.rb +5 -42
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0218155bce003ba4d2c5002c78398553b3ae4097954f07a9f78d9f615a87bd28'
|
4
|
+
data.tar.gz: a3e75129937eadc69b9dd798b4726cc1735c777b11c838c250632a779f710005
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3f50a99a73be9d7608cd08d7b47ff8af15923a590967c1519d9f2df9ab0f03de1ba935cfc98bfeeede06cf963e0ec0eea2a24efc226750cfcfeb836682d26c56
|
7
|
+
data.tar.gz: d993d191ecd78bc05fa66f2f77c0cff899cba18f1589f6ea31c22cab87f554ee743883249bd129675b77d49db6aa0ce8c92139fa3a8ec39a6794a3d4ec3dc043
|
data/README.md
CHANGED
@@ -0,0 +1,54 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class EzClient::Client
|
4
|
+
def initialize(options = {})
|
5
|
+
self.options = options
|
6
|
+
self.clients = {}
|
7
|
+
end
|
8
|
+
|
9
|
+
def request(verb, url, **options)
|
10
|
+
options = { **default_options, **options }
|
11
|
+
|
12
|
+
keep_alive_timeout = options.delete(:keep_alive)
|
13
|
+
api_auth = options.delete(:api_auth)
|
14
|
+
|
15
|
+
if keep_alive_timeout
|
16
|
+
client = persistent_client_for(url, timeout: keep_alive_timeout)
|
17
|
+
else
|
18
|
+
client = HTTP::Client.new
|
19
|
+
end
|
20
|
+
|
21
|
+
EzClient::Request.new(verb, url, client: client, **options).tap do |request|
|
22
|
+
request.api_auth!(*api_auth) if api_auth
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def perform(*args)
|
27
|
+
request(*args).perform
|
28
|
+
end
|
29
|
+
|
30
|
+
def perform!(*args)
|
31
|
+
request(*args).perform!
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
attr_accessor :options, :clients
|
37
|
+
|
38
|
+
def persistent_client_for(url, timeout: 600)
|
39
|
+
uri = HTTP::URI.parse(url)
|
40
|
+
clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
|
41
|
+
end
|
42
|
+
|
43
|
+
def default_options
|
44
|
+
{
|
45
|
+
api_auth: options[:api_auth],
|
46
|
+
keep_alive: options[:keep_alive],
|
47
|
+
max_retries: options[:max_retries],
|
48
|
+
on_complete: options[:on_complete],
|
49
|
+
on_error: options[:on_error],
|
50
|
+
retry_exceptions: options[:retry_exceptions],
|
51
|
+
timeout: options[:default_timeout],
|
52
|
+
}
|
53
|
+
end
|
54
|
+
end
|
data/lib/ezclient/request.rb
CHANGED
@@ -20,6 +20,16 @@ class EzClient::Request
|
|
20
20
|
raise error
|
21
21
|
end
|
22
22
|
|
23
|
+
def perform!
|
24
|
+
response = perform
|
25
|
+
|
26
|
+
if response.error?
|
27
|
+
raise EzClient::ResponseStatusError, response
|
28
|
+
else
|
29
|
+
response
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
23
33
|
def api_auth!(*args)
|
24
34
|
raise "ApiAuth gem is not loaded" unless defined?(ApiAuth)
|
25
35
|
ApiAuth.sign!(http_request, *args)
|
data/lib/ezclient/version.rb
CHANGED
data/lib/ezclient.rb
CHANGED
@@ -2,50 +2,13 @@
|
|
2
2
|
|
3
3
|
require "http"
|
4
4
|
require "ezclient/version"
|
5
|
+
require "ezclient/client"
|
5
6
|
require "ezclient/request"
|
6
7
|
require "ezclient/response"
|
8
|
+
require "ezclient/errors"
|
7
9
|
|
8
|
-
|
9
|
-
def
|
10
|
-
|
11
|
-
self.clients = {}
|
12
|
-
end
|
13
|
-
|
14
|
-
def request(verb, url, **options)
|
15
|
-
options = { **default_options, **options }
|
16
|
-
|
17
|
-
keep_alive_timeout = options.delete(:keep_alive)
|
18
|
-
api_auth = options.delete(:api_auth)
|
19
|
-
|
20
|
-
if keep_alive_timeout
|
21
|
-
client = persistent_client_for(url, timeout: keep_alive_timeout)
|
22
|
-
else
|
23
|
-
client = HTTP::Client.new
|
24
|
-
end
|
25
|
-
|
26
|
-
Request.new(verb, url, client: client, **options).tap do |request|
|
27
|
-
request.api_auth!(*api_auth) if api_auth
|
28
|
-
end
|
29
|
-
end
|
30
|
-
|
31
|
-
private
|
32
|
-
|
33
|
-
attr_accessor :options, :clients
|
34
|
-
|
35
|
-
def persistent_client_for(url, timeout: 600)
|
36
|
-
uri = HTTP::URI.parse(url)
|
37
|
-
clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
|
38
|
-
end
|
39
|
-
|
40
|
-
def default_options
|
41
|
-
{
|
42
|
-
api_auth: options[:api_auth],
|
43
|
-
keep_alive: options[:keep_alive],
|
44
|
-
max_retries: options[:max_retries],
|
45
|
-
on_complete: options[:on_complete],
|
46
|
-
on_error: options[:on_error],
|
47
|
-
retry_exceptions: options[:retry_exceptions],
|
48
|
-
timeout: options[:default_timeout],
|
49
|
-
}
|
10
|
+
module EzClient
|
11
|
+
def self.new(*args)
|
12
|
+
Client.new(*args)
|
50
13
|
end
|
51
14
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ezclient
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuri Smirnov
|
@@ -153,6 +153,8 @@ files:
|
|
153
153
|
- Rakefile
|
154
154
|
- ezclient.gemspec
|
155
155
|
- lib/ezclient.rb
|
156
|
+
- lib/ezclient/client.rb
|
157
|
+
- lib/ezclient/errors.rb
|
156
158
|
- lib/ezclient/request.rb
|
157
159
|
- lib/ezclient/response.rb
|
158
160
|
- lib/ezclient/version.rb
|