lib/preservation/client.rb in preservation-client-5.1.1 vs lib/preservation/client.rb in preservation-client-5.2.0

- old
+ new

@@ -30,10 +30,11 @@ # Error raised when Faraday gem fails to connect, e.g., on SSL errors or # timeouts class ConnectionFailedError < Error; end DEFAULT_API_VERSION = 'v1' + DEFAULT_TIMEOUT = 300 TOKEN_HEADER = 'Authorization' include Singleton # @return [Preservation::Client::Objects] an instance of the `Client::Objects` class @@ -45,26 +46,28 @@ def catalog @catalog ||= Catalog.new(connection: connection, api_version: DEFAULT_API_VERSION) end class << self - # @param [String] url + # @param [String] url the endpoint URL # @param [String] token a bearer token for HTTP authentication - def configure(url:, token:) + # @param [Integer] read_timeout the value in seconds of the read timeout + def configure(url:, token:, read_timeout: DEFAULT_TIMEOUT) instance.url = url instance.token = token + instance.read_timeout = read_timeout # Force connection to be re-established when `.configure` is called instance.connection = nil self end delegate :objects, :update, to: :instance end - attr_writer :url, :connection, :token + attr_writer :connection, :read_timeout, :token, :url delegate :update, to: :catalog private @@ -74,11 +77,15 @@ def token @token || raise(Error, 'auth token has not been configured') end + def read_timeout + @read_timeout || raise(Error, 'read timeout has not been configured') + end + def connection - @connection ||= Faraday.new(url, request: { read_timeout: 300 }) do |builder| + @connection ||= Faraday.new(url, request: { read_timeout: read_timeout }) do |builder| builder.use ErrorFaradayMiddleware builder.use Faraday::Request::UrlEncoded builder.use Faraday::Response::RaiseError # raise exceptions on 40x, 50x responses builder.adapter Faraday.default_adapter builder.headers[:user_agent] = user_agent