faraday 0.17.6 → 1.0.0.pre.rc1
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/LICENSE.md +1 -1
- data/README.md +18 -358
- data/lib/faraday/adapter/em_http.rb +142 -99
- data/lib/faraday/adapter/em_http_ssl_patch.rb +23 -17
- data/lib/faraday/adapter/em_synchrony/parallel_manager.rb +18 -15
- data/lib/faraday/adapter/em_synchrony.rb +104 -60
- data/lib/faraday/adapter/excon.rb +97 -57
- data/lib/faraday/adapter/httpclient.rb +61 -39
- data/lib/faraday/adapter/net_http.rb +103 -51
- data/lib/faraday/adapter/net_http_persistent.rb +49 -28
- data/lib/faraday/adapter/patron.rb +54 -35
- data/lib/faraday/adapter/rack.rb +28 -12
- data/lib/faraday/adapter/test.rb +86 -53
- data/lib/faraday/adapter/typhoeus.rb +4 -1
- data/lib/faraday/adapter.rb +36 -22
- data/lib/faraday/adapter_registry.rb +28 -0
- data/lib/faraday/autoload.rb +47 -36
- data/lib/faraday/connection.rb +321 -179
- data/lib/faraday/dependency_loader.rb +37 -0
- data/lib/faraday/encoders/flat_params_encoder.rb +94 -0
- data/lib/faraday/encoders/nested_params_encoder.rb +171 -0
- data/lib/faraday/error.rb +21 -79
- data/lib/faraday/logging/formatter.rb +92 -0
- data/lib/faraday/middleware.rb +4 -28
- data/lib/faraday/middleware_registry.rb +129 -0
- data/lib/faraday/options/connection_options.rb +22 -0
- data/lib/faraday/options/env.rb +181 -0
- data/lib/faraday/options/proxy_options.rb +28 -0
- data/lib/faraday/options/request_options.rb +21 -0
- data/lib/faraday/options/ssl_options.rb +59 -0
- data/lib/faraday/options.rb +33 -184
- data/lib/faraday/parameters.rb +4 -197
- data/lib/faraday/rack_builder.rb +66 -55
- data/lib/faraday/request/authorization.rb +42 -30
- data/lib/faraday/request/basic_authentication.rb +14 -7
- data/lib/faraday/request/instrumentation.rb +45 -27
- data/lib/faraday/request/multipart.rb +72 -49
- data/lib/faraday/request/retry.rb +197 -171
- data/lib/faraday/request/token_authentication.rb +15 -10
- data/lib/faraday/request/url_encoded.rb +41 -23
- data/lib/faraday/request.rb +68 -38
- data/lib/faraday/response/logger.rb +22 -69
- data/lib/faraday/response/raise_error.rb +36 -18
- data/lib/faraday/response.rb +22 -15
- data/lib/faraday/upload_io.rb +31 -30
- data/lib/faraday/utils/headers.rb +139 -0
- data/lib/faraday/utils/params_hash.rb +61 -0
- data/lib/faraday/utils.rb +28 -245
- data/lib/faraday.rb +93 -174
- data/spec/external_adapters/faraday_specs_setup.rb +14 -0
- metadata +25 -51
- data/CHANGELOG.md +0 -232
- data/Rakefile +0 -13
- data/lib/faraday/deprecate.rb +0 -109
- data/spec/faraday/deprecate_spec.rb +0 -147
- data/spec/faraday/error_spec.rb +0 -102
- data/spec/faraday/response/raise_error_spec.rb +0 -106
- data/spec/spec_helper.rb +0 -105
- data/test/adapters/default_test.rb +0 -14
- data/test/adapters/em_http_test.rb +0 -30
- data/test/adapters/em_synchrony_test.rb +0 -32
- data/test/adapters/excon_test.rb +0 -30
- data/test/adapters/httpclient_test.rb +0 -34
- data/test/adapters/integration.rb +0 -263
- data/test/adapters/logger_test.rb +0 -136
- data/test/adapters/net_http_persistent_test.rb +0 -114
- data/test/adapters/net_http_test.rb +0 -79
- data/test/adapters/patron_test.rb +0 -40
- data/test/adapters/rack_test.rb +0 -38
- data/test/adapters/test_middleware_test.rb +0 -157
- data/test/adapters/typhoeus_test.rb +0 -38
- data/test/authentication_middleware_test.rb +0 -65
- data/test/composite_read_io_test.rb +0 -109
- data/test/connection_test.rb +0 -738
- data/test/env_test.rb +0 -268
- data/test/helper.rb +0 -75
- data/test/live_server.rb +0 -67
- data/test/middleware/instrumentation_test.rb +0 -88
- data/test/middleware/retry_test.rb +0 -282
- data/test/middleware_stack_test.rb +0 -260
- data/test/multibyte.txt +0 -1
- data/test/options_test.rb +0 -333
- data/test/parameters_test.rb +0 -157
- data/test/request_middleware_test.rb +0 -126
- data/test/response_middleware_test.rb +0 -72
- data/test/strawberry.rb +0 -2
- data/test/utils_test.rb +0 -98
| @@ -1,66 +1,89 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 3 | 
            +
            require File.expand_path('url_encoded', __dir__)
         | 
