erp_integration 0.55.0 → 0.56.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 865d21dfcb8c0caaed6bb6e76151e019424294a04c5a553c68459cbda871278b
4
- data.tar.gz: 5da7b05a69bc74a8dcfa508ebe2b7b544e5902b26398b6da552fd139f20170f1
3
+ metadata.gz: 55974d4e2c7b76ca461070fe996522e0b7fa06391760549f8959000ed07b2a9e
4
+ data.tar.gz: 43683efd68525709be0fcba1537051830f52d7c0663779a7ab5b4c24b0a6e2e9
5
5
  SHA512:
6
- metadata.gz: ea5e4f2a3f0e8bec393964d050ef9db305d1b7859032989d6899750306eba46efba5627e741fc469b95c215a23a42e6f6fcdee7f418f1ddf3da2f276cb201629
7
- data.tar.gz: 71db26f1be6f150c53f34591d7ca46f7f89805fb65b5f44258c6dac86067bb8752feae8a08c11bf5cc4f6c096e7b3749b511583d1e3cf536921463e0d829e385
6
+ metadata.gz: f0943cc3b1047ef17259a1eeb039fdd65ee74cb3ebc6911ff431162f60058b89d8e26e3f742028b261c2979c304aab4b1a0fd155e5d72fd0f0063a3a3afa351f
7
+ data.tar.gz: 4a06481fb232e9ad506c0d038af4114169546d622eecec0e6d5b03877e6f85648777c278728f87f6cae52c3e695f382ec34e4c1a2adcfadf0d9af23fc354374f
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 3.0.2
1
+ 3.2.2
data/README.md CHANGED
@@ -44,10 +44,13 @@ end
44
44
  To set up API key rotation, configure multiple keys.
45
45
  Every time a Fulfil client receives a `404` or `429`, it will rotate an API key.
46
46
 
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
+
47
49
  ```erb
48
50
  # config/initializers/erp_integration.rb
49
51
  ErpIntegration.configure do |config|
50
52
  config.fulfil_api_keys = ['<your-api-key1>', '<your-api-key2>']
53
+ config.api_key_rotation_threshold = 1
51
54
  ...
52
55
  end
53
56
  ```
@@ -22,6 +22,12 @@ module ErpIntegration
22
22
  # @return [String] The base URL for Fulfil.
23
23
  attr_accessor :fulfil_base_url
24
24
 
25
+ # The `api_key_rotation_threshold` is used by the `ApiKeysRotation` middleware
26
+ # it will rotate the API key when the remaining requests are less than the threshold.
27
+ #
28
+ # Set the `api_key_rotation_threshold` to 0, it you don't want to rotate by the remaining requests.
29
+ attr_reader :api_key_rotation_threshold
30
+
25
31
  # Allows configuring an adapter for the `BillOfMaterial` resource. When
26
32
  # none is configured, it will default to Fulfil.
27
33
  # @return [Symbol] The configured adapter for the bill_of_material.
@@ -172,6 +178,10 @@ module ErpIntegration
172
178
  end
173
179
  end
174
180
 
181
+ def api_key_rotation_threshold=(threshold)
182
+ @api_key_rotation_threshold = threshold || 0
183
+ end
184
+
175
185
  def carrier_adapter
176
186
  @carrier_adapter || :fulfil
177
187
  end
@@ -309,7 +319,7 @@ module ErpIntegration
309
319
  # Sets the rate limiters that will be used
310
320
  # @raise [MissingMethodError] if the rate limiter object doesn't respond to the required methods.
311
321
  def rate_limiters=(rate_limiters)
312
- @rate_limiters = rate_limiters.each { |limiter| RateLimiter.validate!(limiter) }
322
+ @rate_limiters = (rate_limiters || []).each { |limiter| RateLimiter.validate!(limiter) }
313
323
  end
314
324
  end
315
325
 
@@ -33,7 +33,7 @@ module ErpIntegration
33
33
  # Custom error handling for the error response
34
34
  faraday.use ErpIntegration::Middleware::ErrorHandling
35
35
  # Custom middleware for rotating API keys
36
- faraday.use ErpIntegration::Middleware::ApiKeysRotation, api_keys_pool: api_keys_pool
36
+ faraday.use ErpIntegration::Middleware::ApiKeysRotation, rotation_options
37
37
  # Custom middleware for logging requests and responses
38
38
  faraday.use ErpIntegration::Middleware::Logger, @logger if @logger
39
39
 
@@ -76,6 +76,13 @@ module ErpIntegration
76
76
  'Content-Type': 'application/json'
77
77
  }
78
78
  end
79
+
80
+ def rotation_options
81
+ {
82
+ api_keys_pool: api_keys_pool,
83
+ rotation_theshold: ErpIntegration.config.api_key_rotation_threshold
84
+ }
85
+ end
79
86
  end
80
87
  end
81
88
  end
@@ -20,14 +20,21 @@ module ErpIntegration
20
20
  end
21
21
 
22
22
  def on_request(env)
23
- env[:request_headers]['X-API-KEY'] = options[:api_keys_pool].current_key
23
+ env.request_headers['X-API-KEY'] = options[:api_keys_pool].current_key
24
24
  end
25
25
 
26
26
  def on_complete(env)
27
- return unless HTTP_ROTATE_CODES.include?(env.status)
27
+ return unless rotate?(env)
28
28
 
29
29
  options[:api_keys_pool].rotate_key!
30
30
  end
31
+
32
+ def rotate?(env)
33
+ return true if HTTP_ROTATE_CODES.include?(env.status)
34
+ return false unless env.response_headers['x-ratelimit-remaining'].present?
35
+
36
+ env.response_headers['x-ratelimit-remaining'].to_i < options[:rotation_theshold].to_i
37
+ end
31
38
  end
32
39
  end
33
40
  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]['X-API-KEY'])
22
+ api_key_fragment = sanitize_api_key(env.request_headers['X-API-KEY']) || 'none'
23
23
 
24
24
  if @logger.respond_to?(:tagged)
25
25
  @logger.tagged("API key *#{api_key_fragment}") do
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ErpIntegration
4
- VERSION = '0.55.0'
4
+ VERSION = '0.56.0'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: erp_integration
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.55.0
4
+ version: 0.56.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Stefan Vermaas
@@ -369,7 +369,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
369
369
  - !ruby/object:Gem::Version
370
370
  version: '0'
371
371
  requirements: []
372
- rubygems_version: 3.2.22
372
+ rubygems_version: 3.5.6
373
373
  signing_key:
374
374
  specification_version: 4
375
375
  summary: Connects Mejuri with third-party ERP vendors