Sha256: 76073eb8ccbfc91528423beca87caeac0512bf2c13ba89c3d061264d41072718

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

# frozen_string_literal: true

module HTTPX
  module Plugins
    module Retries
      MAX_RETRIES = 3
      IDEMPOTENT_METHODS = %i[get options head put delete].freeze

      module InstanceMethods
        def max_retries(n)
          branch(default_options.with_max_retries(n.to_i))
        end

        private

        def fetch_response(request)
          response = super
          if response.is_a?(ErrorResponse) &&
             request.retries.positive? &&
             IDEMPOTENT_METHODS.include?(request.verb)
            request.retries -= 1
            channel = find_channel(request)
            channel.send(request)
            return
          end
          response
        end
      end

      module RequestMethods
        attr_accessor :retries

        def initialize(*args)
          super
          @retries = @options.max_retries || MAX_RETRIES
        end
      end

      module OptionsMethods
        def self.included(klass)
          super
          klass.def_option(:max_retries) do |num|
            num = Integer(num)
            raise Error, ":max_retries must be positive" unless num.positive?
            num
          end
        end
      end
    end
    register_plugin :retries, Retries
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
httpx-0.3.1 lib/httpx/plugins/retries.rb
httpx-0.3.0 lib/httpx/plugins/retries.rb
httpx-0.2.1 lib/httpx/plugins/retries.rb
httpx-0.2.0 lib/httpx/plugins/retries.rb
httpx-0.1.0 lib/httpx/plugins/retries.rb