lib/dor/services/client.rb in dor-services-client-0.3.0 vs lib/dor/services/client.rb in dor-services-client-0.4.0

- old
+ new

@@ -2,10 +2,11 @@ require 'dor/services/client/version' require 'singleton' require 'faraday' require 'active_support/core_ext/hash/indifferent_access' +require 'dor/services/client/versioned_service' require 'dor/services/client/files' require 'dor/services/client/objects' require 'dor/services/client/release_tags' require 'dor/services/client/workflow' require 'dor/services/client/workspace' @@ -13,34 +14,40 @@ module Dor module Services class Client class Error < StandardError; end + DEFAULT_VERSION = 'v1' + include Singleton def objects - @objects ||= Objects.new(connection: connection) + @objects ||= Objects.new(connection: connection, version: DEFAULT_VERSION) end def files - @files ||= Files.new(connection: connection) + @files ||= Files.new(connection: connection, version: DEFAULT_VERSION) end def workflow - @workflow ||= Workflow.new(connection: connection) + @workflow ||= Workflow.new(connection: connection, version: DEFAULT_VERSION) end def workspace - @workspace ||= Workspace.new(connection: connection) + @workspace ||= Workspace.new(connection: connection, version: DEFAULT_VERSION) end def release_tags - @release_tags ||= ReleaseTags.new(connection: connection) + @release_tags ||= ReleaseTags.new(connection: connection, version: DEFAULT_VERSION) end - def self.configure(url:) + def self.configure(url:, username: nil, password: nil) instance.url = url + instance.username = username + instance.password = password + # Force connection to be re-established when `.configure` is called + instance.connection = nil end # Creates a new object in DOR # @return [HashWithIndifferentAccess] the response, which includes a :pid def self.register(params:) @@ -95,19 +102,27 @@ # @return [boolean] true on success def self.publish(object:) instance.objects.publish(object: object) end - attr_writer :url + attr_writer :url, :username, :password, :connection private + attr_reader :username, :password + def url @url || raise(Error, 'url has not yet been configured') end def connection - @connection ||= Faraday.new(url) + @connection ||= Faraday.new(url) do |conn| + # @note when username & password 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. + conn.adapter Faraday.default_adapter + conn.basic_auth username, password if username && password + end end end end end