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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 660378e18a953d9e32bbc4065c11838322e4c5159a8335c7640637aa9a20f195
4
- data.tar.gz: b4846e3532320c28bc5d100e4359effd6a0165fe4abf01cf866dd9d0e56fda3b
3
+ metadata.gz: 3a33b8998ea8d1bc9064fe5756119e298e054e27fc521cac42a0d9260e59b924
4
+ data.tar.gz: b18b2ea63f170e11753e516615834775facfac462084c689f3ec1d7d3467da5c
5
5
  SHA512:
6
- metadata.gz: c54d24d6cda9e8cc8938eaf96bf03af4665e1013ec5166f89e06d41709a730177ebb81b6f68fd6c5009e684fc17214841c517b8c31680f2a7860f8c49a52d4ea
7
- data.tar.gz: fbf878bc7dfabf8e36a3e69d18e5573a7e55ebc52b54dc122452416033c4718a49093cb3ca7800cefdbb42be0cf8bc174ec0d06ff8a93b217ef638ee21ae07f6
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
@@ -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.options = options
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 = { **default_options, **options } # TODO: raise on unknown 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 :options, :clients
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
@@ -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
- client = options[:client].dup
72
- client = client.timeout(timeout) if timeout
73
- client = client.basic_auth(basic_auth) if basic_auth
74
- client
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
- client = options.fetch(:client) # Use original client so that connection can be reused
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module EzClient
4
- VERSION = "0.9.1"
4
+ VERSION = "0.10.0"
5
5
  end
data/lib/ezclient.rb CHANGED
@@ -6,6 +6,7 @@ require "ezclient/client"
6
6
  require "ezclient/request"
7
7
  require "ezclient/response"
8
8
  require "ezclient/errors"
9
+ require "ezclient/check_options"
9
10
 
10
11
  module EzClient
11
12
  def self.new(*args)
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.9.1
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