Class: ErpIntegration::Middleware::ApiKeysRotation
- Inherits:
-
Faraday::Middleware
- Object
- Faraday::Middleware
- ErpIntegration::Middleware::ApiKeysRotation
- Defined in:
- lib/erp_integration/middleware/api_keys_rotation.rb
Constant Summary collapse
- HTTP_ROTATE_CODES =
[404, 429].freeze
Instance Attribute Summary collapse
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Instance Method Summary collapse
- #call(env) ⇒ Object
-
#initialize(app, options = {}) ⇒ ApiKeysRotation
constructor
A new instance of ApiKeysRotation.
- #on_complete(env) ⇒ Object
- #on_request(env) ⇒ Object
- #rotate?(env) ⇒ Boolean
Constructor Details
#initialize(app, options = {}) ⇒ ApiKeysRotation
Returns a new instance of ApiKeysRotation.
10 11 12 13 14 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 10 def initialize(app, = {}) super(app) @options = end |
Instance Attribute Details
#options ⇒ Object (readonly)
Returns the value of attribute options.
8 9 10 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 8 def @options end |
Instance Method Details
#call(env) ⇒ Object
16 17 18 19 20 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 16 def call(env) on_request(env) @app.call(env).on_complete { |response| on_complete(response) } end |
#on_complete(env) ⇒ Object
26 27 28 29 30 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 26 def on_complete(env) return unless rotate?(env) [:api_keys_pool].rotate_key! end |
#on_request(env) ⇒ Object
22 23 24 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 22 def on_request(env) env.request_headers['X-API-KEY'] = [:api_keys_pool].current_key end |
#rotate?(env) ⇒ Boolean
32 33 34 35 36 37 |
# File 'lib/erp_integration/middleware/api_keys_rotation.rb', line 32 def rotate?(env) return true if HTTP_ROTATE_CODES.include?(env.status) return false unless env.response_headers['x-ratelimit-remaining'].present? env.response_headers['x-ratelimit-remaining'].to_i < [:rotation_theshold].to_i end |