| 2 4 | 
             
            require 'securerandom'
         | 
| 3 5 |  | 
| 4 6 | 
             
            module Faraday
         | 
| 5 | 
            -
              class Request | 
| 6 | 
            -
                 | 
| 7 | 
            -
                 | 
| 7 | 
            +
              class Request
         | 
| 8 | 
            +
                # Middleware for supporting multi-part requests.
         | 
| 9 | 
            +
                class Multipart < UrlEncoded
         | 
| 10 | 
            +
                  self.mime_type = 'multipart/form-data'
         | 
| 11 | 
            +
                  unless defined? DEFAULT_BOUNDARY_PREFIX
         | 
| 12 | 
            +
                    DEFAULT_BOUNDARY_PREFIX = '-----------RubyMultipartPost'
         | 
| 13 | 
            +
                  end
         | 
| 8 14 |  | 
| 9 | 
            -
             | 
| 10 | 
            -
                   | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
                    env | 
| 15 | 
            +
                  # Checks for files in the payload, otherwise leaves everything untouched.
         | 
| 16 | 
            +
                  #
         | 
| 17 | 
            +
                  # @param env [Faraday::Env]
         | 
| 18 | 
            +
                  def call(env)
         | 
| 19 | 
            +
                    match_content_type(env) do |params|
         | 
| 20 | 
            +
                      env.request.boundary ||= unique_boundary
         | 
| 21 | 
            +
                      env.request_headers[CONTENT_TYPE] +=
         | 
| 22 | 
            +
                        "; boundary=#{env.request.boundary}"
         | 
| 23 | 
            +
                      env.body = create_multipart(env, params)
         | 
| 24 | 
            +
                    end
         | 
| 25 | 
            +
                    @app.call env
         | 
| 14 26 | 
             
                  end
         | 
| 15 | 
            -
                  @app.call env
         | 
| 16 | 
            -
                end
         | 
| 17 27 |  | 
| 18 | 
            -
             | 
| 19 | 
            -
                   | 
| 20 | 
            -
             | 
| 21 | 
            -
                     | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
             | 
| 28 | 
            +
                  # @param env [Faraday::Env]
         | 
| 29 | 
            +
                  def process_request?(env)
         | 
| 30 | 
            +
                    type = request_type(env)
         | 
| 31 | 
            +
                    env.body.respond_to?(:each_key) && !env.body.empty? && (
         | 
| 32 | 
            +
                      (type.empty? && has_multipart?(env.body)) ||
         | 
| 33 | 
            +
                      (type == self.class.mime_type)
         | 
| 34 | 
            +
                    )
         | 
| 35 | 
            +
                  end
         | 
| 25 36 |  | 
| 26 | 
            -
             | 
| 27 | 
            -
                  # | 
| 28 | 
            -
                   | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 37 | 
            +
                  # Returns true if obj is an enumerable with values that are multipart.
         | 
| 38 | 
            +
                  #
         | 
| 39 | 
            +
                  # @param obj [Object]
         | 
| 40 | 
            +
                  # @return [Boolean]
         | 
| 41 | 
            +
                  def has_multipart?(obj) # rubocop:disable Naming/PredicateName
         | 
| 42 | 
            +
                    if obj.respond_to?(:each)
         | 
| 43 | 
            +
                      (obj.respond_to?(:values) ? obj.values : obj).each do |val|
         | 
| 44 | 
            +
                        return true if val.respond_to?(:content_type) || has_multipart?(val)
         | 
| 45 | 
            +
                      end
         | 
| 31 46 | 
             
                    end
         | 
| 47 | 
            +
                    false
         | 
| 32 48 | 
             
                  end
         | 
| 33 | 
            -
                  false
         | 
