erp_integration 1.4.0 → 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 +4 -4
- data/.github/CODEOWNERS +3 -0
- data/.github/workflows/code_quality.yml +1 -0
- data/.github/workflows/main.yml +1 -1
- data/.rubocop.yml +1 -1
- data/.ruby-version +1 -1
- data/CHANGELOG.md +27 -4
- data/README.md +3 -2
- data/erp_integration.gemspec +4 -1
- data/lib/erp_integration/api_key.rb +52 -0
- data/lib/erp_integration/api_keys/pool.rb +73 -0
- data/lib/erp_integration/api_keys/priority_queue.rb +28 -0
- data/lib/erp_integration/configuration.rb +12 -1
- data/lib/erp_integration/errors.rb +3 -0
- data/lib/erp_integration/fulfil/api_resource.rb +10 -6
- data/lib/erp_integration/fulfil/client.rb +60 -29
- data/lib/erp_integration/middleware/api_keys_rotation.rb +19 -14
- data/lib/erp_integration/middleware/logger.rb +2 -2
- data/lib/erp_integration/version.rb +1 -1
- data/lib/erp_integration.rb +3 -1
- metadata +22 -9
- data/lib/erp_integration/api_keys_pool.rb +0 -31
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 70c45c493888b551956263bff59b735d8f8e40bbfc885a40222857d27aaef539
|
|
4
|
+
data.tar.gz: 623ad04c4ab56ac186ac2c9992f5e79ff8e5063d096d1f8561f7dc5bedc88942
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5a2e454b4df3f4de8743802cbb1a2d7b674aeb5c6911d7ccd7e706dfbf5aced8afea6b4ecf82a4c30f8203d783a7601d2a548ff0a6bc475a3328c3e1768ac7b6
|
|
7
|
+
data.tar.gz: ab1b90d35fb4018977271b0a947b62656f66d41acc15aa5d9073e336a1e0d0d878106b8a3bffd33869491ee9ab2276656d9e41924d692e2e2f8ec2c041405547
|
data/.github/CODEOWNERS
ADDED
data/.github/workflows/main.yml
CHANGED
data/.rubocop.yml
CHANGED
data/.ruby-version
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
4.0.9
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,32 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [v.1.5.0](https://github.com/mejuri-inc/erp-integration/tree/v.1.5.0) (2026-11-16)
|
|
4
|
+
|
|
5
|
+
[Full Changelog](https://github.com/mejuri-inc/erp-integration/compare/v.1.4.0...v.1.5.0)
|
|
6
|
+
|
|
7
|
+
**Merged pull requests:**
|
|
8
|
+
|
|
9
|
+
- \[BACK-2946\] Prevent the race condition in API key rotation [\#236](https://github.com/mejuri-inc/erp-integration/pull/236) ([ikzekly](https://github.com/ikzekly))
|
|
10
|
+
|
|
11
|
+
**How API keys are picked after this release:**
|
|
12
|
+
|
|
13
|
+
- A request goes out with the first API key in line. API keys in line are ordered by the time they are ready, and API keys ready at the same time keep their places.
|
|
14
|
+
- An ERP answer with a `429`, or with a remaining quota under `api_key_rotation_threshold`, changes the ready time of the API key its request was sent with. The pool may have moved off that API key by then, so the API key can be anywhere in line. The pool then puts all API keys in order. Any other answer leaves the order as it is.
|
|
15
|
+
- The ERP gives the reset time of the quota in the `x-ratelimit-reset` header, in Unix seconds. The gem adds the seconds left until that time to the monotonic clock, so a change of the system time does not move the wait.
|
|
16
|
+
- A reset time that has passed gives a ready time in the past. That API key goes behind every API key that was ready earlier, and no request waits.
|
|
17
|
+
- When the answer has no reset time, or the reset time is not a number, the API key waits 2 seconds.
|
|
18
|
+
- A request waits for its API key at most 2 seconds. The ERP adds quota back at the start of every second, and the reset time in `x-ratelimit-reset` is 1 to 2 seconds after the answer, so a request is not expected to reach this limit. When the reset time is further away, the request goes out with that API key after 2 seconds and the ERP can answer it with a `429`.
|
|
19
|
+
- A request takes its API key first, then enters the rate limiter of that API key, so it is counted and sent on the same API key. A request that waits in the rate limiter keeps its API key. A rate limiter must not let through more requests on an API key within one ERP quota window than the ERP allows. Otherwise a request that waited in the rate limiter goes out with an API key the ERP has no quota for, and the ERP answers it with a `429`. A rate limiter can keep to this only when every request with that API key goes through it.
|
|
20
|
+
- Under `api_key_rotation_threshold` the API key is not ready until the ERP resets its quota. A host with one API key has no other API key to send with, so its next request waits for that reset, at most 2 seconds, while the ERP still has quota left. A threshold of 0 turns this off.
|
|
21
|
+
|
|
22
|
+
**Removed or changed:**
|
|
23
|
+
|
|
24
|
+
- `ErpIntegration::ApiKeysPool` is replaced by `ErpIntegration::ApiKeys::Pool`. Its `api_keys`, `api_keys=` and `rotate_key!` are removed.
|
|
25
|
+
- `current_key` of the pool returns an `ErpIntegration::ApiKey` with the API key in `value`. It returned a string before. It can wait up to 2 seconds for the API key.
|
|
26
|
+
- A `404` no longer changes the API key.
|
|
27
|
+
- `ErpIntegration::Fulfil::Client#rotate_statuses=` is removed. It had no effect.
|
|
28
|
+
- `ErpIntegration::Fulfil::Client#connection` is private. Requests go through `get`, `post`, `put`, `patch` and `delete`.
|
|
29
|
+
|
|
3
30
|
## [v.1.4.0](https://github.com/mejuri-inc/erp-integration/tree/v.1.4.0) (2026-05-18)
|
|
4
31
|
|
|
5
32
|
[Full Changelog](https://github.com/mejuri-inc/erp-integration/compare/v.1.3.0...v.1.4.0)
|
|
@@ -38,7 +65,3 @@
|
|
|
38
65
|
- \[DU-79\] Replace generic welcome for actionable todos. [\#3](https://github.com/mejuri-inc/erp-integration/pull/3) ([stefanvermaas](https://github.com/stefanvermaas))
|
|
39
66
|
- \[DU-79\] Adds Github's Dependabot [\#2](https://github.com/mejuri-inc/erp-integration/pull/2) ([stefanvermaas](https://github.com/stefanvermaas))
|
|
40
67
|
- \[DU-79\] Add GitHub actions [\#1](https://github.com/mejuri-inc/erp-integration/pull/1) ([stefanvermaas](https://github.com/stefanvermaas))
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
|
data/README.md
CHANGED
|
@@ -42,9 +42,10 @@ end
|
|
|
42
42
|
### API keys rotation
|
|
43
43
|
|
|
44
44
|
To set up API key rotation, configure multiple keys.
|
|
45
|
-
Every time a Fulfil client receives a `
|
|
45
|
+
Every time a Fulfil client receives a `429`, the API key is not ready until Fulfil resets its quota (the `x-ratelimit-reset` response header), and requests go out with the next ready API key.
|
|
46
|
+
When no API key is ready, a request waits for the first API key that is ready again, at most 2 seconds.
|
|
46
47
|
|
|
47
|
-
You can set the `api_key_rotation_threshold` option to automatically change the API key when requests are running low. If you don't want to use the `x-ratelimit-remaining` response header for rotation, set the `api_key_rotation_threshold` to 0 or ignore it. The minimum threshold is 1, meaning the key will rotate when there are 0 remaining requests.
|
|
48
|
+
You can set the `api_key_rotation_threshold` option to automatically change the API key when requests are running low. Under the threshold, the API key is not ready until Fulfil resets its quota, the same as after a `429`. If you don't want to use the `x-ratelimit-remaining` response header for rotation, set the `api_key_rotation_threshold` to 0 or ignore it. The minimum threshold is 1, meaning the key will rotate when there are 0 remaining requests.
|
|
48
49
|
|
|
49
50
|
```erb
|
|
50
51
|
# config/initializers/erp_integration.rb
|
data/erp_integration.gemspec
CHANGED
|
@@ -11,7 +11,7 @@ Gem::Specification.new do |spec|
|
|
|
11
11
|
spec.summary = 'Connects Mejuri with third-party ERP vendors'
|
|
12
12
|
spec.license = 'MIT'
|
|
13
13
|
spec.homepage = 'https://www.github.com/mejuri-inc/erp-integration'
|
|
14
|
-
spec.required_ruby_version = Gem::Requirement.new('>=
|
|
14
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 3.0')
|
|
15
15
|
|
|
16
16
|
spec.metadata['allowed_push_host'] = 'https://rubygems.org'
|
|
17
17
|
|
|
@@ -31,6 +31,9 @@ Gem::Specification.new do |spec|
|
|
|
31
31
|
# ActiveSupport is used for extended support of String inflections.
|
|
32
32
|
spec.add_dependency 'activesupport', '>= 4.1.16'
|
|
33
33
|
|
|
34
|
+
# Concurrent Ruby keeps one Fulfil API keys pool per set of API keys across threads.
|
|
35
|
+
spec.add_dependency 'concurrent-ruby', '~> 1.0'
|
|
36
|
+
|
|
34
37
|
# Faraday is a HTTP/REST API client library to interact with third-party API vendors
|
|
35
38
|
spec.add_dependency 'faraday', '~> 2.12'
|
|
36
39
|
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ErpIntegration
|
|
4
|
+
# A Fulfil API key and when Fulfil takes requests on it again.
|
|
5
|
+
class ApiKey
|
|
6
|
+
# The longest a request waits for its API key, and the wait when Fulfil does not say when it
|
|
7
|
+
# resets the quota, in seconds.
|
|
8
|
+
MAX_WAIT_FOR_QUOTA_RESET = 2
|
|
9
|
+
|
|
10
|
+
# ready_at is when Fulfil takes requests on the API key again, in seconds on the monotonic clock.
|
|
11
|
+
attr_reader :value, :ready_at
|
|
12
|
+
|
|
13
|
+
# A new API key is ready.
|
|
14
|
+
#
|
|
15
|
+
# @param value [String] the Fulfil API key
|
|
16
|
+
def initialize(value)
|
|
17
|
+
@value = value
|
|
18
|
+
@ready_at = monotonic_now
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
# The API key is not ready until Fulfil resets its quota. Fulfil says when in each answer. An
|
|
22
|
+
# answer without that time, or with a time that is not a number, makes the API key wait
|
|
23
|
+
# MAX_WAIT_FOR_QUOTA_RESET seconds.
|
|
24
|
+
#
|
|
25
|
+
# @param reset_header [String, nil] the x-ratelimit-reset header of the Fulfil answer, in Unix seconds
|
|
26
|
+
# @return [String, nil] the header
|
|
27
|
+
def quota_reset=(reset_header)
|
|
28
|
+
reset_at = Float(reset_header, exception: false)
|
|
29
|
+
seconds = reset_at&.finite? ? reset_at - Time.now.to_f : MAX_WAIT_FOR_QUOTA_RESET
|
|
30
|
+
@ready_at = monotonic_now + seconds
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# A request waits until Fulfil takes requests on its API key again, so Fulfil does not answer it
|
|
34
|
+
# with a 429. The wait holds up a thread of the host, so it stops at MAX_WAIT_FOR_QUOTA_RESET
|
|
35
|
+
# even when Fulfil gives a later reset time.
|
|
36
|
+
#
|
|
37
|
+
# @return [void]
|
|
38
|
+
def wait_until_ready
|
|
39
|
+
seconds = [@ready_at - monotonic_now, MAX_WAIT_FOR_QUOTA_RESET].min
|
|
40
|
+
sleep(seconds) if seconds > 0
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
private
|
|
44
|
+
|
|
45
|
+
# Changes to the system time do not move the waits.
|
|
46
|
+
#
|
|
47
|
+
# @return [Float] seconds on the monotonic clock
|
|
48
|
+
def monotonic_now
|
|
49
|
+
Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'concurrent/map'
|
|
4
|
+
|
|
5
|
+
module ErpIntegration
|
|
6
|
+
module ApiKeys
|
|
7
|
+
# Fulfil limits requests per API key, so a resource can hold several API keys and send each
|
|
8
|
+
# request with one that Fulfil takes requests on. Every thread and every resource that calls
|
|
9
|
+
# Fulfil with the same API keys shares one pool.
|
|
10
|
+
class Pool
|
|
11
|
+
@instances = Concurrent::Map.new
|
|
12
|
+
|
|
13
|
+
class << self
|
|
14
|
+
# Resources with the same Fulfil API keys share one pool, in whatever order they list the API
|
|
15
|
+
# keys, so they move off an API key together.
|
|
16
|
+
#
|
|
17
|
+
# @param api_keys [String, Array<String>] one Fulfil API key or several
|
|
18
|
+
# @return [ErpIntegration::ApiKeys::Pool] the one pool for these API keys
|
|
19
|
+
# @raise [ErpIntegration::ConfigurationError] when an API key is missing or duplicated
|
|
20
|
+
def instance(api_keys)
|
|
21
|
+
api_keys = Array(api_keys)
|
|
22
|
+
validate!(api_keys)
|
|
23
|
+
|
|
24
|
+
@instances.compute_if_absent(api_keys.to_set) { new(api_keys) }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
# A host that reads its API keys from settings passes nil for an API key it has not set. A
|
|
28
|
+
# duplicated API key would get two places in line for one Fulfil quota. Both are mistakes in
|
|
29
|
+
# the host configuration.
|
|
30
|
+
#
|
|
31
|
+
# @param api_keys [Array<String>] the Fulfil API keys
|
|
32
|
+
# @return [Array<String>] the same Fulfil API keys
|
|
33
|
+
# @raise [ErpIntegration::ConfigurationError] when an API key is missing or duplicated
|
|
34
|
+
def validate!(api_keys)
|
|
35
|
+
raise ConfigurationError, 'A Fulfil API key is missing.' if api_keys.include?(nil)
|
|
36
|
+
raise ConfigurationError, 'A Fulfil API key is duplicated.' if api_keys.uniq.size < api_keys.size
|
|
37
|
+
|
|
38
|
+
api_keys
|
|
39
|
+
end
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
# @param api_keys [Array<String>] the Fulfil API keys
|
|
43
|
+
def initialize(api_keys)
|
|
44
|
+
@priority_queue = PriorityQueue.new(api_keys.map { |api_key| ApiKey.new(api_key) })
|
|
45
|
+
@mutex = Mutex.new
|
|
46
|
+
end
|
|
47
|
+
|
|
48
|
+
# A request goes out with the API key that is ready first. When that API key is not ready yet,
|
|
49
|
+
# the request waits for it. A host builds its pools while the app boots, also where its
|
|
50
|
+
# credentials are not readable, so a pool without API keys raises only when a request asks it
|
|
51
|
+
# for one.
|
|
52
|
+
#
|
|
53
|
+
# @return [ErpIntegration::ApiKey] the API key the request goes out with
|
|
54
|
+
# @raise [ErpIntegration::ConfigurationError] when the pool has no API key
|
|
55
|
+
def current_key
|
|
56
|
+
api_key = @mutex.synchronize { @priority_queue.first }
|
|
57
|
+
raise ConfigurationError, 'A Fulfil API key is missing.' unless api_key
|
|
58
|
+
|
|
59
|
+
api_key.wait_until_ready
|
|
60
|
+
api_key
|
|
61
|
+
end
|
|
62
|
+
|
|
63
|
+
# A Fulfil answer changed the ready time of the API key its request was sent with, so another
|
|
64
|
+
# API key can be the one that is ready first.
|
|
65
|
+
#
|
|
66
|
+
# @return [void]
|
|
67
|
+
def rotate!
|
|
68
|
+
@mutex.synchronize { @priority_queue.sort! }
|
|
69
|
+
nil
|
|
70
|
+
end
|
|
71
|
+
end
|
|
72
|
+
end
|
|
73
|
+
end
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module ErpIntegration
|
|
4
|
+
module ApiKeys
|
|
5
|
+
# Fulfil API keys in the order they are ready: ready API keys first, then the ones that wait, the
|
|
6
|
+
# one ready soonest first. The pool that holds the queue changes it under the pool's lock.
|
|
7
|
+
class PriorityQueue
|
|
8
|
+
# @param api_keys [Array<ErpIntegration::ApiKey>] API keys that are all ready
|
|
9
|
+
def initialize(api_keys)
|
|
10
|
+
@api_keys = api_keys
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
# @return [ErpIntegration::ApiKey] the API key that is ready, or ready soonest
|
|
14
|
+
def first
|
|
15
|
+
@api_keys.first
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# A Fulfil answer can come late, about an API key the pool has already moved off, so any API key
|
|
19
|
+
# in line can have a new ready time. API keys ready at the same time keep their places, so the
|
|
20
|
+
# pool stays on its current API key.
|
|
21
|
+
#
|
|
22
|
+
# @return [Array<ErpIntegration::ApiKey>] the API keys in line
|
|
23
|
+
def sort!
|
|
24
|
+
@api_keys.sort_by!.with_index { |api_key, index| [api_key.ready_at, index] }
|
|
25
|
+
end
|
|
26
|
+
end
|
|
27
|
+
end
|
|
28
|
+
end
|
|
@@ -15,7 +15,7 @@ module ErpIntegration
|
|
|
15
15
|
# The `fulfil_api_keys` sets a single or a list of API keys to use
|
|
16
16
|
# in the `FulfilClient` to authorize the requests to the Fulfil API endpoints.
|
|
17
17
|
# @return [Array<String>] The API keys for Fulfil.
|
|
18
|
-
|
|
18
|
+
attr_reader :fulfil_api_keys
|
|
19
19
|
|
|
20
20
|
# The `fulfil_base_url` is used by the `FulfilClient` to connect to
|
|
21
21
|
# the right Fulfil API endpoints.
|
|
@@ -184,6 +184,17 @@ module ErpIntegration
|
|
|
184
184
|
end
|
|
185
185
|
end
|
|
186
186
|
|
|
187
|
+
# A host sets its API keys while the app boots, so a mistake in them stops the boot before any
|
|
188
|
+
# request goes out to Fulfil.
|
|
189
|
+
#
|
|
190
|
+
# @param fulfil_api_keys [String, Array<String>, nil] one Fulfil API key, several, or nil when
|
|
191
|
+
# the credentials cannot be read
|
|
192
|
+
# @return [void]
|
|
193
|
+
# @raise [ErpIntegration::ConfigurationError] when an API key is missing or duplicated
|
|
194
|
+
def fulfil_api_keys=(fulfil_api_keys)
|
|
195
|
+
@fulfil_api_keys = ApiKeys::Pool.validate!(Array(fulfil_api_keys))
|
|
196
|
+
end
|
|
197
|
+
|
|
187
198
|
def api_key_rotation_threshold=(threshold)
|
|
188
199
|
@api_key_rotation_threshold = threshold || 0
|
|
189
200
|
end
|
|
@@ -5,6 +5,9 @@ module ErpIntegration
|
|
|
5
5
|
# on purpose by this gem.
|
|
6
6
|
class Error < StandardError; end
|
|
7
7
|
|
|
8
|
+
# The host configured the gem in a way it cannot work with.
|
|
9
|
+
class ConfigurationError < Error; end
|
|
10
|
+
|
|
8
11
|
# The `ErpIntegration::BadConfiguration` is raised whenever the newly provided
|
|
9
12
|
# configuration is not a valid configuration object.
|
|
10
13
|
class BadConfiguration < Error
|
|
@@ -34,16 +34,20 @@ module ErpIntegration
|
|
|
34
34
|
)
|
|
35
35
|
end
|
|
36
36
|
|
|
37
|
-
#
|
|
38
|
-
#
|
|
37
|
+
# A resource without API keys of its own calls Fulfil with the API keys from the gem's
|
|
38
|
+
# configuration.
|
|
39
|
+
#
|
|
40
|
+
# @return [ErpIntegration::ApiKeys::Pool] the pool the resource takes its API keys from
|
|
39
41
|
def self.api_keys_pool
|
|
40
|
-
@api_keys_pool ||=
|
|
42
|
+
@api_keys_pool ||= ApiKeys::Pool.instance(config.fulfil_api_keys)
|
|
41
43
|
end
|
|
42
44
|
|
|
43
|
-
#
|
|
44
|
-
#
|
|
45
|
+
# Gives the resource API keys of its own, shared with every resource given the same API keys.
|
|
46
|
+
#
|
|
47
|
+
# @param fulfil_api_keys [String, Array<String>] one Fulfil API key or several
|
|
48
|
+
# @return [ErpIntegration::ApiKeys::Pool] the pool for these API keys
|
|
45
49
|
def self.api_keys_pool=(fulfil_api_keys)
|
|
46
|
-
@api_keys_pool =
|
|
50
|
+
@api_keys_pool = ApiKeys::Pool.instance(fulfil_api_keys)
|
|
47
51
|
end
|
|
48
52
|
|
|
49
53
|
# The `config` exposes the gem's configuration to the `ApiResource`.
|
|
@@ -4,13 +4,15 @@ module ErpIntegration
|
|
|
4
4
|
module Fulfil
|
|
5
5
|
class Client
|
|
6
6
|
FORMATTER = ErpIntegration::Middleware::Formatter
|
|
7
|
+
# Fulfil reads the API key of a request from this header.
|
|
8
|
+
API_KEY_HEADER = 'X-API-KEY'
|
|
7
9
|
|
|
8
10
|
attr_reader :api_keys_pool, :base_url
|
|
9
|
-
attr_writer :
|
|
11
|
+
attr_writer :faraday_adapter
|
|
10
12
|
|
|
11
13
|
def initialize(api_keys_pool:, base_url:, logger: nil)
|
|
12
14
|
@api_keys_pool = api_keys_pool
|
|
13
|
-
@base_url = base_url
|
|
15
|
+
@base_url = base_url&.strip
|
|
14
16
|
@logger = logger
|
|
15
17
|
end
|
|
16
18
|
|
|
@@ -20,8 +22,45 @@ module ErpIntegration
|
|
|
20
22
|
@faraday_adapter ||= Faraday.default_adapter
|
|
21
23
|
end
|
|
22
24
|
|
|
23
|
-
#
|
|
24
|
-
#
|
|
25
|
+
# The HTTP verbs the Fulfil resources call the Fulfil API with. Each request takes one API key
|
|
26
|
+
# from the pool and goes out inside that API key's rate limiter, so Fulfil gets on each API key
|
|
27
|
+
# only the requests that API key's rate limiter counted, and a host rate limiter sees the errors
|
|
28
|
+
# the gem raises on Fulfil's answer.
|
|
29
|
+
#
|
|
30
|
+
# @param path [String] the Fulfil API path after the API version
|
|
31
|
+
# @param options [Hash] the query parameters or the request body
|
|
32
|
+
# @return [Hash, Array, nil] the decoded Fulfil API response body
|
|
33
|
+
# @raise [ErpIntegration::ConfigurationError] when the pool has no API key
|
|
34
|
+
# @raise [ErpIntegration::Error] when the base URL is missing
|
|
35
|
+
# @raise [ErpIntegration::HttpError] when Fulfil answers with a status listed in
|
|
36
|
+
# ErpIntegration::Middleware::ErrorHandling::HTTP_ERROR_CODES
|
|
37
|
+
# @raise [ErpIntegration::Middleware::JsonDecode::ParseError] when the Fulfil API response body
|
|
38
|
+
# is not JSON
|
|
39
|
+
# @raise [StandardError] what the host's rate limiter raises when the API key stays over its
|
|
40
|
+
# limit for too long
|
|
41
|
+
%i[delete get patch put post].each do |action_name|
|
|
42
|
+
define_method(action_name) do |path, options = {}|
|
|
43
|
+
raise ErpIntegration::Error, 'The Fulfil base URL is missing.' if base_url.nil?
|
|
44
|
+
|
|
45
|
+
api_key = api_keys_pool.current_key
|
|
46
|
+
|
|
47
|
+
RateLimiter.find_by_api_key(api_key.value).within_limit do
|
|
48
|
+
send_request(action_name, path, options, api_key).body
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
# Sets the default version for the Fulfil API endpoints.
|
|
54
|
+
# @return [String] The Fulfil API version to be used
|
|
55
|
+
def version
|
|
56
|
+
@version ||= 'v2'
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
private
|
|
60
|
+
|
|
61
|
+
# Requests reach Fulfil only through the HTTP verbs, which take the API key from the pool.
|
|
62
|
+
#
|
|
63
|
+
# @return [Faraday::Connection] the Faraday connection to the Fulfil API
|
|
25
64
|
def connection
|
|
26
65
|
@connection ||= Faraday.new(url: base_url) do |faraday|
|
|
27
66
|
faraday.headers = default_headers
|
|
@@ -41,34 +80,24 @@ module ErpIntegration
|
|
|
41
80
|
end
|
|
42
81
|
end
|
|
43
82
|
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
83
|
+
# Fulfil reads the API key from the header, and ApiKeysRotation finds it in the request context
|
|
84
|
+
# to mark that same API key when Fulfil says it runs low on quota.
|
|
85
|
+
#
|
|
86
|
+
# @param action_name [Symbol] the HTTP verb
|
|
87
|
+
# @param path [String] the Fulfil API path after the API version
|
|
88
|
+
# @param options [Hash] the query parameters or the request body
|
|
89
|
+
# @param api_key [ErpIntegration::ApiKey] the API key the request goes out with
|
|
90
|
+
# @return [Faraday::Response] the Fulfil API response
|
|
91
|
+
def send_request(action_name, path, options, api_key)
|
|
92
|
+
connection.public_send(action_name, "api/#{version}/#{path}", options) do |request|
|
|
93
|
+
request.headers[API_KEY_HEADER] = api_key.value
|
|
94
|
+
request.options.context = { api_key: api_key }
|
|
53
95
|
end
|
|
54
96
|
end
|
|
55
97
|
|
|
56
|
-
#
|
|
57
|
-
#
|
|
58
|
-
|
|
59
|
-
@version ||= 'v2'
|
|
60
|
-
end
|
|
61
|
-
|
|
62
|
-
private
|
|
63
|
-
|
|
64
|
-
def api_key
|
|
65
|
-
api_keys_pool.current_key
|
|
66
|
-
end
|
|
67
|
-
|
|
68
|
-
def rate_limiter
|
|
69
|
-
RateLimiter.find_by_api_key(api_key)
|
|
70
|
-
end
|
|
71
|
-
|
|
98
|
+
# Fulfil takes and returns JSON.
|
|
99
|
+
#
|
|
100
|
+
# @return [Hash] the headers every Fulfil API request carries
|
|
72
101
|
def default_headers
|
|
73
102
|
{
|
|
74
103
|
'Accept': 'application/json',
|
|
@@ -76,6 +105,8 @@ module ErpIntegration
|
|
|
76
105
|
}
|
|
77
106
|
end
|
|
78
107
|
|
|
108
|
+
# @return [Hash] the pool of the resource and the remaining Fulfil quota under which the API key
|
|
109
|
+
# waits for its quota reset
|
|
79
110
|
def rotation_options
|
|
80
111
|
{
|
|
81
112
|
api_keys_pool: api_keys_pool,
|
|
@@ -2,8 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
module ErpIntegration
|
|
4
4
|
module Middleware
|
|
5
|
+
# Tells the pool the API key a request was sent with is not ready, once Fulfil says that API key
|
|
6
|
+
# runs low on quota.
|
|
5
7
|
class ApiKeysRotation < Faraday::Middleware
|
|
6
|
-
|
|
8
|
+
# Fulfil answers 429 when the API key has no quota left.
|
|
9
|
+
HTTP_ROTATE_CODES = [429].freeze
|
|
7
10
|
|
|
8
11
|
attr_reader :options
|
|
9
12
|
|
|
@@ -13,27 +16,29 @@ module ErpIntegration
|
|
|
13
16
|
@options = options
|
|
14
17
|
end
|
|
15
18
|
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
def on_request(env)
|
|
23
|
-
env.request_headers['X-API-KEY'] = options[:api_keys_pool].current_key
|
|
24
|
-
end
|
|
25
|
-
|
|
19
|
+
# Fulfil's answer is about the API key the Fulfil client sent the request with, which the pool
|
|
20
|
+
# may have moved off already.
|
|
21
|
+
#
|
|
22
|
+
# @param env [Faraday::Env] the Fulfil API response
|
|
23
|
+
# @return [void]
|
|
26
24
|
def on_complete(env)
|
|
27
25
|
return unless rotate?(env)
|
|
28
26
|
|
|
29
|
-
|
|
27
|
+
env.request.context[:api_key].quota_reset = env.response_headers['x-ratelimit-reset']
|
|
28
|
+
|
|
29
|
+
options[:api_keys_pool].rotate!
|
|
30
30
|
end
|
|
31
31
|
|
|
32
|
+
# After a 429, or when Fulfil's remaining quota for the API key is under the rotation threshold,
|
|
33
|
+
# the API key is not ready until Fulfil resets its quota.
|
|
34
|
+
#
|
|
35
|
+
# @param env [Faraday::Env] the Fulfil API response
|
|
36
|
+
# @return [Boolean] true when the API key is not ready
|
|
32
37
|
def rotate?(env)
|
|
33
38
|
return true if HTTP_ROTATE_CODES.include?(env.status)
|
|
34
|
-
return false unless env.response_headers['x-ratelimit-remaining'].present?
|
|
35
39
|
|
|
36
|
-
env.response_headers['x-ratelimit-remaining']
|
|
40
|
+
remaining_requests = env.response_headers['x-ratelimit-remaining']
|
|
41
|
+
remaining_requests.present? && remaining_requests.to_i < options[:rotation_theshold].to_i
|
|
37
42
|
end
|
|
38
43
|
end
|
|
39
44
|
end
|
|
@@ -19,7 +19,7 @@ module ErpIntegration
|
|
|
19
19
|
end
|
|
20
20
|
|
|
21
21
|
def call(env)
|
|
22
|
-
api_key_fragment = sanitize_api_key(env.request_headers[
|
|
22
|
+
api_key_fragment = sanitize_api_key(env.request_headers[Fulfil::Client::API_KEY_HEADER]) || 'none'
|
|
23
23
|
request_uuid = SecureRandom.uuid
|
|
24
24
|
|
|
25
25
|
@logger.with_tags(api_key_tag(api_key_fragment), request_uuid) do
|
|
@@ -40,7 +40,7 @@ module ErpIntegration
|
|
|
40
40
|
# @param [String] api_key
|
|
41
41
|
# @return [String] The last 4 characters of the API key
|
|
42
42
|
def sanitize_api_key(api_key)
|
|
43
|
-
api_key[-4
|
|
43
|
+
api_key[-4..] if api_key
|
|
44
44
|
end
|
|
45
45
|
|
|
46
46
|
def api_key_tag(api_key_fragment)
|
data/lib/erp_integration.rb
CHANGED
|
@@ -9,7 +9,9 @@ require 'json'
|
|
|
9
9
|
|
|
10
10
|
require_relative 'erp_integration/version'
|
|
11
11
|
require_relative 'erp_integration/errors'
|
|
12
|
-
require_relative 'erp_integration/
|
|
12
|
+
require_relative 'erp_integration/api_key'
|
|
13
|
+
require_relative 'erp_integration/api_keys/priority_queue'
|
|
14
|
+
require_relative 'erp_integration/api_keys/pool'
|
|
13
15
|
require_relative 'erp_integration/rate_limiter'
|
|
14
16
|
require_relative 'erp_integration/logger'
|
|
15
17
|
require_relative 'erp_integration/configuration'
|
metadata
CHANGED
|
@@ -1,14 +1,13 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: erp_integration
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.5.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Stefan Vermaas
|
|
8
|
-
autorequire:
|
|
9
8
|
bindir: exe
|
|
10
9
|
cert_chain: []
|
|
11
|
-
date:
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
12
11
|
dependencies:
|
|
13
12
|
- !ruby/object:Gem::Dependency
|
|
14
13
|
name: activesupport
|
|
@@ -24,6 +23,20 @@ dependencies:
|
|
|
24
23
|
- - ">="
|
|
25
24
|
- !ruby/object:Gem::Version
|
|
26
25
|
version: 4.1.16
|
|
26
|
+
- !ruby/object:Gem::Dependency
|
|
27
|
+
name: concurrent-ruby
|
|
28
|
+
requirement: !ruby/object:Gem::Requirement
|
|
29
|
+
requirements:
|
|
30
|
+
- - "~>"
|
|
31
|
+
- !ruby/object:Gem::Version
|
|
32
|
+
version: '1.0'
|
|
33
|
+
type: :runtime
|
|
34
|
+
prerelease: false
|
|
35
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
36
|
+
requirements:
|
|
37
|
+
- - "~>"
|
|
38
|
+
- !ruby/object:Gem::Version
|
|
39
|
+
version: '1.0'
|
|
27
40
|
- !ruby/object:Gem::Dependency
|
|
28
41
|
name: faraday
|
|
29
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -198,7 +211,6 @@ dependencies:
|
|
|
198
211
|
- - '='
|
|
199
212
|
- !ruby/object:Gem::Version
|
|
200
213
|
version: 1.19.2
|
|
201
|
-
description:
|
|
202
214
|
email:
|
|
203
215
|
- stefan@knowndecimal.com
|
|
204
216
|
executables: []
|
|
@@ -206,6 +218,7 @@ extensions: []
|
|
|
206
218
|
extra_rdoc_files: []
|
|
207
219
|
files:
|
|
208
220
|
- ".flayignore"
|
|
221
|
+
- ".github/CODEOWNERS"
|
|
209
222
|
- ".github/dependabot.yml"
|
|
210
223
|
- ".github/pull_request_template.md"
|
|
211
224
|
- ".github/workflows/code_quality.yml"
|
|
@@ -233,7 +246,9 @@ files:
|
|
|
233
246
|
- bin/setup
|
|
234
247
|
- erp_integration.gemspec
|
|
235
248
|
- lib/erp_integration.rb
|
|
236
|
-
- lib/erp_integration/
|
|
249
|
+
- lib/erp_integration/api_key.rb
|
|
250
|
+
- lib/erp_integration/api_keys/pool.rb
|
|
251
|
+
- lib/erp_integration/api_keys/priority_queue.rb
|
|
237
252
|
- lib/erp_integration/bill_of_material.rb
|
|
238
253
|
- lib/erp_integration/bill_of_material_input.rb
|
|
239
254
|
- lib/erp_integration/bill_of_material_output.rb
|
|
@@ -336,7 +351,6 @@ metadata:
|
|
|
336
351
|
homepage_uri: https://www.github.com/mejuri-inc/erp-integration
|
|
337
352
|
source_code_uri: https://www.github.com/mejuri-inc/erp-integration
|
|
338
353
|
changelog_uri: https://www.github.com/mejuri-inc/erp-integration/blob/main/CHANGELOG.md
|
|
339
|
-
post_install_message:
|
|
340
354
|
rdoc_options: []
|
|
341
355
|
require_paths:
|
|
342
356
|
- lib
|
|
@@ -344,15 +358,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
344
358
|
requirements:
|
|
345
359
|
- - ">="
|
|
346
360
|
- !ruby/object:Gem::Version
|
|
347
|
-
version:
|
|
361
|
+
version: '3.0'
|
|
348
362
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
349
363
|
requirements:
|
|
350
364
|
- - ">="
|
|
351
365
|
- !ruby/object:Gem::Version
|
|
352
366
|
version: '0'
|
|
353
367
|
requirements: []
|
|
354
|
-
rubygems_version:
|
|
355
|
-
signing_key:
|
|
368
|
+
rubygems_version: 4.0.20
|
|
356
369
|
specification_version: 4
|
|
357
370
|
summary: Connects Mejuri with third-party ERP vendors
|
|
358
371
|
test_files: []
|
|
@@ -1,31 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module ErpIntegration
|
|
4
|
-
# The `ApiKeysPool` class is a simple
|
|
5
|
-
# class that holds a list of API keys
|
|
6
|
-
# and rotates between them.
|
|
7
|
-
class ApiKeysPool
|
|
8
|
-
attr_reader :api_keys
|
|
9
|
-
|
|
10
|
-
def initialize
|
|
11
|
-
@api_keys = []
|
|
12
|
-
@current_key_index = 0
|
|
13
|
-
end
|
|
14
|
-
|
|
15
|
-
# Allows setting a list of API keys for the pool.
|
|
16
|
-
def api_keys=(keys)
|
|
17
|
-
@api_keys = Array(keys)
|
|
18
|
-
end
|
|
19
|
-
|
|
20
|
-
# Returns the current API key.
|
|
21
|
-
# @return [String] The current API key.
|
|
22
|
-
def current_key
|
|
23
|
-
@api_keys[@current_key_index]
|
|
24
|
-
end
|
|
25
|
-
|
|
26
|
-
# Rotates the API key to the next one in the list.
|
|
27
|
-
def rotate_key!
|
|
28
|
-
@current_key_index = (@current_key_index + 1) % @api_keys.size
|
|
29
|
-
end
|
|
30
|
-
end
|
|
31
|
-
end
|