idempotent-request 0.1.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 +7 -0
- data/.gitignore +12 -0
- data/.rspec +2 -0
- data/.travis.yml +13 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +146 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/idempotent-request.gemspec +31 -0
- data/lib/idempotent-request.rb +5 -0
- data/lib/idempotent-request/middleware.rb +39 -0
- data/lib/idempotent-request/redis_storage.rb +29 -0
- data/lib/idempotent-request/request.rb +33 -0
- data/lib/idempotent-request/request_manager.rb +56 -0
- data/lib/version.rb +3 -0
- metadata +159 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: 97567b4fefdeb4f2519df13c738a9922c47dcef9
|
|
4
|
+
data.tar.gz: 95e8440f7d1ed1d84d0b86876609b0a2bd24b14f
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 746a79896a310f59f79179c78459ed25195c30c01a695b759fc0288db9f0ac8502d70f1506e8d97acf3fb208793d665cf875f83b05f614a80ec39620893ab09c
|
|
7
|
+
data.tar.gz: 8c333a9db17087beb9f3b7bde8bdb31f68c80951eed7b77afc90e681f462520ff6797c7f5a56bcba4dafad30fff5cd9555f1b30ec27f89786ac8fd5b8613d8d6
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
sudo: false
|
|
2
|
+
language: ruby
|
|
3
|
+
rvm:
|
|
4
|
+
- 2.4.2
|
|
5
|
+
deploy:
|
|
6
|
+
skip_cleanup: true
|
|
7
|
+
provider: rubygems
|
|
8
|
+
api_key:
|
|
9
|
+
secure: JOZUQM6qAYJN/N0k4UmV3jBSKMaW3oMYiZ4boPrGm7MsUY8ETHXSYR+dDhrWOvDR4Wdr6DVbTmMtXkWJHaQaM/LkrjN2FM6mTuDfmhkcvi9VBOMstptwXeHMS9fWeogtQwHyd+pKac9vUlaMhRYyoeYyJ1i6/LHua4NftZZ8oPu/9kMuKDD3Rj8zPJNui+fGcZxq90XdxjafCfRJu7EnzSZXzVi5msaEBKcUVxsHxprBjyjmp/yh/Wn0GLyBkYYXkKEUNx9DhxVtWaNG2FfOzznj3HLXQgBdz1or1tDxG8iIegC5jEj4G+gLtldLhMYWfHDaEr4iqKLF1LkjO9TK0RjcVzGNHNxCTQaDDcjgimcVNozWg707IzT5Ap6jG4Y9JWk5KY4ysOaVFqhemafoDwQcXkPb+39N/tpeBPCByCVNrZ+5lgWaHvs+iDv0X9PyAFs5nTz6/u6bz7GDZ91oJOGs9OW6szzHwkQn5TN4omdGiOFca7lviz6OyjXeSn+a6whU2DsRXxp+omrPT/gELLGl21Wd3GGTpiGAdnu/vwvoKOZqGfkr8HS/Bozc0S/vafFJ5KoPAkNQ9iwxPgL1xcEbSF5uK67T2EYVKg7z4psXDeWGBWzk8657SBCB1VCgujkmhg+lWt0TGgrckupKSyUXL4/nnAECgizSLJSbgRE=
|
|
10
|
+
gem: idempotent-request
|
|
11
|
+
on:
|
|
12
|
+
tags: true
|
|
13
|
+
repo: qonto/idempotent-request
|
data/CODE_OF_CONDUCT.md
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
|
2
|
+
|
|
3
|
+
## Our Pledge
|
|
4
|
+
|
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
|
10
|
+
orientation.
|
|
11
|
+
|
|
12
|
+
## Our Standards
|
|
13
|
+
|
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
|
15
|
+
include:
|
|
16
|
+
|
|
17
|
+
* Using welcoming and inclusive language
|
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
|
19
|
+
* Gracefully accepting constructive criticism
|
|
20
|
+
* Focusing on what is best for the community
|
|
21
|
+
* Showing empathy towards other community members
|
|
22
|
+
|
|
23
|
+
Examples of unacceptable behavior by participants include:
|
|
24
|
+
|
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
|
26
|
+
advances
|
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
|
28
|
+
* Public or private harassment
|
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
|
30
|
+
address, without explicit permission
|
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
|
32
|
+
professional setting
|
|
33
|
+
|
|
34
|
+
## Our Responsibilities
|
|
35
|
+
|
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
|
38
|
+
response to any instances of unacceptable behavior.
|
|
39
|
+
|
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
|
44
|
+
threatening, offensive, or harmful.
|
|
45
|
+
|
|
46
|
+
## Scope
|
|
47
|
+
|
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
|
49
|
+
when an individual is representing the project or its community. Examples of
|
|
50
|
+
representing a project or community include using an official project e-mail
|
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
|
53
|
+
further defined and clarified by project maintainers.
|
|
54
|
+
|
|
55
|
+
## Enforcement
|
|
56
|
+
|
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
|
58
|
+
reported by contacting the project team at dmytro@qonto.eu. All
|
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
|
63
|
+
|
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
|
66
|
+
members of the project's leadership.
|
|
67
|
+
|
|
68
|
+
## Attribution
|
|
69
|
+
|
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
|
72
|
+
|
|
73
|
+
[homepage]: http://contributor-covenant.org
|
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Dmytro Zakharov
|
|
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
|
|
13
|
+
all 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
|
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# Idempotent Request [](https://travis-ci.org/qonto/idempotent-request)
|
|
2
|
+
|
|
3
|
+
Rack middleware ensuring at most once requests for mutating endpoints.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'idempotent-request'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
$ bundle
|
|
16
|
+
|
|
17
|
+
Or install it yourself as:
|
|
18
|
+
|
|
19
|
+
$ gem install idempotent-request
|
|
20
|
+
|
|
21
|
+
## How it works
|
|
22
|
+
|
|
23
|
+
1. Front-end generates a unique `key` then a user goes to a specific route (for example, transfer page).
|
|
24
|
+
2. When user clicks "Submit" button, the `key` is sent in the header `idempotency-key` and back-end stores server response into redis.
|
|
25
|
+
3. All the consecutive requests with the `key` won't be executer by the server and the result of previous response (2) will be fetched from redis.
|
|
26
|
+
4. Once the user leaves or refreshes the page, front-end should re-generate the key.
|
|
27
|
+
|
|
28
|
+
## Configuration
|
|
29
|
+
```ruby
|
|
30
|
+
# application.rb
|
|
31
|
+
config.middleware.use IdempotentRequest::Middleware,
|
|
32
|
+
storage: IdempotentRequest::RedisStorage.new(::Redis.current, expire_time: 1.day),
|
|
33
|
+
policy: YOUR_CLASS
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
To define a policy, whether a request should be idempotent, you have to provider a class with the following interface:
|
|
37
|
+
|
|
38
|
+
```ruby
|
|
39
|
+
class Policy
|
|
40
|
+
attr_reader :request
|
|
41
|
+
|
|
42
|
+
def initialize(request)
|
|
43
|
+
@request = request
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def should?
|
|
47
|
+
# request is Rack::Request class
|
|
48
|
+
end
|
|
49
|
+
end
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Example of integration for rails
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
# application.rb
|
|
57
|
+
config.middleware.use IdempotentRequest::Middleware,
|
|
58
|
+
storage: IdempotentRequest::RedisStorage.new(::Redis.current, expire_time: 1.day),
|
|
59
|
+
policy: IdempotentRequest::Policy
|
|
60
|
+
|
|
61
|
+
config.idempotent_routes = [
|
|
62
|
+
{ controller: :'v1/transfers', action: :create },
|
|
63
|
+
]
|
|
64
|
+
```
|
|
65
|
+
|
|
66
|
+
```ruby
|
|
67
|
+
# lib/idempotent-request/policy.rb
|
|
68
|
+
module IdempotentRequest
|
|
69
|
+
class Policy
|
|
70
|
+
attr_reader :request
|
|
71
|
+
|
|
72
|
+
def initialize(request)
|
|
73
|
+
@request = request
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def should?
|
|
77
|
+
route = Rails.application.routes.recognize_path(request.path, method: request.request_method)
|
|
78
|
+
Rails.application.config.idempotent_routes.any? do |idempotent_route|
|
|
79
|
+
idempotent_route[:controller] == route[:controller].to_sym &&
|
|
80
|
+
idempotent_route[:action] == route[:action].to_sym
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## Custom options
|
|
88
|
+
|
|
89
|
+
```ruby
|
|
90
|
+
# application.rb
|
|
91
|
+
config.middleware.use IdempotentRequest::Middleware,
|
|
92
|
+
header_key: 'X-Qonto-Idempotency-Key', # by default Idempotency-key
|
|
93
|
+
policy: IdempotentRequest::Policy,
|
|
94
|
+
callback: IdempotentRequest::RailsCallback,
|
|
95
|
+
storage: IdempotentRequest::RedisStorage.new(::Redis.current, expire_time: 1.day, namespace: 'idempotency_keys')
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Policy
|
|
99
|
+
|
|
100
|
+
Custom class to decide whether the request should be idempotent.
|
|
101
|
+
|
|
102
|
+
See *Example of integration for rails*
|
|
103
|
+
|
|
104
|
+
### Storage
|
|
105
|
+
|
|
106
|
+
Where the response will be stored. Can be any class that implements the following interface:
|
|
107
|
+
|
|
108
|
+
```ruby
|
|
109
|
+
def read(key)
|
|
110
|
+
# read from a storage
|
|
111
|
+
end
|
|
112
|
+
|
|
113
|
+
def write(key, payload)
|
|
114
|
+
# write to a storage
|
|
115
|
+
end
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Callback
|
|
119
|
+
|
|
120
|
+
Get notified when the client sends a request with the same idempotency key:
|
|
121
|
+
|
|
122
|
+
```ruby
|
|
123
|
+
class RailsCallback
|
|
124
|
+
attr_reader :request
|
|
125
|
+
|
|
126
|
+
def initialize(request)
|
|
127
|
+
@request = request
|
|
128
|
+
end
|
|
129
|
+
|
|
130
|
+
def detected(key:)
|
|
131
|
+
Rails.logger.warn "IdempotentRequest request detected, key: #{key}"
|
|
132
|
+
end
|
|
133
|
+
end
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
## Contributing
|
|
137
|
+
|
|
138
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/idempotent-request. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
|
139
|
+
|
|
140
|
+
## License
|
|
141
|
+
|
|
142
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
|
143
|
+
|
|
144
|
+
## Code of Conduct
|
|
145
|
+
|
|
146
|
+
Everyone interacting in the Idempotent::Request project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/idempotent-request/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "bundler/setup"
|
|
4
|
+
require "idempotent/request"
|
|
5
|
+
|
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
|
8
|
+
|
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
|
10
|
+
# require "pry"
|
|
11
|
+
# Pry.start
|
|
12
|
+
|
|
13
|
+
require "irb"
|
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = 'idempotent-request'
|
|
8
|
+
spec.version = IdempotentRequest::VERSION
|
|
9
|
+
spec.authors = ['Dmytro Zakharov']
|
|
10
|
+
spec.email = ['dmytro@qonto.eu']
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Rack middleware ensuring at most once requests for mutating endpoints.}
|
|
13
|
+
spec.homepage = 'https://github.com/qonto/idempotent-request'
|
|
14
|
+
spec.license = 'MIT'
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
|
18
|
+
end
|
|
19
|
+
spec.bindir = 'exe'
|
|
20
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
21
|
+
spec.require_paths = ['lib']
|
|
22
|
+
|
|
23
|
+
spec.add_dependency 'rack', '~> 2.0'
|
|
24
|
+
spec.add_dependency 'oj', '~> 3.0'
|
|
25
|
+
|
|
26
|
+
spec.add_development_dependency 'bundler', '~> 1.15'
|
|
27
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
|
28
|
+
spec.add_development_dependency 'rspec', '~> 3.0'
|
|
29
|
+
spec.add_development_dependency 'fakeredis', '~> 0.6'
|
|
30
|
+
spec.add_development_dependency 'pry', '~> 0.11'
|
|
31
|
+
end
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
module IdempotentRequest
|
|
2
|
+
class Middleware
|
|
3
|
+
def initialize(app, config = {})
|
|
4
|
+
@app = app
|
|
5
|
+
@config = config
|
|
6
|
+
@policy = config.fetch(:policy)
|
|
7
|
+
end
|
|
8
|
+
|
|
9
|
+
def call(env)
|
|
10
|
+
# dup the middleware to be thread-safe
|
|
11
|
+
dup.process(env)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def process(env)
|
|
15
|
+
set_request(env)
|
|
16
|
+
return app.call(request.env) unless process?
|
|
17
|
+
storage = RequestManager.new(request, config)
|
|
18
|
+
storage.read || storage.write(*app.call(request.env))
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
private
|
|
22
|
+
|
|
23
|
+
attr_reader :app, :env, :config, :request, :policy
|
|
24
|
+
|
|
25
|
+
def process?
|
|
26
|
+
!request.key.to_s.empty? && should_be_idempotent?
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def should_be_idempotent?
|
|
30
|
+
return false unless policy
|
|
31
|
+
policy.new(request).should?
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def set_request(env)
|
|
35
|
+
@env = env
|
|
36
|
+
@request ||= Request.new(env, config)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
module IdempotentRequest
|
|
2
|
+
class RedisStorage
|
|
3
|
+
attr_reader :redis, :namespace, :expire_time
|
|
4
|
+
|
|
5
|
+
def initialize(redis, config = {})
|
|
6
|
+
@redis = redis
|
|
7
|
+
@namespace = config.fetch(:namespace, 'idempotency_keys')
|
|
8
|
+
@expire_time = config[:expire_time]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def read(key)
|
|
12
|
+
redis.get(namespaced_key(key))
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def write(key, payload)
|
|
16
|
+
redis.setnx(namespaced_key(key), payload)
|
|
17
|
+
redis.expire(namespaced_key(key), expire_time.to_i) if expire_time.to_i > 0
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
private
|
|
21
|
+
|
|
22
|
+
def namespaced_key(idempotency_key)
|
|
23
|
+
[namespace, idempotency_key.strip]
|
|
24
|
+
.compact
|
|
25
|
+
.join(':')
|
|
26
|
+
.downcase
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
end
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module IdempotentRequest
|
|
2
|
+
class Request
|
|
3
|
+
attr_reader :request
|
|
4
|
+
|
|
5
|
+
def initialize(env, config = {})
|
|
6
|
+
@request = Rack::Request.new(env)
|
|
7
|
+
@header_name = config.fetch(:header_key, 'HTTP_IDEMPOTENCY_KEY')
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def key
|
|
11
|
+
request.env[header_name]
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def method_missing(method, *args)
|
|
15
|
+
if request.respond_to?(method)
|
|
16
|
+
request.send(method, *args)
|
|
17
|
+
else
|
|
18
|
+
super
|
|
19
|
+
end
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
private
|
|
23
|
+
|
|
24
|
+
def header_name
|
|
25
|
+
key = @header_name
|
|
26
|
+
.to_s
|
|
27
|
+
.upcase
|
|
28
|
+
.gsub('-', '_')
|
|
29
|
+
|
|
30
|
+
key.start_with?('HTTP_') ? key : "HTTP_#{key}"
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
module IdempotentRequest
|
|
2
|
+
class RequestManager
|
|
3
|
+
attr_reader :request, :storage
|
|
4
|
+
|
|
5
|
+
def initialize(request, config)
|
|
6
|
+
@request = request
|
|
7
|
+
@storage = config.fetch(:storage)
|
|
8
|
+
@callback = config[:callback]
|
|
9
|
+
end
|
|
10
|
+
|
|
11
|
+
def read
|
|
12
|
+
status, headers, response = parse_data(storage.read(key)).values
|
|
13
|
+
|
|
14
|
+
return unless status
|
|
15
|
+
run_callback(:detected, key: request.key)
|
|
16
|
+
[status, headers, response]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def write(*data)
|
|
20
|
+
status, headers, response = data
|
|
21
|
+
response = response.body if response.respond_to?(:body)
|
|
22
|
+
|
|
23
|
+
return data unless status == 200
|
|
24
|
+
|
|
25
|
+
storage.write(key, payload(status, headers, response))
|
|
26
|
+
|
|
27
|
+
data
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
private
|
|
31
|
+
|
|
32
|
+
def parse_data(data)
|
|
33
|
+
return {} if data.to_s.empty?
|
|
34
|
+
|
|
35
|
+
Oj.load(data)
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def payload(status, headers, response)
|
|
39
|
+
Oj.dump({
|
|
40
|
+
status: status,
|
|
41
|
+
headers: headers.to_h,
|
|
42
|
+
response: response
|
|
43
|
+
})
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def run_callback(action, args)
|
|
47
|
+
return unless @callback
|
|
48
|
+
|
|
49
|
+
@callback.new(request).send(action, args)
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def key
|
|
53
|
+
request.key
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: idempotent-request
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Dmytro Zakharov
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-01-23 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: rack
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '2.0'
|
|
20
|
+
type: :runtime
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '2.0'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: oj
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '3.0'
|
|
34
|
+
type: :runtime
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '3.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: bundler
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '1.15'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '1.15'
|
|
55
|
+
- !ruby/object:Gem::Dependency
|
|
56
|
+
name: rake
|
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - "~>"
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '10.0'
|
|
62
|
+
type: :development
|
|
63
|
+
prerelease: false
|
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
65
|
+
requirements:
|
|
66
|
+
- - "~>"
|
|
67
|
+
- !ruby/object:Gem::Version
|
|
68
|
+
version: '10.0'
|
|
69
|
+
- !ruby/object:Gem::Dependency
|
|
70
|
+
name: rspec
|
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
|
72
|
+
requirements:
|
|
73
|
+
- - "~>"
|
|
74
|
+
- !ruby/object:Gem::Version
|
|
75
|
+
version: '3.0'
|
|
76
|
+
type: :development
|
|
77
|
+
prerelease: false
|
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
79
|
+
requirements:
|
|
80
|
+
- - "~>"
|
|
81
|
+
- !ruby/object:Gem::Version
|
|
82
|
+
version: '3.0'
|
|
83
|
+
- !ruby/object:Gem::Dependency
|
|
84
|
+
name: fakeredis
|
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
|
86
|
+
requirements:
|
|
87
|
+
- - "~>"
|
|
88
|
+
- !ruby/object:Gem::Version
|
|
89
|
+
version: '0.6'
|
|
90
|
+
type: :development
|
|
91
|
+
prerelease: false
|
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
93
|
+
requirements:
|
|
94
|
+
- - "~>"
|
|
95
|
+
- !ruby/object:Gem::Version
|
|
96
|
+
version: '0.6'
|
|
97
|
+
- !ruby/object:Gem::Dependency
|
|
98
|
+
name: pry
|
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
|
100
|
+
requirements:
|
|
101
|
+
- - "~>"
|
|
102
|
+
- !ruby/object:Gem::Version
|
|
103
|
+
version: '0.11'
|
|
104
|
+
type: :development
|
|
105
|
+
prerelease: false
|
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
107
|
+
requirements:
|
|
108
|
+
- - "~>"
|
|
109
|
+
- !ruby/object:Gem::Version
|
|
110
|
+
version: '0.11'
|
|
111
|
+
description:
|
|
112
|
+
email:
|
|
113
|
+
- dmytro@qonto.eu
|
|
114
|
+
executables: []
|
|
115
|
+
extensions: []
|
|
116
|
+
extra_rdoc_files: []
|
|
117
|
+
files:
|
|
118
|
+
- ".gitignore"
|
|
119
|
+
- ".rspec"
|
|
120
|
+
- ".travis.yml"
|
|
121
|
+
- CODE_OF_CONDUCT.md
|
|
122
|
+
- Gemfile
|
|
123
|
+
- LICENSE.txt
|
|
124
|
+
- README.md
|
|
125
|
+
- Rakefile
|
|
126
|
+
- bin/console
|
|
127
|
+
- bin/setup
|
|
128
|
+
- idempotent-request.gemspec
|
|
129
|
+
- lib/idempotent-request.rb
|
|
130
|
+
- lib/idempotent-request/middleware.rb
|
|
131
|
+
- lib/idempotent-request/redis_storage.rb
|
|
132
|
+
- lib/idempotent-request/request.rb
|
|
133
|
+
- lib/idempotent-request/request_manager.rb
|
|
134
|
+
- lib/version.rb
|
|
135
|
+
homepage: https://github.com/qonto/idempotent-request
|
|
136
|
+
licenses:
|
|
137
|
+
- MIT
|
|
138
|
+
metadata: {}
|
|
139
|
+
post_install_message:
|
|
140
|
+
rdoc_options: []
|
|
141
|
+
require_paths:
|
|
142
|
+
- lib
|
|
143
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
144
|
+
requirements:
|
|
145
|
+
- - ">="
|
|
146
|
+
- !ruby/object:Gem::Version
|
|
147
|
+
version: '0'
|
|
148
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
149
|
+
requirements:
|
|
150
|
+
- - ">="
|
|
151
|
+
- !ruby/object:Gem::Version
|
|
152
|
+
version: '0'
|
|
153
|
+
requirements: []
|
|
154
|
+
rubyforge_project:
|
|
155
|
+
rubygems_version: 2.6.13
|
|
156
|
+
signing_key:
|
|
157
|
+
specification_version: 4
|
|
158
|
+
summary: Rack middleware ensuring at most once requests for mutating endpoints.
|
|
159
|
+
test_files: []
|