| 34 | 
            -
                end
         | 
| 35 49 |  | 
| 36 | 
            -
             | 
| 37 | 
            -
                   | 
| 38 | 
            -
                   | 
| 39 | 
            -
                     | 
| 40 | 
            -
             | 
| 41 | 
            -
             | 
| 50 | 
            +
                  # @param env [Faraday::Env]
         | 
| 51 | 
            +
                  # @param params [Hash]
         | 
| 52 | 
            +
                  def create_multipart(env, params)
         | 
| 53 | 
            +
                    boundary = env.request.boundary
         | 
| 54 | 
            +
                    parts = process_params(params) do |key, value|
         | 
| 55 | 
            +
                      Faraday::Parts::Part.new(boundary, key, value)
         | 
| 56 | 
            +
                    end
         | 
| 57 | 
            +
                    parts << Faraday::Parts::EpiloguePart.new(boundary)
         | 
| 42 58 |  | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 46 | 
            -
             | 
| 59 | 
            +
                    body = Faraday::CompositeReadIO.new(parts)
         | 
| 60 | 
            +
                    env.request_headers[Faraday::Env::ContentLength] = body.length.to_s
         | 
| 61 | 
            +
                    body
         | 
| 62 | 
            +
                  end
         | 
| 47 63 |  | 
| 48 | 
            -
             | 
| 49 | 
            -
                   | 
| 50 | 
            -
             | 
| 64 | 
            +
                  # @return [String]
         | 
| 65 | 
            +
                  def unique_boundary
         | 
| 66 | 
            +
                    "#{DEFAULT_BOUNDARY_PREFIX}-#{SecureRandom.hex}"
         | 
| 67 | 
            +
                  end
         | 
| 51 68 |  | 
| 52 | 
            -
             | 
| 53 | 
            -
                   | 
| 54 | 
            -
             | 
| 69 | 
            +
                  # @param params [Hash]
         | 
| 70 | 
            +
                  # @param prefix [String]
         | 
| 71 | 
            +
                  # @param pieces [Array]
         | 
| 72 | 
            +
                  def process_params(params, prefix = nil, pieces = nil, &block)
         | 
| 73 | 
            +
                    params.inject(pieces || []) do |all, (key, value)|
         | 
| 74 | 
            +
                      key = "#{prefix}[#{key}]" if prefix
         | 
| 55 75 |  | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 58 | 
            -
             | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 62 | 
            -
             | 
| 63 | 
            -
             | 
| 76 | 
            +
                      case value
         | 
| 77 | 
            +
                      when Array
         | 
| 78 | 
            +
                        values = value.inject([]) { |a, v| a << [nil, v] }
         | 
| 79 | 
            +
                        process_params(values, key, all, &block)
         | 
| 80 | 
            +
                      when Hash
         | 
| 81 | 
            +
                        process_params(value, key, all, &block)
         | 
| 82 | 
            +
                      else
         | 
| 83 | 
            +
                        # rubocop:disable Performance/RedundantBlockCall
         | 
| 84 | 
            +
                        all << block.call(key, value)
         | 
| 85 | 
            +
                        # rubocop:enable Performance/RedundantBlockCall
         | 
| 86 | 
            +
                      end
         | 
| 64 87 | 
             
                    end
         | 
| 65 88 | 
             
                  end
         | 
| 66 89 | 
             
                end
         | 
| @@ -1,213 +1,239 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 2 |  | 
| 3 3 | 
             
            module Faraday
         | 
| 4 | 
            -
               | 
| 5 | 
            -
             | 
| 6 | 
            -
             | 
| 7 | 
            -
             | 
| 8 | 
            -
             | 
| 9 | 
            -
             | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 12 | 
            -
             | 
| 13 | 
            -
             | 
| 14 | 
            -
             | 
| 15 | 
            -
             | 
| 16 | 
            -
             | 
| 17 | 
            -
             | 
| 18 | 
            -
             | 
| 19 | 
            -
             | 
| 20 | 
            -
             | 
| 21 | 
            -
             | 
| 22 | 
            -
             | 
| 23 | 
            -
             | 
| 24 | 
            -
                 | 
| 25 | 
            -
             | 
| 26 | 
            -
             | 
| 27 | 
            -
             | 
| 28 | 
            -
             | 
| 29 | 
            -
             | 
| 30 | 
            -
             | 
| 31 | 
            -
             | 
| 32 | 
            -
             | 
| 33 | 
            -
                   | 
| 34 | 
            -
             | 
