rest_requestor 0.4.0 → 0.5.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: 6c16e96223b812472703d763390c9795e11058d2c27a78d35c317a8ef8a53f2e
4
- data.tar.gz: 96250ea144fb1e39f0f4621a92519eb0fec001cedf50ed82eaf670bc49517a28
3
+ metadata.gz: 0242630bd4798ef36691ab96aa6706fdfbfceb077c0234c2014800964c2b5aa0
4
+ data.tar.gz: 2b4d0e7c1e8fbd0c3dd2a343e008fb01c346a6abe7090b7c14a516393b5e824e
5
5
  SHA512:
6
- metadata.gz: 43f6a99dece90901df0b5413eb83e08a128fb77fddbd5d0674df82af223c3752d84dbeb665146e0aa96aae0c64a26b870ced5ab31d9b9a635298b41f708838a4
7
- data.tar.gz: 6cd37575e2d8e1c6fa17b4f7754f05d9791eb9612d0e8a40e883356c442d7f425034b38d0667a7208aa4f0b1ee1256a76df83c2ab7add13d96d37a3175e8ddfa
6
+ metadata.gz: d2dd190087855d8a9f415276cd53b260a6d857a70408ad5a340c18f8326de004b3d536254f2f97b1765a3448a9e4d22df29cbea6d618d636a70f609b288330ca
7
+ data.tar.gz: 0c2822b6ebeb1a1b5e7f8a2503097647d6d7d1db57ef6c6ae1553e4bfe28becca651f7e5472bdcb0f05e39067062d1ee3e16746bb0e6e9fa51fa21b38159aa47
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Scott Wright
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -17,7 +17,7 @@ class RestRequestor
17
17
  ],
18
18
  # Tier 2 — explicit path via environment variable
19
19
  [
20
- -> { (v = ENV["REST_REQUESTOR_CONFIG"]) && !v.empty? ? v : nil }
20
+ -> { ((v = ENV["REST_REQUESTOR_CONFIG"]) && !v.empty?) ? v : nil }
21
21
  ],
22
22
  # Tier 3 — project-local (most specific)
