Sha256: 3499014a5bfc432f38d70cef82a8f3aae125f6817960e76780f1251086262a54

Contents?: true

Size: 1.76 KB

Versions: 10

Compression:

Stored size: 1.76 KB

Contents

# frozen_string_literal: true

require 'deprecation'
require 'active_support/json'
require 'active_support/core_ext/object/json'

module Dor
  module Services
    class Client
      # API calls that are about a repository objects
      class Objects < VersionedService
        # Creates a new object in DOR
        # @param [Hash,Cocina::Models::RequestDRO,Cocina::Models::RequestCollection,Cocina::Models::RequestAPO] passing a hash is deprecated
        # @return [HashWithIndifferentAccess] the response, which includes a :pid
        def register(params:)
          if params.is_a? Hash
            Deprecation.warn(self, 'passing a Hash to register is deprecated and will ' \
              'be removed in dor-services-client 5.0. Use a Cocina::Models object instead.')
          end
          json_str = register_response(params: params)
          json = JSON.parse(json_str)
          return json.with_indifferent_access unless json.key?('type') # Legacy return

          Cocina::Models.build(json)
        end

        private

        # make the registration request to the server
        # @param params [Hash] optional params (see dor-services-app)
        # @raise [UnexpectedResponse] on an unsuccessful response from the server
        # @return [String] the raw JSON from the server
        def register_response(params:)
          resp = connection.post do |req|
            req.url "#{api_version}/objects"
            req.headers['Content-Type'] = 'application/json'
            # asking the service to return JSON (else it'll be plain text)
            req.headers['Accept'] = 'application/json'
            req.body = params.to_json
          end
          return resp.body if resp.success?

          raise_exception_based_on_response!(resp)
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dor-services-client-4.20.0 lib/dor/services/client/objects.rb
dor-services-client-4.19.0 lib/dor/services/client/objects.rb
dor-services-client-4.18.0 lib/dor/services/client/objects.rb
dor-services-client-4.17.1 lib/dor/services/client/objects.rb
dor-services-client-4.17.0 lib/dor/services/client/objects.rb
dor-services-client-4.16.0 lib/dor/services/client/objects.rb
dor-services-client-4.15.0 lib/dor/services/client/objects.rb
dor-services-client-4.14.0 lib/dor/services/client/objects.rb
dor-services-client-4.13.1 lib/dor/services/client/objects.rb
dor-services-client-4.13.0 lib/dor/services/client/objects.rb