| 35 | 
            -
             | 
| 36 | 
            -
             | 
| 37 | 
            -
             | 
| 38 | 
            -
             | 
| 39 | 
            -
             | 
| 4 | 
            +
              class Request
         | 
| 5 | 
            +
                # Catches exceptions and retries each request a limited number of times.
         | 
| 6 | 
            +
                #
         | 
| 7 | 
            +
                # By default, it retries 2 times and handles only timeout exceptions. It can
         | 
| 8 | 
            +
                # be configured with an arbitrary number of retries, a list of exceptions to
         | 
| 9 | 
            +
                # handle, a retry interval, a percentage of randomness to add to the retry
         | 
| 10 | 
            +
                # interval, and a backoff factor.
         | 
| 11 | 
            +
                #
         | 
| 12 | 
            +
                # @example Configure Retry middleware using intervals
         | 
| 13 | 
            +
                #   Faraday.new do |conn|
         | 
| 14 | 
            +
                #     conn.request(:retry, max: 2,
         | 
| 15 | 
            +
                #                          interval: 0.05,
         | 
| 16 | 
            +
                #                          interval_randomness: 0.5,
         | 
| 17 | 
            +
                #                          backoff_factor: 2,
         | 
| 18 | 
            +
                #                          exceptions: [CustomException, 'Timeout::Error'])
         | 
| 19 | 
            +
                #
         | 
| 20 | 
            +
                #     conn.adapter(:net_http) # NB: Last middleware must be the adapter
         | 
| 21 | 
            +
                #   end
         | 
| 22 | 
            +
                #
         | 
| 23 | 
            +
                # This example will result in a first interval that is random between 0.05
         | 
| 24 | 
            +
                # and 0.075 and a second interval that is random between 0.1 and 0.15.
         | 
| 25 | 
            +
                class Retry < Faraday::Middleware
         | 
| 26 | 
            +
                  DEFAULT_EXCEPTIONS = [
         | 
| 27 | 
            +
                    Errno::ETIMEDOUT, 'Timeout::Error',
         | 
| 28 | 
            +
                    Faraday::TimeoutError, Faraday::RetriableResponse
         | 
| 29 | 
            +
                  ].freeze
         | 
| 30 | 
            +
                  IDEMPOTENT_METHODS = %i[delete get head options put].freeze
         | 
| 31 | 
            +
             | 
| 32 | 
            +
                  # Options contains the configurable parameters for the Retry middleware.
         | 
| 33 | 
            +
                  class Options < Faraday::Options.new(:max, :interval, :max_interval,
         | 
| 34 | 
            +
                                                       :interval_randomness,
         | 
| 35 | 
            +
                                                       :backoff_factor, :exceptions,
         | 
| 36 | 
            +
                                                       :methods, :retry_if, :retry_block,
         | 
| 37 | 
            +
                                                       :retry_statuses)
         | 
| 38 | 
            +
             | 
| 39 | 
            +
                    DEFAULT_CHECK = ->(_env, _exception) { false }
         | 
| 40 | 
            +
             | 
| 41 | 
            +
                    def self.from(value)
         | 
| 42 | 
            +
                      if value.is_a?(Integer)
         | 
| 43 | 
            +
                        new(value)
         | 
| 44 | 
            +
                      else
         | 
| 45 | 
            +
                        super(value)
         | 
| 46 | 
            +
                      end
         | 
| 40 47 | 
             
                    end
         | 
| 41 | 
            -
                  end
         | 
| 42 48 |  | 
| 43 | 
            -
             | 
| 44 | 
            -
             | 
| 45 | 
            -
             | 
| 49 | 
            +
                    def max
         | 
| 50 | 
            +
                      (self[:max] ||= 2).to_i
         | 
| 51 | 
            +
                    end
         | 
| 46 52 |  | 
| 47 | 
            -
             | 
| 48 | 
            -
             | 
| 49 | 
            -
             | 
| 53 | 
            +
                    def interval
         | 
| 54 | 
            +
                      (self[:interval] ||= 0).to_f
         | 
| 55 | 
            +
                    end
         | 
| 50 56 |  | 
| 51 | 
            -
             | 
| 52 | 
            -
             | 
| 53 | 
            -
             | 
| 57 | 
            +
                    def max_interval
         | 
| 58 | 
            +
                      (self[:max_interval] ||= Float::MAX).to_f
         | 
| 59 | 
            +
                    end
         | 
| 54 60 |  | 
| 55 | 
            -
             | 
