grape-idempotency 0.1.3 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +25 -12
- data/README.md +131 -4
- data/lib/grape/idempotency/helpers.rb +2 -2
- data/lib/grape/idempotency/middleware/error.rb +6 -2
- data/lib/grape/idempotency/version.rb +1 -1
- data/lib/grape/idempotency.rb +92 -39
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 756cff52967b35d996913c3d682847a4cd362c2cbd4a95d8bf766c2f037d86ce
|
4
|
+
data.tar.gz: bf87694a06895bfd09749676574a078f71b698f59c23211a438112b22f332f06
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: bdaaa014a0a39fdbba3f850bdddbd27db5fca791f543609c4a847615eaeeac52d0dfb16e642d1310915bbdada5b46267291a1e43fec70299a027c5a9848285b2
|
7
|
+
data.tar.gz: 2030a6016fcbaf64667b22a2be43af70239381e6d3311571e45c172d439bb67222ca57f2566becd847eb867d8ae9750ddcef70e1dd2444492191e2924f27e7cd
|
data/CHANGELOG.md
CHANGED
@@ -4,31 +4,44 @@ All changes to `grape-idempotency` will be documented in this file.
|
|
4
4
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
5
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
6
|
|
7
|
-
## [0.
|
7
|
+
## [1.0.0] - 2023-11-23
|
8
|
+
|
9
|
+
### Changed
|
10
|
+
|
11
|
+
* [#11](https://github.com/jcagarcia/grape-idempotency/pull/11): Changing error response formats - [@Flip120](https://github.com/Flip120).
|
12
|
+
* [#16](https://github.com/jcagarcia/grape-idempotency/pull/16): Changing error response code to 422 for conflict - [@Flip120](https://github.com/Flip120).
|
13
|
+
|
14
|
+
### Feature
|
15
|
+
|
16
|
+
* [#11](https://github.com/jcagarcia/grape-idempotency/pull/11): Return 409 conflict when a request is still being processed - [@Flip120](https://github.com/Flip120).
|
17
|
+
* [#15](https://github.com/jcagarcia/grape-idempotency/pull/15): Allow to mark the idempotent header as required - [@jcagarcia](https://github.com/jcagarcia).
|
18
|
+
* [#17](https://github.com/jcagarcia/grape-idempotency/pull/17): Allow to configure logger - [@jcagarcia](https://github.com/jcagarcia).
|
19
|
+
|
20
|
+
## [0.1.3] - 2023-11-07
|
8
21
|
|
9
22
|
### Fix
|
10
23
|
|
11
|
-
- Second calls were returning `null` when the first response was generated inside a `rescue_from`.
|
12
|
-
- Conflict response had invalid format.
|
24
|
+
* [#9](https://github.com/jcagarcia/grape-idempotency/pull/9): Second calls were returning `null` when the first response was generated inside a `rescue_from`. - [@jcagarcia](https://github.com/jcagarcia).
|
25
|
+
- [#9](https://github.com/jcagarcia/grape-idempotency/pull/9): Conflict response had invalid format. - [@jcagarcia](https://github.com/jcagarcia).
|
13
26
|
|
14
|
-
## [0.1.2] - 2023-
|
27
|
+
## [0.1.2] - 2023-11-06
|
15
28
|
|
16
29
|
### Fix
|
17
30
|
|
18
|
-
- Return correct original response when the endpoint returns a hash in the body
|
31
|
+
* [#5](https://github.com/jcagarcia/grape-idempotency/pull/5): Return correct original response when the endpoint returns a hash in the body - [@jcagarcia](https://github.com/jcagarcia).
|
19
32
|
|
20
|
-
## [0.1.1] - 2023-
|
33
|
+
## [0.1.1] - 2023-11-06
|
21
34
|
|
22
35
|
### Fix
|
23
36
|
|
24
|
-
- Return `409 - Conflict` response if idempotency key is provided for same query and body parameters BUT different endpoints.
|
25
|
-
- Use `nx: true` when storing the original request in the Redis storage for only setting the key if it does not already exist.
|
37
|
+
* [#4](https://github.com/jcagarcia/grape-idempotency/pull/4): Return `409 - Conflict` response if idempotency key is provided for same query and body parameters BUT different endpoints. - [@jcagarcia](https://github.com/jcagarcia).
|
38
|
+
* [#4](https://github.com/jcagarcia/grape-idempotency/pull/4): Use `nx: true` when storing the original request in the Redis storage for only setting the key if it does not already exist. - [@jcagarcia](https://github.com/jcagarcia).
|
26
39
|
|
27
40
|
### Changed
|
28
41
|
|
29
|
-
- Include `idempotency-key` in the response headers
|
30
|
-
|
42
|
+
* [#4](https://github.com/jcagarcia/grape-idempotency/pull/4): Include `idempotency-key` in the response headers - [@jcagarcia](https://github.com/jcagarcia).
|
43
|
+
* In the case of a concurrency error when storing the request into the redis storage (because now `nx: true`), a new idempotency key will be generated, so the consumer can check the new one seeing the headers.
|
31
44
|
|
32
|
-
## [0.1.0] - 2023-
|
45
|
+
## [0.1.0] - 2023-11-03
|
33
46
|
|
34
|
-
- Initial version
|
47
|
+
* [#1](https://github.com/jcagarcia/grape-idempotency/pull/1): Initial version - [@jcagarcia](https://github.com/jcagarcia).
|
data/README.md
CHANGED
@@ -5,11 +5,14 @@
|
|
5
5
|
|
6
6
|
Gem for supporting idempotency in your [Grape](https://github.com/ruby-grape/grape) APIs.
|
7
7
|
|
8
|
+
Implementation based on the Active Internet-Draft [draft-ietf-httpapi-idempotency-key-header-04](https://datatracker.ietf.org/doc/draft-ietf-httpapi-idempotency-key-header/04/)
|
9
|
+
|
8
10
|
Topics covered in this README:
|
9
11
|
|
10
12
|
- [Installation](#installation-)
|
11
13
|
- [Basic Usage](#basic-usage-)
|
12
14
|
- [How it works](#how-it-works-)
|
15
|
+
- [Making idempotency key header mandatory](#making-idempotency-key-header-mandatory-)
|
13
16
|
- [Configuration](#configuration-)
|
14
17
|
- [Changelog](#changelog)
|
15
18
|
- [Contributing](#contributing)
|
@@ -63,7 +66,7 @@ That's all! 🚀
|
|
63
66
|
|
64
67
|
## How it works 🤔
|
65
68
|
|
66
|
-
Once you've set up the gem and enclosed your endpoint code within the `idempotent` method, your endpoint will exhibit idempotent behavior, but this will only occur if the consumer of the endpoint includes an idempotency key in their request.
|
69
|
+
Once you've set up the gem and enclosed your endpoint code within the `idempotent` method, your endpoint will exhibit idempotent behavior, but this will only occur if the consumer of the endpoint includes an idempotency key in their request. (If you want to make the idempotency key header mandatory for your endpoint, check [How to make idempotency key header mandatory](#making-idempotency-key-header-mandatory-) section)
|
67
70
|
|
68
71
|
This key allows your consumer to make the same request again in case of a connection error, without the risk of creating a duplicate object or executing the update twice.
|
69
72
|
|
@@ -71,12 +74,45 @@ To execute an idempotent request, simply request your user to include an extra `
|
|
71
74
|
|
72
75
|
This gem operates by storing the initial request's status code and response body, regardless of whether the request succeeded or failed, using a specific idempotency key. Subsequent requests with the same key will consistently yield the same result, even if there were 500 errors.
|
73
76
|
|
74
|
-
Keys are automatically removed from the system if they are at least 24 hours old, and a new request is generated when a key is reused after the original has been removed. The idempotency layer compares incoming parameters to those of the original request and returns a `
|
77
|
+
Keys are automatically removed from the system if they are at least 24 hours old, and a new request is generated when a key is reused after the original has been removed. The idempotency layer compares incoming parameters to those of the original request and returns a `422 - Unprocessable Entity` status code if they don't match, preventing accidental misuse.
|
78
|
+
If a request is received while another one with the same idempotency key is still being processed the idempotency layer returns a `409 - Conflict` status
|
75
79
|
|
76
80
|
Results are only saved if an API endpoint begins its execution. If incoming parameters fail validation or if the request conflicts with another one executing concurrently, no idempotent result is stored because no API endpoint has initiated execution. In such cases, retrying these requests is safe.
|
77
81
|
|
78
82
|
Additionally, this gem automatically appends the `Original-Request` header and the `Idempotency-Key` header to your API's response, enabling you to trace back to the initial request that generated that specific response.
|
79
83
|
|
84
|
+
## Making idempotency key header mandatory ⚠️
|
85
|
+
|
86
|
+
For some endpoints, you want to enforce your consumers to provide idempotency key. So, when wrapping the code inside the `idempotent` method, you can mark it as `required`:
|
87
|
+
|
88
|
+
```ruby
|
89
|
+
require 'grape'
|
90
|
+
require 'grape-idempotency'
|
91
|
+
|
92
|
+
class API < Grape::API
|
93
|
+
post '/payments' do
|
94
|
+
idempotent(required: true) do
|
95
|
+
status 201
|
96
|
+
Payment.create!({
|
97
|
+
amount: params[:amount]
|
98
|
+
})
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
```
|
104
|
+
|
105
|
+
If the Idempotency-Key request header is missing for a idempotent operation requiring this header, the gem will reply with an HTTP 400 status code with the following body:
|
106
|
+
|
107
|
+
```json
|
108
|
+
{
|
109
|
+
"title": "Idempotency-Key is missing",
|
110
|
+
"detail": "This operation is idempotent and it requires correct usage of Idempotency Key.",
|
111
|
+
}
|
112
|
+
```
|
113
|
+
|
114
|
+
If you want to change the error message returned in this scenario, check [How to configure idempotency key missing error message](#mandatory_header_response) section.
|
115
|
+
|
80
116
|
## Configuration 🪚
|
81
117
|
|
82
118
|
In addition to the storage aspect, you have the option to supply additional configuration details to tailor the gem to the specific requirements of your project.
|
@@ -124,6 +160,40 @@ end
|
|
124
160
|
|
125
161
|
In the case above, you request your consumers to use the `X-Trace-Id: <trace-id>` header when requesting your API.
|
126
162
|
|
163
|
+
### logger, logger_level and logger_prefix
|
164
|
+
|
165
|
+
By default, the logger used by the gem is configured like `Logger.new(STDOUT)` and `INFO` level. As this gem does not log any message with `INFO` level, only `ERROR` messages will be logged.
|
166
|
+
|
167
|
+
|
168
|
+
If you want to provide your own logger, you want to change the level to `DEBUG` or you want to provide your own prefix, you can configure the gem like:
|
169
|
+
|
170
|
+
```ruby
|
171
|
+
Grape::Idempotency.configure do |c|
|
172
|
+
c.logger = Infrastructure::MyLogger.new
|
173
|
+
c.logger_level = :debug
|
174
|
+
c.logger_prefix = '[my-own-prefix]'
|
175
|
+
end
|
176
|
+
```
|
177
|
+
|
178
|
+
An example of the logged information when changing the level of the log to `DEBUG` and customizing the `logger_prefix`:
|
179
|
+
|
180
|
+
```shell
|
181
|
+
I, [2023-11-23T22:41:39.148163 #1] DEBUG -- : [my-own-prefix] Performing endpoint "/payments" with idempotency.
|
182
|
+
I, [2023-11-23T22:41:39.148176 #1] DEBUG -- : [my-own-prefix] Idempotency key is NOT mandatory for this endpoint.
|
183
|
+
I, [2023-11-23T22:41:39.148192 #1] DEBUG -- : [my-own-prefix] Idempotency key received in request header "x-custom-idempotency-key" => "fd77c9d6-b7da-4966-aac8-40ee258f24aa"
|
184
|
+
I, [2023-11-23T22:41:39.148210 #1] DEBUG -- : [my-own-prefix] Previous request information has NOT been found for the provided idempotency key.
|
185
|
+
I, [2023-11-23T22:41:39.148248 #1] DEBUG -- : [my-own-prefix] Request stored as processing.
|
186
|
+
I, [2023-11-23T22:41:39.148261 #1] DEBUG -- : [my-own-prefix] Performing the provided block.
|
187
|
+
I, [2023-11-23T22:41:39.148268 #1] DEBUG -- : [my-own-prefix] Block has been performed.
|
188
|
+
I, [2023-11-23T22:41:39.148287 #1] DEBUG -- : [my-own-prefix] Storing response.
|
189
|
+
I, [2023-11-23T22:41:39.148317 #1] DEBUG -- : [my-own-prefix] Response stored.
|
190
|
+
I, [2023-11-23T22:41:39.148473 #1] DEBUG -- : [my-own-prefix] Performing endpoint "/payments" with idempotency.
|
191
|
+
I, [2023-11-23T22:41:39.148486 #1] DEBUG -- : [my-own-prefix] Idempotency key is NOT mandatory for this endpoint.
|
192
|
+
I, [2023-11-23T22:41:39.148502 #1] DEBUG -- : [my-own-prefix] Idempotency key received in request header "x-custom-idempotency-key" => "fd77c9d6-b7da-4966-aac8-40ee258f24aa"
|
193
|
+
I, [2023-11-23T22:41:39.148523 #1] DEBUG -- : [my-own-prefix] Request has been found for the provided idempotency key => {"path"=>"/payments", "params"=>{"locale"=>"undefined", "{\"amount\":10000}"=>nil}, "status"=>500, "original_request"=>"wadus", "response"=>"{\"error\":\"Internal Server Error\"}"}
|
194
|
+
I, [2023-11-23T22:41:39.148537 #1] DEBUG -- : [my-own-prefix] Returning the response from the original request.
|
195
|
+
```
|
196
|
+
|
127
197
|
### conflict_error_response
|
128
198
|
|
129
199
|
When providing a `Idempotency-Key: <key>` header, this gem compares incoming parameters to those of the original request (if exists) and returns a `409 - Conflict` status code if they don't match, preventing accidental misuse. The response body returned by the gem looks like:
|
@@ -131,7 +201,8 @@ When providing a `Idempotency-Key: <key>` header, this gem compares incoming par
|
|
131
201
|
```json
|
132
202
|
{
|
133
203
|
|
134
|
-
"
|
204
|
+
"title": "Idempotency-Key is already used",
|
205
|
+
"detail": "This operation is idempotent and it requires correct usage of Idempotency Key. Idempotency Key MUST not be reused across different payloads of this operation."
|
135
206
|
}
|
136
207
|
```
|
137
208
|
|
@@ -151,6 +222,61 @@ end
|
|
151
222
|
|
152
223
|
In the configuration above, the error is following the [RFC-7807](https://datatracker.ietf.org/doc/html/rfc7807) format.
|
153
224
|
|
225
|
+
### processing_response
|
226
|
+
|
227
|
+
When a request with a `Idempotency-Key: <key>` header is performed while a previous one still on going with the same idempotency value, this gem returns a `409 - Conflict` status. The response body returned by the gem looks like:
|
228
|
+
|
229
|
+
```json
|
230
|
+
{
|
231
|
+
|
232
|
+
"title": "A request is outstanding for this Idempotency-Key",
|
233
|
+
"detail": "A request with the same idempotent key for the same operation is being processed or is outstanding."
|
234
|
+
}
|
235
|
+
```
|
236
|
+
|
237
|
+
You have the option to specify the desired response body to be returned to your users when this error occurs. This allows you to align the error format with the one used in your application.
|
238
|
+
|
239
|
+
```ruby
|
240
|
+
Grape::Idempotency.configure do |c|
|
241
|
+
c.storage = @storage
|
242
|
+
c.processing_response = {
|
243
|
+
"type": "about:blank",
|
244
|
+
"status": 409,
|
245
|
+
"title": "A request is still being processed",
|
246
|
+
"detail": "A request with the same idempotent key is being procesed"
|
247
|
+
}
|
248
|
+
end
|
249
|
+
```
|
250
|
+
|
251
|
+
In the configuration above, the error is following the [RFC-7807](https://datatracker.ietf.org/doc/html/rfc7807) format.
|
252
|
+
|
253
|
+
### mandatory_header_response
|
254
|
+
|
255
|
+
If the Idempotency-Key request header is missing for a idempotent operation requiring this header, the gem will reply with an HTTP 400 status code with the following body:
|
256
|
+
|
257
|
+
```json
|
258
|
+
{
|
259
|
+
"title": "Idempotency-Key is missing",
|
260
|
+
"detail": "This operation is idempotent and it requires correct usage of Idempotency Key.",
|
261
|
+
}
|
262
|
+
```
|
263
|
+
|
264
|
+
You have the option to specify the desired response body to be returned to your users when this error occurs. This allows you to align the error format with the one used in your application.
|
265
|
+
|
266
|
+
```ruby
|
267
|
+
Grape::Idempotency.configure do |c|
|
268
|
+
c.storage = @storage
|
269
|
+
c.mandatory_header_response = {
|
270
|
+
"type": "about:blank",
|
271
|
+
"status": 400,
|
272
|
+
"title": "Idempotency-Key is missing",
|
273
|
+
"detail": "Please, provide a valid idempotent key in the headers for performing this operation"
|
274
|
+
}
|
275
|
+
end
|
276
|
+
```
|
277
|
+
|
278
|
+
In the configuration above, the error is following the [RFC-7807](https://datatracker.ietf.org/doc/html/rfc7807) format.
|
279
|
+
|
154
280
|
## Changelog
|
155
281
|
|
156
282
|
If you're interested in seeing the changes and bug fixes between each version of `grape-idempotency`, read the [Changelog](https://github.com/jcagarcia/grape-idempotency/blob/main/CHANGELOG.md).
|
@@ -193,4 +319,5 @@ Open issues on the GitHub issue tracker with clear information.
|
|
193
319
|
|
194
320
|
### Contributors
|
195
321
|
|
196
|
-
*
|
322
|
+
* Juan Carlos García - Creator - https://github.com/jcagarcia
|
323
|
+
* Carlos Cabanero - Contributor - https://github.com/Flip120
|
@@ -3,14 +3,18 @@ require 'grape/middleware/base'
|
|
3
3
|
module Grape
|
4
4
|
module Middleware
|
5
5
|
class Error < Base
|
6
|
-
def run_rescue_handler(handler, error)
|
6
|
+
def run_rescue_handler(handler, error, endpoint=nil)
|
7
7
|
if handler.instance_of?(Symbol)
|
8
8
|
raise NoMethodError, "undefined method '#{handler}'" unless respond_to?(handler)
|
9
9
|
|
10
10
|
handler = public_method(handler)
|
11
11
|
end
|
12
12
|
|
13
|
-
|
13
|
+
if endpoint
|
14
|
+
response = handler.arity.zero? ? endpoint.instance_exec(&handler) : endpoint.instance_exec(error, &handler)
|
15
|
+
else
|
16
|
+
response = handler.arity.zero? ? instance_exec(&handler) : instance_exec(error, &handler)
|
17
|
+
end
|
14
18
|
|
15
19
|
if response.is_a?(Rack::Response)
|
16
20
|
update_idempotency_error_with(error, response)
|
data/lib/grape/idempotency.rb
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
require 'grape'
|
2
|
+
require 'logger'
|
2
3
|
require 'securerandom'
|
3
4
|
require 'grape/idempotency/version'
|
4
5
|
require 'grape/idempotency/middleware/error'
|
@@ -21,45 +22,79 @@ module Grape
|
|
21
22
|
@configuration = clean_configuration
|
22
23
|
end
|
23
24
|
|
24
|
-
def idempotent(grape, &block)
|
25
|
+
def idempotent(grape, required: false, &block)
|
25
26
|
validate_config!
|
27
|
+
log(:debug, "Performing endpoint \"#{grape.request.path}\" with idempotency.")
|
28
|
+
log(:debug, "Idempotency key is #{!required ? 'NOT' : ''} mandatory for this endpoint.")
|
26
29
|
|
27
30
|
idempotency_key = get_idempotency_key(grape.request.headers)
|
28
|
-
|
31
|
+
log(:debug, "Idempotency key received in request header \"#{configuration.idempotency_key_header.downcase}\" => \"#{idempotency_key}\"") if idempotency_key
|
32
|
+
|
33
|
+
grape.error!(configuration.mandatory_header_response, 400) if required && !idempotency_key
|
34
|
+
return block.call if !idempotency_key
|
29
35
|
|
30
36
|
cached_request = get_from_cache(idempotency_key)
|
37
|
+
log(:debug, "Request has been found for the provided idempotency key => #{cached_request}") if cached_request
|
31
38
|
if cached_request && (cached_request["params"] != grape.request.params || cached_request["path"] != grape.request.path)
|
32
|
-
grape.
|
33
|
-
|
39
|
+
log(:debug, "Request has conflicts. Same params? => #{cached_request["params"] != grape.request.params}. Same path? => #{cached_request["path"] != grape.request.path}")
|
40
|
+
log(:debug, "Returning conflict error response.")
|
41
|
+
grape.error!(configuration.conflict_error_response, 422)
|
42
|
+
elsif cached_request && cached_request["processing"] == true
|
43
|
+
log(:debug, "Returning processing error response.")
|
44
|
+
grape.error!(configuration.processing_response, 409)
|
34
45
|
elsif cached_request
|
46
|
+
log(:debug, "Returning the response from the original request.")
|
35
47
|
grape.status cached_request["status"]
|
36
48
|
grape.header(ORIGINAL_REQUEST_HEADER, cached_request["original_request"])
|
37
49
|
grape.header(configuration.idempotency_key_header, idempotency_key)
|
38
50
|
return cached_request["response"]
|
39
51
|
end
|
40
52
|
|
53
|
+
log(:debug, "Previous request information has NOT been found for the provided idempotency key.")
|
54
|
+
|
55
|
+
original_request_id = get_request_id(grape.request.headers)
|
56
|
+
success = store_processing_request(idempotency_key, grape.request.path, grape.request.params, original_request_id)
|
57
|
+
if !success
|
58
|
+
log(:error, "Request NOT stored as processing. Concurrent requests for the same idempotency key appeared.")
|
59
|
+
grape.error!(configuration.processing_response, 409)
|
60
|
+
else
|
61
|
+
log(:debug, "Request stored as processing.")
|
62
|
+
end
|
63
|
+
|
64
|
+
log(:debug, "Performing the provided block.")
|
65
|
+
|
41
66
|
response = catch(:error) do
|
42
67
|
block.call
|
43
68
|
end
|
44
69
|
|
45
|
-
|
70
|
+
log(:debug, "Block has been performed.")
|
71
|
+
|
72
|
+
if is_an_error?(response)
|
73
|
+
log(:debug, "An error response was returned by the performed block. => #{response}")
|
74
|
+
response = response[:message]
|
75
|
+
end
|
46
76
|
|
47
|
-
original_request_id = get_request_id(grape.request.headers)
|
48
77
|
grape.header(ORIGINAL_REQUEST_HEADER, original_request_id)
|
49
78
|
grape.body response
|
50
79
|
rescue => e
|
80
|
+
log(:debug, "An unexpected error was raised when performing the block.")
|
51
81
|
if !cached_request && !response
|
52
82
|
validate_config!
|
83
|
+
log(:debug, "Storing error response.")
|
53
84
|
original_request_id = get_request_id(grape.request.headers)
|
54
85
|
stored_key = store_error_request(idempotency_key, grape.request.path, grape.request.params, grape.status, original_request_id, e)
|
86
|
+
log(:debug, "Error response stored.")
|
55
87
|
grape.header(ORIGINAL_REQUEST_HEADER, original_request_id)
|
56
88
|
grape.header(configuration.idempotency_key_header, stored_key)
|
57
89
|
end
|
90
|
+
log(:debug, "Re-raising the error.")
|
58
91
|
raise
|
59
92
|
ensure
|
60
93
|
if !cached_request && response
|
61
94
|
validate_config!
|
62
|
-
|
95
|
+
log(:debug, "Storing response.")
|
96
|
+
stored_key = store_request_response(idempotency_key, grape.request.path, grape.request.params, grape.status, original_request_id, response)
|
97
|
+
log(:debug, "Response stored.")
|
63
98
|
grape.header(configuration.idempotency_key_header, stored_key)
|
64
99
|
end
|
65
100
|
end
|
@@ -76,18 +111,18 @@ module Grape
|
|
76
111
|
params = request_with_unmanaged_error["params"]
|
77
112
|
original_request_id = request_with_unmanaged_error["original_request"]
|
78
113
|
|
79
|
-
|
114
|
+
store_request_response(idempotency_key, path, params, status, original_request_id, response)
|
80
115
|
storage.del(stored_error[:error_key])
|
81
116
|
end
|
82
117
|
|
83
118
|
private
|
84
119
|
|
85
120
|
def validate_config!
|
86
|
-
storage
|
121
|
+
raise Configuration::Error.new("A Redis instance must be configured as cache storage") unless valid_storage?
|
122
|
+
end
|
87
123
|
|
88
|
-
|
89
|
-
|
90
|
-
end
|
124
|
+
def valid_storage?
|
125
|
+
configuration.storage && configuration.storage.respond_to?(:set)
|
91
126
|
end
|
92
127
|
|
93
128
|
def get_idempotency_key(headers)
|
@@ -113,22 +148,29 @@ module Grape
|
|
113
148
|
JSON.parse(value)
|
114
149
|
end
|
115
150
|
|
116
|
-
def
|
151
|
+
def store_processing_request(idempotency_key, path, params, request_id)
|
152
|
+
body = {
|
153
|
+
path: path,
|
154
|
+
params: params,
|
155
|
+
original_request: request_id,
|
156
|
+
processing: true
|
157
|
+
}
|
158
|
+
|
159
|
+
storage.set(key(idempotency_key), body.to_json, ex: configuration.expires_in, nx: true)
|
160
|
+
end
|
161
|
+
|
162
|
+
def store_request_response(idempotency_key, path, params, status, request_id, response)
|
117
163
|
body = {
|
118
164
|
path: path,
|
119
165
|
params: params,
|
120
166
|
status: status,
|
121
167
|
original_request: request_id,
|
122
168
|
response: response
|
123
|
-
}
|
169
|
+
}
|
124
170
|
|
125
|
-
|
171
|
+
storage.set(key(idempotency_key), body.to_json, ex: configuration.expires_in, nx: false)
|
126
172
|
|
127
|
-
|
128
|
-
return store_request(random_idempotency_key, path, params, status, request_id, response)
|
129
|
-
else
|
130
|
-
return idempotency_key
|
131
|
-
end
|
173
|
+
idempotency_key
|
132
174
|
end
|
133
175
|
|
134
176
|
def store_error_request(idempotency_key, path, params, status, request_id, error)
|
@@ -143,13 +185,9 @@ module Grape
|
|
143
185
|
}
|
144
186
|
}.to_json
|
145
187
|
|
146
|
-
|
188
|
+
storage.set(error_key(idempotency_key), body, ex: 30, nx: false)
|
147
189
|
|
148
|
-
|
149
|
-
return store_error_request(random_idempotency_key, path, params, status, request_id, error)
|
150
|
-
else
|
151
|
-
return idempotency_key
|
152
|
-
end
|
190
|
+
idempotency_key
|
153
191
|
end
|
154
192
|
|
155
193
|
def get_error_request_for(error)
|
@@ -191,37 +229,52 @@ module Grape
|
|
191
229
|
"grape:idempotency:"
|
192
230
|
end
|
193
231
|
|
194
|
-
def random_idempotency_key
|
195
|
-
tentative_key = SecureRandom.uuid
|
196
|
-
already_existing_key = storage.get(key(tentative_key))
|
197
|
-
if already_existing_key
|
198
|
-
return random_idempotency_key
|
199
|
-
else
|
200
|
-
return tentative_key
|
201
|
-
end
|
202
|
-
end
|
203
|
-
|
204
232
|
def storage
|
205
233
|
configuration.storage
|
206
234
|
end
|
207
235
|
|
236
|
+
def log(level, msg)
|
237
|
+
logger.send(level, "#{configuration.logger_prefix} #{msg}")
|
238
|
+
end
|
239
|
+
|
240
|
+
def logger
|
241
|
+
return @logger if @logger
|
242
|
+
|
243
|
+
@logger = configuration.logger
|
244
|
+
@logger.level = configuration.logger_level
|
245
|
+
@logger
|
246
|
+
end
|
247
|
+
|
208
248
|
def configuration
|
209
249
|
@configuration ||= Configuration.new
|
210
250
|
end
|
211
251
|
end
|
212
252
|
|
213
253
|
class Configuration
|
214
|
-
attr_accessor :storage, :
|
254
|
+
attr_accessor :storage, :logger, :logger_level, :logger_prefix, :expires_in, :idempotency_key_header,
|
255
|
+
:request_id_header, :conflict_error_response, :processing_response, :mandatory_header_response
|
215
256
|
|
216
257
|
class Error < StandardError; end
|
217
258
|
|
218
259
|
def initialize
|
219
260
|
@storage = nil
|
261
|
+
@logger = Logger.new(STDOUT)
|
262
|
+
@logger_level = Logger::INFO
|
263
|
+
@logger_prefix = "[grape-idempotency]"
|
220
264
|
@expires_in = 216_000
|
221
265
|
@idempotency_key_header = "idempotency-key"
|
222
266
|
@request_id_header = "x-request-id"
|
223
|
-
@conflict_error_response = {
|
224
|
-
"
|
267
|
+
@conflict_error_response = {
|
268
|
+
"title" => "Idempotency-Key is already used",
|
269
|
+
"detail" => "This operation is idempotent and it requires correct usage of Idempotency Key. Idempotency Key MUST not be reused across different payloads of this operation."
|
270
|
+
}
|
271
|
+
@processing_response = {
|
272
|
+
"title" => "A request is outstanding for this Idempotency-Key",
|
273
|
+
"detail" => "A request with the same idempotent key for the same operation is being processed or is outstanding."
|
274
|
+
}
|
275
|
+
@mandatory_header_response = {
|
276
|
+
"title" => "Idempotency-Key is missing",
|
277
|
+
"detail" => "This operation is idempotent and it requires correct usage of Idempotency Key."
|
225
278
|
}
|
226
279
|
end
|
227
280
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: grape-idempotency
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Juan Carlos García
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-11-
|
11
|
+
date: 2023-11-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: grape
|