Sha256: 1a13d2af1de0e7c9978191a1419bd887ac349472f15b6e1d7fb5f1fd488455ae

Contents?: true

Size: 1.95 KB

Versions: 2

Compression:

Stored size: 1.95 KB

Contents

# frozen_string_literal: true

module SdrClient
  module Deposit
    # Updates a resource (metadata) in SDR
    class UpdateResource
      DRO_PATH = '/v1/resources/%<id>s'

      def self.run(...)
        new(...).run
      end

      # @param [Cocina::Models::DRO] metadata
      # @param [Hash<Symbol,String>] the result of the metadata call
      # @param [String] version_description
      # @param [String] user_versions action (none, new, update) to take for user version when closing version
      def initialize(metadata:, logger:, connection:, version_description: nil, user_versions: nil)
        @metadata = metadata
        @logger = logger
        @connection = connection
        @version_description = version_description
        @user_versions = user_versions
      end

      # @param [Hash<Symbol,String>] the result of the metadata call
      # @return [String] job id for the background job result
      def run
        response = metadata_request
        UnexpectedResponse.call(response) unless response.status == 202

        logger.info("Response from server: #{response.body}")

        JSON.parse(response.body)['jobId']
      end

      private

      attr_reader :metadata, :logger, :connection, :version_description, :user_versions

      # rubocop:disable Metrics/AbcSize
      def metadata_request
        json = metadata.to_json
        logger.debug("Starting update metadata: #{json}")

        connection.put(path(metadata), json,
                       'Content-Type' => 'application/json',
                       'X-Cocina-Models-Version' => Cocina::Models::VERSION) do |req|
                         req.params['versionDescription'] = version_description if version_description
                         req.params['user_versions'] = user_versions if user_versions.present?
                       end
      end
      # rubocop:enable Metrics/AbcSize

      def path(metadata)
        format(DRO_PATH, id: metadata.externalIdentifier)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sdr-client-2.17.0 lib/sdr_client/deposit/update_resource.rb
sdr-client-2.16.0 lib/sdr_client/deposit/update_resource.rb