| 56 | 
            -
             | 
| 57 | 
            -
             | 
| 61 | 
            +
                    def interval_randomness
         | 
| 62 | 
            +
                      (self[:interval_randomness] ||= 0).to_f
         | 
| 63 | 
            +
                    end
         | 
| 58 64 |  | 
| 59 | 
            -
             | 
| 60 | 
            -
             | 
| 61 | 
            -
             | 
| 65 | 
            +
                    def backoff_factor
         | 
| 66 | 
            +
                      (self[:backoff_factor] ||= 1).to_f
         | 
| 67 | 
            +
                    end
         | 
| 62 68 |  | 
| 63 | 
            -
             | 
| 64 | 
            -
             | 
| 65 | 
            -
             | 
| 69 | 
            +
                    def exceptions
         | 
| 70 | 
            +
                      Array(self[:exceptions] ||= DEFAULT_EXCEPTIONS)
         | 
| 71 | 
            +
                    end
         | 
| 66 72 |  | 
| 67 | 
            -
             | 
| 68 | 
            -
             | 
| 69 | 
            -
             | 
| 73 | 
            +
                    def methods
         | 
| 74 | 
            +
                      Array(self[:methods] ||= IDEMPOTENT_METHODS)
         | 
| 75 | 
            +
                    end
         | 
| 70 76 |  | 
| 71 | 
            -
             | 
| 72 | 
            -
             | 
| 73 | 
            -
             | 
| 77 | 
            +
                    def retry_if
         | 
| 78 | 
            +
                      self[:retry_if] ||= DEFAULT_CHECK
         | 
| 79 | 
            +
                    end
         | 
| 74 80 |  | 
| 75 | 
            -
             | 
| 76 | 
            -
             | 
| 77 | 
            -
             | 
| 81 | 
            +
                    def retry_block
         | 
| 82 | 
            +
                      self[:retry_block] ||= proc {}
         | 
| 83 | 
            +
                    end
         | 
| 78 84 |  | 
| 79 | 
            -
             | 
| 80 | 
            -
             | 
| 85 | 
            +
                    def retry_statuses
         | 
| 86 | 
            +
                      Array(self[:retry_statuses] ||= [])
         | 
| 87 | 
            +
                    end
         | 
| 81 88 | 
             
                  end
         | 
| 82 | 
            -
                end
         | 
| 83 89 |  | 
| 84 | 
            -
             | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 | 
            -
             | 
| 88 | 
            -
             | 
| 89 | 
            -
             | 
| 90 | 
            -
             | 
| 91 | 
            -
             | 
| 92 | 
            -
             | 
| 93 | 
            -
             | 
| 94 | 
            -
             | 
| 95 | 
            -
             | 
| 96 | 
            -
             | 
| 97 | 
            -
             | 
| 98 | 
            -
             | 
| 99 | 
            -
             | 
| 100 | 
            -
             | 
| 101 | 
            -
             | 
| 102 | 
            -
             | 
| 103 | 
            -
             | 
| 104 | 
            -
             | 
| 105 | 
            -
             | 
| 106 | 
            -
             | 
| 107 | 
            -
             | 
| 108 | 
            -
             | 
| 109 | 
            -
             | 
| 110 | 
            -
             | 
| 111 | 
            -
             | 
| 112 | 
            -
                   | 
| 113 | 
            -
                   | 
| 114 | 
            -
                   | 
| 115 | 
            -
             | 
| 90 | 
            +
                  # @param app [#call]
         | 
| 91 | 
            +
                  # @param options [Hash]
         | 
| 92 | 
            +
                  # @option options [Integer] :max (2) Maximum number of retries
         | 
| 93 | 
            +
                  # @option options [Integer] :interval (0) Pause in seconds between retries
         | 
| 94 | 
            +
                  # @option options [Integer] :interval_randomness (0) The maximum random
         | 
| 95 | 
            +
                  #   interval amount expressed as a float between
         | 
| 96 | 
            +
                  #   0 and 1 to use in addition to the interval.
         | 
| 97 | 
            +
                  # @option options [Integer] :max_interval (Float::MAX) An upper limit
         | 
| 98 | 
            +
                  #   for the interval
         | 
| 99 | 
            +
                  # @option options [Integer] :backoff_factor (1) The amount to multiply
         | 
| 100 | 
            +
                  #   each successive retry's interval amount by in order to provide backoff
         | 
| 101 | 
            +
                  # @option options [Array] :exceptions ([ Errno::ETIMEDOUT,
         | 
