lib/preservation/client.rb in preservation-client-2.2.0 vs lib/preservation/client.rb in preservation-client-3.0.0

- old
+ new

@@ -25,10 +25,11 @@ class UnexpectedResponseError < Error; end class ConnectionFailedError < Error; end DEFAULT_API_VERSION = 'v1' + TOKEN_HEADER = 'Authorization' include Singleton # @return [Preservation::Client::Objects] an instance of the `Client::Objects` class def objects @@ -40,37 +41,44 @@ @catalog ||= Catalog.new(connection: connection, api_version: DEFAULT_API_VERSION) end class << self # @param [String] url - def configure(url:) + # @param [String] token a bearer token for HTTP authentication + def configure(url:, token:) instance.url = url + instance.token = token # 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 + attr_writer :url, :connection, :token delegate :update, to: :catalog private def url @url || raise(Error, 'url has not yet been configured') end + def token + @token || raise(Error, 'auth token has not been configured') + end + def connection @connection ||= Faraday.new(url) 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 + builder.headers[TOKEN_HEADER] = "Bearer #{token}" end end def user_agent "preservation-client #{Preservation::Client::VERSION}"