lib/dor/services/client.rb in dor-services-client-6.1.0 vs lib/dor/services/client.rb in dor-services-client-6.2.0

- old
+ new

@@ -77,43 +77,53 @@ end class << self # @param [String] url the base url of the endpoint the client should connect to (required) # @param [String] token a bearer token for HTTP authentication (required) - def configure(url:, token:) + # @param [Boolean] enable_get_retries retries get requests on errors + def configure(url:, token:, enable_get_retries: false) instance.url = url instance.token = token + instance.enable_get_retries = enable_get_retries # Force connection to be re-established when `.configure` is called instance.connection = nil self end delegate :background_job_results, :marcxml, :objects, :object, :virtual_objects, to: :instance end - attr_writer :url, :token, :connection + attr_writer :url, :token, :connection, :enable_get_retries private - attr_reader :token + attr_reader :token, :enable_get_retries def url @url || raise(Error, 'url has not yet been configured') end def connection - @connection ||= Faraday.new(url) do |builder| + @connection ||= ConnectionWrapper.new(connection: build_connection, get_connection: build_connection(with_retries: enable_get_retries)) + end + + def build_connection(with_retries: false) + Faraday.new(url) do |builder| builder.use ErrorFaradayMiddleware builder.use Faraday::Request::UrlEncoded # @note when token & token_header are nil, this line is required else # the Faraday instance will be passed an empty block, which # causes the adapter not to be set. Thus, everything breaks. builder.adapter Faraday.default_adapter builder.headers[:user_agent] = user_agent builder.headers[TOKEN_HEADER] = "Bearer #{token}" + if with_retries + builder.request :retry, max: 4, interval: 1, + backoff_factor: 2, exceptions: ['Faraday::Error', 'Timeout::Error'] + end end end def user_agent "dor-services-client #{Dor::Services::Client::VERSION}"