Class: Dor::Services::Client::Objects

Inherits:
VersionedService show all
Defined in:
lib/dor/services/client/objects.rb

Overview

API calls that are about repository objects

Constant Summary

Constants inherited from VersionedService

VersionedService::EXCEPTION_CLASS, VersionedService::JSON_API_MIME_TYPE

Instance Method Summary collapse

Methods inherited from VersionedService

#async_result, #initialize

Constructor Details

This class inherits a constructor from Dor::Services::Client::VersionedService

Instance Method Details

#find(source_id:, validate: false) ⇒ Cocina::Models::DROWithMetadata, Cocina::Models::CollectionWithMetadata

Find an object by source ID

Parameters:

  • validate (boolean) (defaults to: false)

    validate the response object

Returns:

  • (Cocina::Models::DROWithMetadata, Cocina::Models::CollectionWithMetadata)

    the returned object

Raises:



36
37
38
39
40
41
42
43
44
# File 'lib/dor/services/client/objects.rb', line 36

def find(source_id:, validate: false)
  resp = connection.get do |req|
    req.url "#{objects_path}/find"
    req.params['sourceId'] = source_id
  end
  raise_exception_based_on_response!(resp) unless resp.success?

  build_cocina_from_response(JSON.parse(resp.body), headers: resp.headers, validate: validate)
end

#find_all(druids:, validate: false) ⇒ Array<Cocina::Models::DROWithMetadata,Cocina::Models::CollectionWithMetadata,,Cocina::Models::AdminPolicyWithMetadata>

Find objects by a list of druids

Returns:

  • (Array<Cocina::Models::DROWithMetadata,Cocina::Models::CollectionWithMetadata,,Cocina::Models::AdminPolicyWithMetadata>)

    the returned objects

Raises:



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/dor/services/client/objects.rb', line 49

def find_all(druids:, validate: false) # rubocop:disable Metrics/AbcSize
  return [] if druids.empty?

  resp = connection.post do |req|
    req.url "#{objects_path}/find_all"
    req.headers['Content-Type'] = 'application/json'
    req.body = { 'externalIdentifiers' => druids }.to_json
  end
  raise_exception_based_on_response!(resp) unless resp.success?

  JSON.parse(resp.body).map do |item|
    # The ETag header is used as the lock parameter when instantiating a cocina model with metadata
    build_cocina_from_response(item, headers: { 'ETag' => item['lock'] }, validate: validate)
  end
end

#indexable(druid:, cocina:) ⇒ string

Verify the object can be indexed into Solr

Parameters:

  • druid (String)

    the object’s druid

  • cocina (Cocina::Models::DROWithoutMetadata)

    the cocina object to verify

Returns:

  • (string)

    JSON representation of the descriptive solr document

Raises:

  • (UnprocessableContent)

    when the response is not successful.



86
87
88
89
90
91
92
93
94
95
# File 'lib/dor/services/client/objects.rb', line 86

def indexable(druid:, cocina:)
  resp = connection.post do |req|
    req.url "#{objects_path}/#{druid}/indexable"
    req.headers['Content-Type'] = 'application/json'
    req.body = cocina.to_json
  end
  return true if resp.success?

  raise_exception_based_on_response!(resp)
end

#register(params:, assign_doi: false, validate: false, user_name: nil) ⇒ Cocina::Models::DROWithMetadata, ...

Creates a new object in DOR

Parameters:

  • params (Cocina::Models::RequestDRO, Cocina::Models::RequestCollection, Cocina::Models::RequestAdminPolicy)
  • assign (boolean)

    a doi to the object

  • user_name (string) (defaults to: nil)

    the sunetid of the user registering the object

  • validate (boolean) (defaults to: false)

    validate the response object

Returns:

  • (Cocina::Models::DROWithMetadata, Cocina::Models::CollectionWithMetadata, Cocina::Models::AdminPolicyWithMetadata)

    the returned model



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dor/services/client/objects.rb', line 16

def register(params:, assign_doi: false, validate: false, user_name: nil) # rubocop:disable Metrics/AbcSize
  resp = connection.post do |req|
    req.url objects_path
    req.params = { assign_doi: assign_doi, user_name: user_name }.compact
    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

  raise_exception_based_on_response!(resp) unless resp.success?

  build_cocina_from_response(JSON.parse(resp.body), headers: resp.headers, validate: validate)
end

#statuses(object_ids:) ⇒ Hash<String,VersionStatus>

Retrieves the version statuses for a batch of objects

Parameters:

  • object_ids (Array<String>)

    the druids to get statuses for

Returns:

  • (Hash<String,VersionStatus>)

    Map of druids to statuses

Raises:



69
70
71
72
73
74
75
76
77
78
79
# File 'lib/dor/services/client/objects.rb', line 69

def statuses(object_ids:)
  resp = connection.post do |req|
    req.url "#{objects_path}/versions/status"
    req.headers['Content-Type'] = 'application/json'
    req.body = { externalIdentifiers: object_ids }.to_json
  end

  raise_exception_based_on_response!(resp) unless resp.success?

  JSON.parse(resp.body).transform_values { |status| ObjectVersion::VersionStatus.new(status.symbolize_keys!) }
end