23
23
  [
@@ -36,15 +36,25 @@ class RestRequestor
36
36
  result = {}
37
37
 
38
38
  if (raw_ua = data["user_agent"] || data[:user_agent])
39
- sanitized = raw_ua.to_s.gsub(/[^A-Za-z0-9_\-]/, "_")
39
+ sanitized = raw_ua.to_s.gsub(/[^A-Za-z0-9_-]/, "_")
40
40
  result[:user_agent] = "#{sanitized}/#{VERSION}"
41
41
  end
42
42
 
43
+ if (v = data["proxy_probe_uri"] || data[:proxy_probe_uri])
44
+ result[:proxy_probe_uri] = v.to_s.strip unless v.to_s.strip.empty?
45
+ end
46
+
43
47
  if (v = data["max_retries"] || data[:max_retries])
44
48
  n = v.to_i
45
49
  result[:max_retries] = n if n > 0
46
50
  end
47
51
 
52
+ # Zero is allowed here: it turns server-error retries off
53
+ if (v = data["max_server_error_retries"] || data[:max_server_error_retries])
54
+ n = v.to_i
55
+ result[:max_server_error_retries] = n if n >= 0
56
+ end
57
+
48
58
  %i[open_timeout read_timeout].each do |key|
49
59
  if (v = data[key.to_s] || data[key])
50
60
  n = v.to_f
@@ -60,9 +70,9 @@ class RestRequestor
60
70
  path = location.call
61
71
  next unless path && File.exist?(path)
62
72
  begin
63
- data = YAML.safe_load(File.read(path))
73
+ data = YAML.safe_load_file(path)
64
74
  return data if data.is_a?(Hash)
65
- rescue ::StandardError
75
+ rescue
66
76
  next
67
77
  end
68
78
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class RestRequestor
4
- VERSION = "0.4.0"
4
+ VERSION = "0.5.0"
5
5
  end
@@ -27,16 +27,27 @@ class RestRequestor
27
27
  class RateLimitError < StandardError; end
28
28
  class NotFoundError < StandardError; end
29
29
  class ServiceUnavailableError < StandardError; end
30
+ class ServerError < StandardError; end
30
31
  class TooManyRedirectsError < ::StandardError; end
31
32
 
32
33
  attr_reader :resp, :elapsed, :options
33
34
 
35
+ # Server errors worth retrying: usually transient on the server's side
36
+ RETRYABLE_SERVER_ERRORS = [500, 502, 504].freeze
37
+ # Verbs safe to resend after a server error, which may have come after the server
38
+ # acted on the request. POST and PATCH aren't, so they raise without retrying.
39
+ IDEMPOTENT_VERBS = [:get, :put, :delete, :head].freeze
40
+ # Upper bound on a server's Retry-After, in seconds
41
+ MAX_RETRY_AFTER = 30
42
+
34
43
  OPTION_DEFAULTS = {
35
44
  max_retries: 7,
45
+ max_server_error_retries: 2,
36
46
  open_timeout: 10,
37
47
  read_timeout: 30,
38
48
  proxy_address: "127.0.0.1",
39
49
  proxy_port: 9090,
50
+ proxy_probe_uri: "https://example.com",
40
51
  user_agent: nil,
41
52
  skip_proxy: true,
42
53
  logger: nil
@@ -60,7 +71,7 @@ class RestRequestor
60
71
  warn_no_user_agent if options[:user_agent].to_s.strip.empty?
61
72
  end
62
73
 
63
- def request(uri, verb, j_params: {}, f_hash: {}, q_params: {}, auth: nil, headers: {}, body: nil, retries: 0)
74
+ def request(uri, verb, j_params: {}, f_hash: {}, q_params: {}, auth: nil, headers: {}, body: nil, retries: 0, server_retries: 0)
64
75
  unless [:get, :post, :put, :delete, :patch, :head].include?(verb)
65
76
  raise ArgumentError, "Verb must be one of :get, :post, :put, :delete, :patch, :head — got #{verb.inspect}"
66
77
  end
@@ -93,10 +104,21 @@ class RestRequestor
93
104
  options[:logger]&.info { "Retrying (#{retries}), pausing #{waitfor} seconds" }
94
105
  sleep waitfor
95
106
  # q_params already incorporated into uri above; omit here to avoid double-encoding
96
- request(uri, verb, j_params: j_params, f_hash: f_hash, auth: auth, headers: headers, body: body, retries: retries)
107
+ request(uri, verb, j_params: j_params, f_hash: f_hash, auth: auth, headers: headers, body: body, retries: retries, server_retries: server_retries)
97
108
  else
98
109
  raise ServiceUnavailableError.new(uri, verb, resp, j_params)
99
110
  end
111
+ when *RETRYABLE_SERVER_ERRORS
112
+ if IDEMPOTENT_VERBS.include?(verb) && server_retries < options[:max_server_error_retries]
113
+ server_retries += 1
114
+ waitfor = retry_after || retry_delay(server_retries)
115
+ options[:logger]&.info { "#{resp.code} from server, retrying (#{server_retries}), pausing #{waitfor} seconds" }
116
+ sleep waitfor
117
+ request(uri, verb, j_params: j_params, f_hash: f_hash, auth: auth, headers: headers, body: body, retries: retries, server_retries: server_retries)
118
+ else
119
+ raise ServerError.new(uri, verb, resp, j_params)
120
+ end
121
+ when 500...600 then raise ServerError.new(uri, verb, resp, j_params)
100
122
  else
101
123
  raise StandardError.new(uri, verb, resp, j_params)
102
124
  end
@@ -132,6 +154,14 @@ class RestRequestor
132
154
  2**retries + rand(0..(2**retries / 2.0))
133
155
  end
134
156
 
157
+ # The response's Retry-After in seconds, if it gives one (the HTTP-date form is ignored)
158
+ def retry_after
159
+ value = resp["Retry-After"].to_s.strip
160
+ return unless value.match?(/\A\d+\z/)
161
+
162
+ [value.to_i, MAX_RETRY_AFTER].min
163
+ end
164
+
135
165
  def add_query_string(uri, q_params)
136
166
  separator = uri.include?("?") ? "&" : "?"
137
167
  uri + separator + URI.encode_www_form(q_params)
@@ -161,12 +191,12 @@ class RestRequestor
161
191
  http = build_http(uri_obj)
162
192
 
163
193
  request_class = case verb
164
- when :get then Net::HTTP::Get
165
- when :post then Net::HTTP::Post
166
- when :put then Net::HTTP::Put
194
+ when :get then Net::HTTP::Get
195
+ when :post then Net::HTTP::Post
196
+ when :put then Net::HTTP::Put
167
197
  when :delete then Net::HTTP::Delete
168
- when :patch then Net::HTTP::Patch
169
- when :head then Net::HTTP::Head
198
+ when :patch then Net::HTTP::Patch
199
+ when :head then Net::HTTP::Head
170
200
  end
171
201
 
172
202
  request_path = uri_obj.path
@@ -218,7 +248,7 @@ class RestRequestor
218
248
  end
219
249
 
220
250
  begin
221
- uri = URI("https://httpbin.org/ip")
251
+ uri = URI(options[:proxy_probe_uri])
222
252
  http = Net::HTTP.new(uri.host, uri.port, options[:proxy_address], options[:proxy_port])
223
253
  http.use_ssl = true
224
254
  http.verify_mode = OpenSSL::SSL::VERIFY_PEER
metadata CHANGED
@@ -1,44 +1,46 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest_requestor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
- - Scott Wright
7
+ - Scott Wright
8
8
  bindir: bin
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies: []
12
- description: Wraps Net::HTTP with JSON handling, exponential backoff on 503 errors,
13
- optional proxy support, and typed error classes.
12
+ description: "Wraps Net::HTTP with JSON handling, exponential backoff on 503 errors, optional proxy support, and typed error classes."
14
13
  email:
15
- - scott@wrightzone.com
14
+ - scott@wrightzone.com
16
15
  executables: []
17
16
  extensions: []
18
17
  extra_rdoc_files: []
19
18
  files:
20
- - lib/rest_requestor.rb
21
- - lib/rest_requestor/config.rb
22
- - lib/rest_requestor/version.rb
23
- homepage: https://codeberg.org/jswright61/rest_requestor
19
+ - LICENSE
20
+ - lib/rest_requestor.rb
21
+ - lib/rest_requestor/config.rb
22
+ - lib/rest_requestor/version.rb
23
+ homepage: "https://codeberg.org/jswright61/rest_requestor"
24
24
  licenses:
25
- - MIT
25
+ - MIT
26
26
  metadata: {}
27
27
  rdoc_options: []
28
28
  require_paths:
29
- - lib
29
+ - lib
30
30
  required_ruby_version: !ruby/object:Gem::Requirement
31
31
  requirements:
32
- - - ">="
33
- - !ruby/object:Gem::Version
34
- version: '3.0'
32
+ -
33
+ - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "3.0"
35
36
  required_rubygems_version: !ruby/object:Gem::Requirement
36
37
  requirements:
37
- - - ">="
38
- - !ruby/object:Gem::Version
39
- version: '0'
38
+ -
39
+ - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
40
42
  requirements: []
41
- rubygems_version: 4.0.10
43
+ rubygems_version: 4.1.0
42
44
  specification_version: 4
43
- summary: HTTP client with retry support, proxy awareness, and structured errors
45
+ summary: "HTTP client with retry support, proxy awareness, and structured errors"
44
46
  test_files: []