Sha256: 246524786852652de91e8dd4cc9fad52d6de7edb369108a437f930292d834903
Contents?: true
Size: 945 Bytes
Versions: 6
Compression:
Stored size: 945 Bytes
Contents
# frozen_string_literal: true module ErpIntegration module Middleware class ApiKeysRotation < Faraday::Middleware HTTP_ROTATE_CODES = [404, 429].freeze attr_reader :options def initialize(app, options = {}) super(app) @options = options end def call(env) on_request(env) @app.call(env).on_complete { |response| on_complete(response) } end def on_request(env) env.request_headers['X-API-KEY'] = options[:api_keys_pool].current_key end def on_complete(env) return unless rotate?(env) options[:api_keys_pool].rotate_key! end 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 < options[:rotation_theshold].to_i end end end end
Version data entries
6 entries across 6 versions & 1 rubygems