ezclient 0.9.1 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/lib/ezclient/check_options.rb +11 -0
- data/lib/ezclient/client.rb +18 -19
- data/lib/ezclient/request.rb +26 -7
- data/lib/ezclient/version.rb +1 -1
- data/lib/ezclient.rb +1 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3a33b8998ea8d1bc9064fe5756119e298e054e27fc521cac42a0d9260e59b924
|
4
|
+
data.tar.gz: b18b2ea63f170e11753e516615834775facfac462084c689f3ec1d7d3467da5c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 8357a3071ed41858d52f27639fb6c72c90915d5fe46a10da91ec59bd711ef577f8b2e2970d2e6d364276321c1502f6b768ad557bd8a2f1ff52e283ce323e9c6d
|
7
|
+
data.tar.gz: dd8116c2553ee074b89b9a18d3595899f3e54a4adaf8e65dec8384a011a2b8de3a783277eb066dd5ccdb5059786e2cfaa580c9269374006f8b6fe0b79f2fbde4
|
@@ -0,0 +1,11 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module EzClient::CheckOptions
|
4
|
+
def self.call(options, allowed_keys)
|
5
|
+
if (options.keys - allowed_keys).any?
|
6
|
+
raise ArgumentError, "Unrecognized options: #{options.keys.map(&:inspect).join(", ")}"
|
7
|
+
end
|
8
|
+
|
9
|
+
options
|
10
|
+
end
|
11
|
+
end
|
data/lib/ezclient/client.rb
CHANGED
@@ -1,13 +1,28 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class EzClient::Client
|
4
|
+
REQUEST_OPTION_KEYS = %i[
|
5
|
+
api_auth
|
6
|
+
basic_auth
|
7
|
+
headers
|
8
|
+
keep_alive
|
9
|
+
max_retries
|
10
|
+
on_complete
|
11
|
+
on_error
|
12
|
+
on_retry
|
13
|
+
retry_exceptions
|
14
|
+
ssl_context
|
15
|
+
timeout
|
16
|
+
].freeze
|
17
|
+
|
4
18
|
def initialize(options = {})
|
5
|
-
self.
|
19
|
+
self.request_options = options
|
6
20
|
self.clients = {}
|
21
|
+
EzClient::CheckOptions.call(options, REQUEST_OPTION_KEYS)
|
7
22
|
end
|
8
23
|
|
9
24
|
def request(verb, url, **options)
|
10
|
-
options = { **
|
25
|
+
options = { **request_options, **options }
|
11
26
|
|
12
27
|
keep_alive_timeout = options.delete(:keep_alive)
|
13
28
|
api_auth = options.delete(:api_auth)
|
@@ -33,26 +48,10 @@ class EzClient::Client
|
|
33
48
|
|
34
49
|
private
|
35
50
|
|
36
|
-
attr_accessor :
|
51
|
+
attr_accessor :request_options, :clients
|
37
52
|
|
38
53
|
def persistent_client_for(url, timeout: 600)
|
39
54
|
uri = HTTP::URI.parse(url)
|
40
55
|
clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
|
41
56
|
end
|
42
|
-
|
43
|
-
def default_options
|
44
|
-
keys = %i[
|
45
|
-
api_auth
|
46
|
-
keep_alive
|
47
|
-
max_retries
|
48
|
-
on_complete
|
49
|
-
on_error
|
50
|
-
retry_exceptions
|
51
|
-
ssl_context
|
52
|
-
timeout
|
53
|
-
]
|
54
|
-
|
55
|
-
# RUBY25: Hash#slice
|
56
|
-
options.select { |key| keys.include?(key) }
|
57
|
-
end
|
58
57
|
end
|
data/lib/ezclient/request.rb
CHANGED
@@ -1,12 +1,23 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
class EzClient::Request
|
4
|
+
OPTION_KEYS = %i[
|
5
|
+
body
|
6
|
+
form
|
7
|
+
json
|
8
|
+
metadata
|
9
|
+
params
|
10
|
+
query
|
11
|
+
].freeze
|
12
|
+
|
4
13
|
attr_accessor :verb, :url, :options
|
5
14
|
|
6
15
|
def initialize(verb, url, options)
|
7
16
|
self.verb = verb.to_s.upcase
|
8
17
|
self.url = url
|
18
|
+
self.client = options.delete(:client)
|
9
19
|
self.options = options
|
20
|
+
EzClient::CheckOptions.call(options, OPTION_KEYS + EzClient::Client::REQUEST_OPTION_KEYS)
|
10
21
|
end
|
11
22
|
|
12
23
|
def perform
|
@@ -53,6 +64,8 @@ class EzClient::Request
|
|
53
64
|
|
54
65
|
private
|
55
66
|
|
67
|
+
attr_accessor :client
|
68
|
+
|
56
69
|
def http_request
|
57
70
|
@http_request ||= begin
|
58
71
|
# RUBY25: Hash#slice
|
@@ -68,10 +81,10 @@ class EzClient::Request
|
|
68
81
|
def http_client
|
69
82
|
# Only used to build proper HTTP::Request and HTTP::Options instances
|
70
83
|
@http_client ||= begin
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
84
|
+
http_client = client.dup
|
85
|
+
http_client = http_client.timeout(timeout) if timeout
|
86
|
+
http_client = http_client.basic_auth(basic_auth) if basic_auth
|
87
|
+
http_client
|
75
88
|
end
|
76
89
|
end
|
77
90
|
|
@@ -80,12 +93,13 @@ class EzClient::Request
|
|
80
93
|
|
81
94
|
begin
|
82
95
|
retry_on_connection_error do
|
83
|
-
|
96
|
+
# Use original client so that connection can be reused
|
84
97
|
client.perform(http_request, http_options)
|
85
98
|
end
|
86
|
-
rescue *retried_exceptions
|
99
|
+
rescue *retried_exceptions => error
|
87
100
|
if retries < max_retries.to_i
|
88
101
|
retries += 1
|
102
|
+
on_retry.call(self, error, options[:metadata])
|
89
103
|
retry
|
90
104
|
else
|
91
105
|
raise
|
@@ -97,7 +111,8 @@ class EzClient::Request
|
|
97
111
|
# This may result in 2 requests reaching the server so I hope HTTP fixes it
|
98
112
|
# https://github.com/httprb/http/issues/459
|
99
113
|
yield
|
100
|
-
rescue HTTP::ConnectionError
|
114
|
+
rescue HTTP::ConnectionError => error
|
115
|
+
on_retry.call(self, error, options[:metadata])
|
101
116
|
yield
|
102
117
|
end
|
103
118
|
|
@@ -113,6 +128,10 @@ class EzClient::Request
|
|
113
128
|
options[:on_error] || proc {}
|
114
129
|
end
|
115
130
|
|
131
|
+
def on_retry
|
132
|
+
options[:on_retry] || proc {}
|
133
|
+
end
|
134
|
+
|
116
135
|
def retried_exceptions
|
117
136
|
Array(options[:retry_exceptions])
|
118
137
|
end
|
data/lib/ezclient/version.rb
CHANGED
data/lib/ezclient.rb
CHANGED
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.10.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yuri Smirnov
|
@@ -153,6 +153,7 @@ files:
|
|
153
153
|
- Rakefile
|
154
154
|
- ezclient.gemspec
|
155
155
|
- lib/ezclient.rb
|
156
|
+
- lib/ezclient/check_options.rb
|
156
157
|
- lib/ezclient/client.rb
|
157
158
|
- lib/ezclient/errors.rb
|
158
159
|
- lib/ezclient/request.rb
|