rest-easy 1.4.1 → 1.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: a30780e0b38e31f1c183d1ec0a91ea93658a78e67d2415aeb8aef279d03359b7
4
- data.tar.gz: 2ed7aafbcaf5e0c6fd463992a65293d1f064c693611f64b9b3256e8e4b518fdf
3
+ metadata.gz: c047221a492782c10e7531864861b44537f867a5c87411311ce1d7ff1233e515
4
+ data.tar.gz: 0bcb2f88739589f5d906bf629550a8d2293a516ea0d799e52bd360e716aa44e6
5
5
  SHA512:
6
- metadata.gz: 3ac9ea37e26b652d5fe590075d7668f70e2cf8b7290b02d7e4dd1f227bf7a7e26ea9e5faa02ed1b84b5706262d90e774db52381ea0ce2d026da40d410ae38c11
7
- data.tar.gz: 8f8773d0cbacc6366daade22ceee18ee76de91ee5eb5cd718c392e0abfe1ed3a911658a867dab1bcd1f21b077f2296e0cb5ffbb1aa5597f6fee7e77a7b40b84d
6
+ metadata.gz: e09ac325027fbcc7d13e0d1520fc26db758f6cd77701ce69a249bfed78db2a910b1e61aa3abb1e7284f21d2a0080e60766fa84222940aafadcf304fb9ce27c62
7
+ data.tar.gz: 430830b51e4c3e5a658ebd73a374d3bb2db419cd996a455221ad6e1bcfd2e3d8af07141f9fe76a8b7cfe5e66dc28abf6084459e78d3ce61ea0e2c2a75cacc2e8
data/CHANGELOG.md CHANGED
@@ -2,6 +2,48 @@
2
2
 
3
3
  ## [Unreleased]
4
4
 
5
+ ## [1.5.0] - 2026-11-17
6
+
7
+ ### Added
8
+
9
+ - **`retry_transient_errors` setting** (default `false`). When on, 429
10
+ responses, and 5xx responses, timeouts and dropped connections on GET, PUT
11
+ and DELETE, are retried up to `max_retries` without calling the
12
+ authentication's `on_rejected`, which then only sees the other failures.
13
+ These retries wait for the response's `Retry-After` header (seconds or an
14
+ HTTP date, 1 second without one or without a response); one asking for
15
+ more than 10 seconds raises instead. A retry that `on_rejected` asks for
16
+ is still made at once. Off by default, since PUT and DELETE are only safe
17
+ to repeat on APIs that implement them idempotently.
18
+
19
+ ## [1.4.2] - 2026-09-23
20
+
21
+ ### Fixed
22
+
23
+ - **`update` no longer discards state it should carry forward.** It rebuilt
24
+ the instance's metadata and its change set from scratch instead of
25
+ inheriting them, which broke three things at once:
26
+
27
+ - An instance that had never been saved came back marked as persisted, so
28
+ `save` took the update branch: a PUT to the collection path with no id in
29
+ it, for a record the API has never seen, instead of a POST that creates
30
+ one. Building a record and adjusting it before the first save is the
31
+ ordinary way to use an immutable resource, and it could not produce a
32
+ correct create request.
33
+ - Chained updates kept only the last call's fields. After
34
+ `a.update(x: 1).update(y: 2)`, `__changes__` reported just `y` while `x`
35
+ read back as `1` on the instance, so a partial payload built from the
36
+ change set left out a field the object claimed to carry, and nothing
37
+ raised.
38
+ - Metadata set by a `before_parse` hook lives on the instance, not the
39
+ class, and was dropped. A consumer flagging parsed instances — to mark
40
+ the thin records a collection endpoint returns, say — lost the flag as
41
+ soon as anything called `update`.
42
+
43
+ `update` now inherits the receiver's metadata and merges its change set.
44
+ The metadata is copied rather than shared, so mutating an updated copy's
45
+ metadata still cannot reach the original's.
46
+
5
47
  ## [1.4.1] - 2026-08-18
6
48
 
7
49
  ### Fixed
@@ -167,7 +209,9 @@
167
209
 
168
210
  Initial release.
169
211
 