| 102 | 
            +
                  #   'Timeout::Error', Faraday::TimeoutError, Faraday::RetriableResponse])
         | 
| 103 | 
            +
                  #   The list of exceptions to handle. Exceptions can be given as
         | 
| 104 | 
            +
                  #   Class, Module, or String.
         | 
| 105 | 
            +
                  # @option options [Array] :methods (the idempotent HTTP methods
         | 
| 106 | 
            +
                  #   in IDEMPOTENT_METHODS) A list of HTTP methods to retry without
         | 
| 107 | 
            +
                  #   calling retry_if. Pass an empty Array to call retry_if
         | 
| 108 | 
            +
                  #   for all exceptions.
         | 
| 109 | 
            +
                  # @option options [Block] :retry_if (false) block that will receive
         | 
| 110 | 
            +
                  #   the env object and the exception raised
         | 
| 111 | 
            +
                  #   and should decide if the code should retry still the action or
         | 
| 112 | 
            +
                  #   not independent of the retry count. This would be useful
         | 
| 113 | 
            +
                  #   if the exception produced is non-recoverable or if the
         | 
| 114 | 
            +
                  #   the HTTP method called is not idempotent.
         | 
| 115 | 
            +
                  # @option options [Block] :retry_block block that is executed after
         | 
| 116 | 
            +
                  #   every retry. Request environment, middleware options, current number
         | 
| 117 | 
            +
                  #   of retries and the exception is passed to the block as parameters.
         | 
| 118 | 
            +
                  # @option options [Array] :retry_statuses Array of Integer HTTP status
         | 
| 119 | 
            +
                  #   codes or a single Integer value that determines whether to raise
         | 
| 120 | 
            +
                  #   a Faraday::RetriableResponse exception based on the HTTP status code
         | 
| 121 | 
            +
                  #   of an HTTP response.
         | 
| 122 | 
            +
                  def initialize(app, options = nil)
         | 
| 123 | 
            +
                    super(app)
         | 
| 124 | 
            +
                    @options = Options.from(options)
         | 
| 125 | 
            +
                    @errmatch = build_exception_matcher(@options.exceptions)
         | 
| 126 | 
            +
                  end
         | 
| 116 127 |  | 
| 117 | 
            -
             | 
| 118 | 
            -
             | 
| 119 | 
            -
             | 
| 128 | 
            +
                  def calculate_sleep_amount(retries, env)
         | 
| 129 | 
            +
                    retry_after = calculate_retry_after(env)
         | 
| 130 | 
            +
                    retry_interval = calculate_retry_interval(retries)
         | 
| 120 131 |  | 
| 121 | 
            -
             | 
| 132 | 
            +
                    return if retry_after && retry_after > @options.max_interval
         | 
| 122 133 |  | 
| 123 | 
            -
             | 
| 124 | 
            -
             | 
| 134 | 
            +
                    if retry_after && retry_after >= retry_interval
         | 
| 135 | 
            +
                      retry_after
         | 
| 136 | 
            +
                    else
         | 
| 137 | 
            +
                      retry_interval
         | 
| 138 | 
            +
                    end
         | 
| 139 | 
            +
                  end
         | 
| 125 140 |  | 
| 126 | 
            -
             | 
| 127 | 
            -
                   | 
| 128 | 
            -
             | 
| 129 | 
            -
             | 
| 130 | 
            -
                     | 
| 131 | 
            -
             | 
| 132 | 
            -
                       | 
| 133 | 
            -
             | 
| 134 | 
            -
             | 
| 135 | 
            -
             | 
| 136 | 
            -
             | 
| 137 | 
            -
                       | 
| 138 | 
            -
             | 
| 139 | 
            -
                      if  | 
| 140 | 
            -
                         | 
| 141 | 
            -
                         | 
| 141 | 
            +
                  # @param env [Faraday::Env]
         | 
| 142 | 
            +
                  def call(env)
         | 
| 143 | 
            +
                    retries = @options.max
         | 
| 144 | 
            +
                    request_body = env[:body]
         | 
| 145 | 
            +
                    begin
         | 
| 146 | 
            +
                      # after failure env[:body] is set to the response body
         | 
| 147 | 
            +
                      env[:body] = request_body
         | 
| 148 | 
            +
                      @app.call(env).tap do |resp|
         | 
| 149 | 
            +
                        if @options.retry_statuses.include?(resp.status)
         | 
| 150 | 
            +
                          raise Faraday::RetriableResponse.new(nil, resp)
         | 
| 151 | 
            +
                        end
         | 
| 152 | 
            +
                      end
         | 
| 153 | 
            +
                    rescue @errmatch => e
         | 
| 154 | 
            +
                      if retries.positive? && retry_request?(env, e)
         | 
| 155 | 
            +
                        retries -= 1
         | 
| 156 | 
            +
                        rewind_files(request_body)
         | 
| 157 | 
            +
                        @options.retry_block.call(env, @options, retries, e)
         | 
| 158 | 
            +
                        if (sleep_amount = calculate_sleep_amount(retries + 1, env))
         | 
| 159 | 
            +
                          sleep sleep_amount
         | 
| 160 | 
            +
                          retry
         | 
| 161 | 
            +
                        end
         | 
| 142 162 | 
             
                      end
         | 
| 143 | 
            -
                    end
         | 
| 144 163 |  | 
| 145 | 
            -
             | 
| 146 | 
            -
             | 
| 147 | 
            -
             | 
| 148 | 
            -
                      raise
         | 
| 164 | 
            +
                      raise unless e.is_a?(Faraday::RetriableResponse)
         | 
| 165 | 
            +
             | 
| 166 | 
            +
                      e.response
         | 
| 149 167 | 
             
                    end
         | 
| 150 168 | 
             
                  end
         | 
| 151 | 
            -
                end
         | 
| 152 169 |  | 
| 153 | 
            -
             | 
| 154 | 
            -
             | 
| 155 | 
            -
             | 
| 156 | 
            -
             | 
| 157 | 
            -
             | 
| 158 | 
            -
                   | 