170
- [Unreleased]: https://github.com/accodeing/rest-easy/compare/v1.4.1...HEAD
212
+ [Unreleased]: https://github.com/accodeing/rest-easy/compare/v1.5.0...HEAD
213
+ [1.5.0]: https://github.com/accodeing/rest-easy/compare/v1.4.2...v1.5.0
214
+ [1.4.2]: https://github.com/accodeing/rest-easy/compare/v1.4.1...v1.4.2
171
215
  [1.4.1]: https://github.com/accodeing/rest-easy/compare/v1.4.0...v1.4.1
172
216
  [1.4.0]: https://github.com/accodeing/rest-easy/compare/v1.3.1...v1.4.0
173
217
  [1.3.1]: https://github.com/accodeing/rest-easy/compare/v1.3.0...v1.3.1
data/README.md CHANGED
@@ -101,6 +101,7 @@ end
101
101
  | `log_bodies` | `false` | When `true`, request/response bodies are logged. Off by default to avoid leaking domain secrets |
102
102
  | `logger` | `nil` | When set, attaches Faraday's logger middleware and writes HTTP request/response details to it |
103
103
  | `max_retries` | `3` | Retry count on request failure |
104
+ | `retry_transient_errors` | `false` | When `true`, 429 responses, and 5xx responses, timeouts and dropped connections on GET, PUT and DELETE, are retried without calling `on_rejected`; see [the retry lifecycle](#custom-authentication) |
104
105
 
105
106
  ### Logging HTTP traffic
106
107
 
@@ -655,6 +656,14 @@ The retry lifecycle:
655
656
  - Return normally → retry (up to `max_retries`)
656
657
  - Raise → propagate error
657
658
 
659
+ With `retry_transient_errors` on, a 429 skips step 3 and is retried directly, up to `max_retries`. So is a 5xx on GET, PUT or DELETE. A 5xx on POST still goes through step 3, because the server may already have acted on it, and repeating a POST that creates something can create it twice.
660
+
661
+ A timeout or a dropped connection (`Faraday::TimeoutError`, `Faraday::ConnectionFailed`) has no response to pass to `on_rejected`, so it is normally raised straight away. With the setting on, it is retried on GET, PUT or DELETE in the same way, for the same reason. On POST it is still raised.
662
+
663
+ Before each of these retries the request waits for as long as the response's `Retry-After` header says, either in seconds or as an HTTP date, and 1 second without one or without a response. If `Retry-After` asks for more than 10 seconds the error is raised straight away, since retrying sooner than the server allows would only fail again. A retry that `on_rejected` asks for, such as after a 5xx on POST, is made straight away, as it is without the setting.
664
+
665
+ It is off by default because a PUT or DELETE is only safe to repeat when the API implements them idempotently. Turn it on for APIs that do.
666
+
658
667
  ## CRUD operations
659
668
 
660
669
  Resources provide standard CRUD methods:
@@ -6,6 +6,11 @@ module RestEasy
6
6
  @data = { new: new_record, saved: saved, **defaults }
7
7
  end
8
8
 
9
+ def initialize_copy(source)
10
+ super
11
+ @data = @data.dup
12
+ end
13
+
9
14
  def new?
10
15
  @data[:new]
11
16
  end
@@ -598,7 +598,7 @@ module RestEasy
598
598
 
599
599
  new_model = @model_attributes.merge(coerced)
600
600
  new_instance = self.class.allocate
601
- new_instance.send(:init_from_update, new_model, @api_data, coerced)
601
+ new_instance.send(:init_from_update, new_model, @api_data, __changes__.merge(coerced), @meta)
602
602
  new_instance
603
603
  end
604
604
 
@@ -801,11 +801,14 @@ module RestEasy
801
801
  end
802
802
  end
803
803
 
804
- def init_from_update(new_model_attrs, original_api_data, changes)
804
+ def init_from_update(new_model_attrs, original_api_data, changes, meta)
805
805
  @api_data = original_api_data
806
806
  @model_attributes = new_model_attrs
807
807
  @changes = changes
808
- @meta = Meta.new(new_record: false, saved: true, **self.class.metadata)
808
+ # Carried from the receiver, not rebuilt: an instance that was never
809
+ # saved stays new, so `save` still POSTs it, and metadata a parse hook
810
+ # set (which lives on the instance, not the class) survives the update.
811
+ @meta = meta.dup
809
812
  end
810
813
  end
811
814
  end
@@ -12,6 +12,7 @@ module RestEasy
12
12
  setting :log_bodies, default: false, reader: true
13
13
  setting :logger, reader: true
14
14
  setting :max_retries, default: 3, reader: true
15
+ setting :retry_transient_errors, default: false, reader: true
15
16
 
16
17
  setting :conversions do
17
18
  setting :json_attributes, default: Conventions::DEFAULT, reader: true
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module RestEasy
4
- VERSION = "1.4.1"
4
+ VERSION = "1.5.0"
5
5
  end
data/lib/rest_easy.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rubygems"
4
+ require "time"
4
5
  require "dry/types"
5
6
  require "faraday"
6
7
  require "zeitwerk"
@@ -17,6 +18,9 @@ module RestEasy
17
18
  # Make Boolean available as a bare type constant (Ruby has no built-in Boolean)
18
19
  ::Object.const_set(:Boolean, Dry::Types["params.bool"]) unless defined?(::Boolean)
19
20
 
21
+ DEFAULT_RETRY_AFTER_IN_SECONDS = 1
22
+ MAX_RETRY_AFTER_IN_SECONDS = 10
23
+
20
24
  # ── Error hierarchy ──────────────────────────────────────────────────
21
25
 
22
26
  class Error < StandardError; end
@@ -69,6 +73,9 @@ module RestEasy
69
73
  STANDARD_AUTH_HEADERS = %w[Authorization Proxy-Authorization Cookie Set-Cookie].freeze
70
74
  private_constant :STANDARD_AUTH_HEADERS
71
75
 
76
+ IDEMPOTENT_METHODS = %i[get put delete].freeze
77
+ private_constant :IDEMPOTENT_METHODS
78
+
72
79
  def config
73
80
  self::Settings.config
74
81
  end
@@ -166,13 +173,50 @@ module RestEasy
166
173
  rescue RequestError => e
167
174
  attempts += 1
168
175
  if attempts <= max_retries
169
- auth.on_rejected(e.response)
176
+ if config.retry_transient_errors && transient?(method, e.response)
177
+ wait = retry_after(e.response)
178
+ raise if wait > MAX_RETRY_AFTER_IN_SECONDS
179
+
180
+ Kernel.sleep(wait)
181
+ else
182
+ auth.on_rejected(e.response)
183
+ end
170
184
  retry
171
185
  end
172
186
  raise
187
+
188
+ rescue Faraday::TimeoutError, Faraday::ConnectionFailed
189
+ attempts += 1
190
+ raise unless attempts <= max_retries && config.retry_transient_errors && IDEMPOTENT_METHODS.include?(method)
191
+
192
+ Kernel.sleep(DEFAULT_RETRY_AFTER_IN_SECONDS)
193
+ retry
173
194
  end
174
195
  end
175
196
 
197
+ # A 429 means the server did not act on the request, so any method can be
198
+ # repeated. A 5xx may come after it did, so only idempotent ones are.
199
+ def transient?(method, response)
200
+ return false unless response
201
+ return true if response.status == 429
202
+
203
+ (500..599).cover?(response.status) && IDEMPOTENT_METHODS.include?(method)
204
+ end
205
+
206
+ def retry_after(response)
207
+ value = response.headers["Retry-After"]
208
+ return DEFAULT_RETRY_AFTER_IN_SECONDS unless value
209
+
210
+ seconds = Integer(value, 10, exception: false) || seconds_until(value) || DEFAULT_RETRY_AFTER_IN_SECONDS
211
+ seconds.clamp(0..)
212
+ end
213
+
214
+ def seconds_until(http_date)
215
+ (Time.httpdate(http_date) - Time.now).ceil
216
+ rescue ArgumentError
217
+ nil
218
+ end
219
+
176
220
  end
177
221
 
178
222
  # ── Type bridge ─────────────────────────────────────────────────────
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rest-easy
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.1
4
+ version: 1.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jonas Schubert Erlandsson
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2026-08-18 00:00:00.000000000 Z
13
+ date: 2026-11-17 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: dry-types