| 159 | 
            -
                  ( | 
| 160 | 
            -
                     | 
| 161 | 
            -
             | 
| 162 | 
            -
             | 
| 163 | 
            -
             | 
| 164 | 
            -
             | 
| 165 | 
            -
             | 
| 170 | 
            +
                  # An exception matcher for the rescue clause can usually be any object
         | 
| 171 | 
            +
                  # that responds to `===`, but for Ruby 1.8 it has to be a Class or Module.
         | 
| 172 | 
            +
                  #
         | 
| 173 | 
            +
                  # @param exceptions [Array]
         | 
| 174 | 
            +
                  # @api private
         | 
| 175 | 
            +
                  # @return [Module] an exception matcher
         | 
| 176 | 
            +
                  def build_exception_matcher(exceptions)
         | 
| 177 | 
            +
                    matcher = Module.new
         | 
| 178 | 
            +
                    (
         | 
| 179 | 
            +
                    class << matcher
         | 
| 180 | 
            +
                      self
         | 
| 181 | 
            +
                    end).class_eval do
         | 
| 182 | 
            +
                      define_method(:===) do |error|
         | 
| 183 | 
            +
                        exceptions.any? do |ex|
         | 
| 184 | 
            +
                          if ex.is_a? Module
         | 
| 185 | 
            +
                            error.is_a? ex
         | 
| 186 | 
            +
                          else
         | 
| 187 | 
            +
                            error.class.to_s == ex.to_s
         | 
| 188 | 
            +
                          end
         | 
| 166 189 | 
             
                        end
         | 
| 167 190 | 
             
                      end
         | 
| 168 191 | 
             
                    end
         | 
| 192 | 
            +
                    matcher
         | 
| 169 193 | 
             
                  end
         | 
| 170 | 
            -
                  matcher
         | 
| 171 | 
            -
                end
         | 
| 172 194 |  | 
| 173 | 
            -
             | 
| 195 | 
            +
                  private
         | 
| 174 196 |  | 
| 175 | 
            -
             | 
| 176 | 
            -
             | 
| 177 | 
            -
             | 
| 197 | 
            +
                  def retry_request?(env, exception)
         | 
| 198 | 
            +
                    @options.methods.include?(env[:method]) ||
         | 
| 199 | 
            +
                      @options.retry_if.call(env, exception)
         | 
| 200 | 
            +
                  end
         | 
| 201 | 
            +
             | 
| 202 | 
            +
                  def rewind_files(body)
         | 
| 203 | 
            +
                    return unless body.is_a?(Hash)
         | 
| 178 204 |  | 
| 179 | 
            -
             | 
| 180 | 
            -
             | 
| 181 | 
            -
                  body.each do |_, value|
         | 
| 182 | 
            -
                    if value.is_a? UploadIO
         | 
| 183 | 
            -
                      value.rewind
         | 
| 205 | 
            +
                    body.each do |_, value|
         | 
| 206 | 
            +
                      value.rewind if value.is_a?(UploadIO)
         | 
| 184 207 | 
             
                    end
         | 
| 185 208 | 
             
                  end
         | 
| 186 | 
            -
                end
         | 
| 187 209 |  | 
| 188 | 
            -
             | 
| 189 | 
            -
             | 
| 190 | 
            -
                   | 
| 191 | 
            -
             | 
| 210 | 
            +
                  # MDN spec for Retry-After header:
         | 
| 211 | 
            +
                  # https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Retry-After
         | 
| 212 | 
            +
                  def calculate_retry_after(env)
         | 
| 213 | 
            +
                    response_headers = env[:response_headers]
         | 
| 214 | 
            +
                    return unless response_headers
         | 
| 192 215 |  | 
| 193 | 
            -
             | 
| 216 | 
            +
                    retry_after_value = env[:response_headers]['Retry-After']
         | 
| 194 217 |  | 
| 195 | 
            -
             | 
| 196 | 
            -
             | 
| 197 | 
            -
             | 
| 198 | 
            -
             | 
| 199 | 
            -
             | 
| 200 | 
            -
             | 
| 218 | 
            +
                    # Try to parse date from the header value
         | 
| 219 | 
            +
                    begin
         | 
| 220 | 
            +
                      datetime = DateTime.rfc2822(retry_after_value)
         | 
| 221 | 
            +
                      datetime.to_time - Time.now.utc
         | 
| 222 | 
            +
                    rescue ArgumentError
         | 
| 223 | 
            +
                      retry_after_value.to_f
         | 
| 224 | 
            +
                    end
         | 
| 201 225 | 
             
                  end
         | 
| 202 | 
            -
                end
         | 
| 203 226 |  | 
| 204 | 
            -
             | 
| 205 | 
            -
             | 
| 206 | 
            -
             | 
| 207 | 
            -
             | 
| 208 | 
            -
             | 
| 227 | 
            +
                  def calculate_retry_interval(retries)
         | 
| 228 | 
            +
                    retry_index = @options.max - retries
         | 
| 229 | 
            +
                    current_interval = @options.interval *
         | 
| 230 | 
            +
                                       (@options.backoff_factor**retry_index)
         | 
| 231 | 
            +
                    current_interval = [current_interval, @options.max_interval].min
         | 
| 232 | 
            +
                    random_interval = rand * @options.interval_randomness.to_f *
         | 
| 233 | 
            +
                                      @options.interval
         | 
| 209 234 |  | 
| 210 | 
            -
             | 
| 235 | 
            +
                    current_interval + random_interval
         | 
| 236 | 
            +
                  end
         | 
| 211 237 | 
             
                end
         | 
| 212 238 | 
             
              end
         | 
| 213 239 | 
             
            end
         | 
| @@ -1,15 +1,20 @@ | |
| 1 | 
            +
            # frozen_string_literal: true
         | 
| 2 | 
            +
             | 
| 1 3 | 
             
            module Faraday
         | 
| 2 | 
            -
              class Request | 
| 3 | 
            -
                #  | 
| 4 | 
            -
                 | 
| 5 | 
            -
             | 
| 6 | 
            -
                   | 
| 7 | 
            -
                   | 
| 8 | 
            -
             | 
| 4 | 
            +
              class Request
         | 
| 5 | 
            +
                # TokenAuthentication is a middleware that adds a 'Token' header to a
         | 
| 6 | 
            +
                # Faraday request.
         | 
| 7 | 
            +
                class TokenAuthentication < load_middleware(:authorization)
         | 
| 8 | 
            +
                  # Public
         | 
| 9 | 
            +
                  def self.header(token, options = nil)
         | 
| 10 | 
            +
                    options ||= {}
         | 
| 11 | 
            +
                    options[:token] = token
         | 
| 12 | 
            +
                    super(:Token, options)
         | 
| 13 | 
            +
                  end
         | 
| 9 14 |  | 
| 10 | 
            -
             | 
| 11 | 
            -
             | 
| 15 | 
            +
                  def initialize(app, token, options = nil)
         | 
| 16 | 
            +
                    super(app, token, options)
         | 
| 17 | 
            +
                  end
         | 
| 12 18 | 
             
                end
         | 
| 13 19 | 
             
              end
         | 
| 14 20 | 
             
            end
         | 
| 15 | 
            -
             |