sdr-client 0.33.0 → 0.37.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/sdr_client/deposit.rb +6 -1
- data/lib/sdr_client/deposit/{upload_resource.rb → create_resource.rb} +7 -13
- data/lib/sdr_client/deposit/model_process.rb +10 -21
- data/lib/sdr_client/deposit/process.rb +7 -6
- data/lib/sdr_client/deposit/unexpected_response.rb +16 -0
- data/lib/sdr_client/deposit/update_dro_with_file_identifiers.rb +35 -0
- data/lib/sdr_client/deposit/update_resource.rb +48 -0
- data/lib/sdr_client/deposit/upload_files.rb +13 -19
- data/lib/sdr_client/deposit/upload_files_metadata_builder.rb +45 -0
- data/lib/sdr_client/version.rb +1 -1
- data/sdr-client.gemspec +1 -1
- metadata +10 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0317804f79b720e64c6f6625b248c33ce62c90b41f7100b80f5a692f2686ff01'
|
4
|
+
data.tar.gz: 80cfbe267682f2b272a45ae380bb1db9749fd626f4b40279f4ce7bae76a2ed9a
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 113498f64f0e405daa754b8e20ce126e8ed2c1ef2ce2c0cbe0c4cfb1a8343dd261529ec146228b560d7dc13af22a1837cf3b7377c1c62e8da17fbc9b0c493d63
|
7
|
+
data.tar.gz: d2fe355591c76be1c2242d6420eb08742bddee7c784b38419b6d99f1293e82024a17a2c66d0530a92a740805b3121a45dc76a6ca29524b807b76a6054f047d51
|
data/lib/sdr_client/deposit.rb
CHANGED
@@ -52,6 +52,7 @@ module SdrClient
|
|
52
52
|
# rubocop:enable Metrics/MethodLength
|
53
53
|
# rubocop:enable Metrics/ParameterLists
|
54
54
|
|
55
|
+
# @param [Array<String>] files absolute paths to files
|
55
56
|
def self.model_run(request_dro:,
|
56
57
|
files: [],
|
57
58
|
url:,
|
@@ -67,6 +68,7 @@ module SdrClient
|
|
67
68
|
end
|
68
69
|
end
|
69
70
|
require 'json'
|
71
|
+
require 'sdr_client/deposit/create_resource'
|
70
72
|
require 'sdr_client/deposit/single_file_grouping_strategy'
|
71
73
|
require 'sdr_client/deposit/matching_file_grouping_strategy'
|
72
74
|
require 'sdr_client/deposit/files/direct_upload_request'
|
@@ -78,5 +80,8 @@ require 'sdr_client/deposit/request'
|
|
78
80
|
require 'sdr_client/deposit/metadata_builder'
|
79
81
|
require 'sdr_client/deposit/model_process'
|
80
82
|
require 'sdr_client/deposit/process'
|
83
|
+
require 'sdr_client/deposit/unexpected_response'
|
84
|
+
require 'sdr_client/deposit/update_resource'
|
85
|
+
require 'sdr_client/deposit/update_dro_with_file_identifiers'
|
81
86
|
require 'sdr_client/deposit/upload_files'
|
82
|
-
require 'sdr_client/deposit/
|
87
|
+
require 'sdr_client/deposit/upload_files_metadata_builder'
|
@@ -2,8 +2,8 @@
|
|
2
2
|
|
3
3
|
module SdrClient
|
4
4
|
module Deposit
|
5
|
-
#
|
6
|
-
class
|
5
|
+
# Creates a resource (metadata) in SDR
|
6
|
+
class CreateResource
|
7
7
|
DRO_PATH = '/v1/resources'
|
8
8
|
|
9
9
|
def self.run(accession:, metadata:, logger:, connection:)
|
@@ -11,7 +11,7 @@ module SdrClient
|
|
11
11
|
end
|
12
12
|
|
13
13
|
# @param [Boolean] accession should the accessionWF be started
|
14
|
-
# @param [
|
14
|
+
# @param [Cocina::Models::RequestDRO, Cocina::Models::RequestCollection] metadata
|
15
15
|
# @param [Hash<Symbol,String>] the result of the metadata call
|
16
16
|
def initialize(accession:, metadata:, logger:, connection:)
|
17
17
|
@accession = accession
|
@@ -24,7 +24,7 @@ module SdrClient
|
|
24
24
|
# @return [String] job id for the background job result
|
25
25
|
def run
|
26
26
|
response = metadata_request
|
27
|
-
|
27
|
+
UnexpectedResponse.call(response) unless response.status == 201
|
28
28
|
|
29
29
|
logger.info("Response from server: #{response.body}")
|
30
30
|
|
@@ -36,16 +36,10 @@ module SdrClient
|
|
36
36
|
attr_reader :metadata, :logger, :connection
|
37
37
|
|
38
38
|
def metadata_request
|
39
|
-
|
39
|
+
json = metadata.to_json
|
40
|
+
logger.debug("Starting upload metadata: #{json}")
|
40
41
|
|
41
|
-
connection.post(path,
|
42
|
-
end
|
43
|
-
|
44
|
-
def unexpected_response(response)
|
45
|
-
raise "There was an error with your request: #{response.body}" if response.status == 400
|
46
|
-
raise 'There was an error with your credentials. Perhaps they have expired?' if response.status == 401
|
47
|
-
|
48
|
-
raise "unexpected response: #{response.status} #{response.body}"
|
42
|
+
connection.post(path, json, 'Content-Type' => 'application/json')
|
49
43
|
end
|
50
44
|
|
51
45
|
def accession?
|
@@ -24,13 +24,13 @@ module SdrClient
|
|
24
24
|
check_files_exist
|
25
25
|
child_files_match
|
26
26
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
new_request_dro =
|
32
|
-
|
33
|
-
metadata: new_request_dro
|
27
|
+
file_metadata = UploadFilesMetadataBuilder.build(files: files, mime_types: mime_types)
|
28
|
+
upload_responses = UploadFiles.upload(file_metadata: file_metadata,
|
29
|
+
logger: logger,
|
30
|
+
connection: connection)
|
31
|
+
new_request_dro = UpdateDroWithFileIdentifiers.update(request_dro: request_dro, upload_responses: upload_responses)
|
32
|
+
CreateResource.run(accession: @accession,
|
33
|
+
metadata: new_request_dro,
|
34
34
|
logger: logger,
|
35
35
|
connection: connection)
|
36
36
|
end
|
@@ -73,7 +73,9 @@ module SdrClient
|
|
73
73
|
|
74
74
|
# Map of filenames to request files
|
75
75
|
def request_files
|
76
|
-
@request_files ||=
|
76
|
+
@request_files ||= begin
|
77
|
+
return {} unless request_dro.structural
|
78
|
+
|
77
79
|
Hash[
|
78
80
|
request_dro.structural.contains.map do |file_set|
|
79
81
|
file_set.structural.contains.map do |file|
|
@@ -81,20 +83,7 @@ module SdrClient
|
|
81
83
|
end
|
82
84
|
end.flatten(1)
|
83
85
|
]
|
84
|
-
end
|
85
|
-
|
86
|
-
def with_external_identifiers(upload_responses)
|
87
|
-
signed_id_map = Hash[upload_responses.map { |response| [response.filename, response.signed_id] }]
|
88
|
-
|
89
|
-
# Manipulating request_dro as hash since immutable
|
90
|
-
request_dro_hash = request_dro.to_h
|
91
|
-
request_dro_hash[:structural][:contains].each do |file_set|
|
92
|
-
file_set[:structural][:contains].each do |file|
|
93
|
-
file[:externalIdentifier] = signed_id_map[file[:filename]]
|
94
|
-
end
|
95
86
|
end
|
96
|
-
|
97
|
-
Cocina::Models::RequestDRO.new(request_dro_hash)
|
98
87
|
end
|
99
88
|
end
|
100
89
|
end
|
@@ -29,17 +29,18 @@ module SdrClient
|
|
29
29
|
# @return [String] job id for the background job result
|
30
30
|
def run
|
31
31
|
check_files_exist
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
32
|
+
|
33
|
+
file_metadata = UploadFilesMetadataBuilder.build(files: files, mime_types: mime_types)
|
34
|
+
upload_responses = UploadFiles.upload(file_metadata: file_metadata,
|
35
|
+
logger: logger,
|
36
|
+
connection: connection)
|
36
37
|
metadata_builder = MetadataBuilder.new(metadata: metadata,
|
37
38
|
grouping_strategy: grouping_strategy,
|
38
39
|
logger: logger)
|
39
40
|
request = metadata_builder.with_uploads(upload_responses)
|
40
41
|
model = Cocina::Models.build_request(request.as_json.with_indifferent_access)
|
41
|
-
|
42
|
-
metadata:
|
42
|
+
CreateResource.run(accession: @accession,
|
43
|
+
metadata: model,
|
43
44
|
logger: logger,
|
44
45
|
connection: connection)
|
45
46
|
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SdrClient
|
4
|
+
module Deposit
|
5
|
+
# Handles unexpected responses when manipulating resources
|
6
|
+
class UnexpectedResponse
|
7
|
+
# @param [Faraday::Response] response
|
8
|
+
def self.call(response)
|
9
|
+
raise "There was an error with your request: #{response.body}" if response.status == 400
|
10
|
+
raise 'There was an error with your credentials. Perhaps they have expired?' if response.status == 401
|
11
|
+
|
12
|
+
raise "unexpected response: #{response.status} #{response.body}"
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SdrClient
|
4
|
+
module Deposit
|
5
|
+
# Updates a DRO so that the structural metadata references the uploaded file ids
|
6
|
+
class UpdateDroWithFileIdentifiers
|
7
|
+
# @param [Cocina::Model::RequestDRO] request_dro for depositing
|
8
|
+
# @param [Array<Files::DirectUploadResponse>] upload_responses the responses from uploading files
|
9
|
+
# @returns [Cocina::Models::RequestDRO]
|
10
|
+
def self.update(request_dro:, upload_responses:)
|
11
|
+
# Manipulating request_dro as hash since immutable
|
12
|
+
structural = request_dro.to_h[:structural]
|
13
|
+
return request_dro.new({}) unless structural
|
14
|
+
|
15
|
+
signed_ids = signed_id_map(upload_responses)
|
16
|
+
request_dro.new(structural: updated_structural(structural, signed_ids))
|
17
|
+
end
|
18
|
+
|
19
|
+
def self.signed_id_map(upload_responses)
|
20
|
+
Hash[upload_responses.map { |response| [response.filename, response.signed_id] }]
|
21
|
+
end
|
22
|
+
private_class_method :signed_id_map
|
23
|
+
|
24
|
+
def self.updated_structural(structural, signed_ids)
|
25
|
+
structural[:contains].each do |file_set|
|
26
|
+
file_set[:structural][:contains].each do |file|
|
27
|
+
file[:externalIdentifier] = signed_ids[file[:filename]]
|
28
|
+
end
|
29
|
+
end
|
30
|
+
structural
|
31
|
+
end
|
32
|
+
private_class_method :updated_structural
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module SdrClient
|
4
|
+
module Deposit
|
5
|
+
# Updates a resource (metadata) in SDR
|
6
|
+
class UpdateResource
|
7
|
+
DRO_PATH = '/v1/resources/%<id>s'
|
8
|
+
|
9
|
+
def self.run(metadata:, logger:, connection:)
|
10
|
+
new(metadata: metadata, logger: logger, connection: connection).run
|
11
|
+
end
|
12
|
+
|
13
|
+
# @param [Cocina::Models::DRO] metadata
|
14
|
+
# @param [Hash<Symbol,String>] the result of the metadata call
|
15
|
+
def initialize(metadata:, logger:, connection:)
|
16
|
+
@metadata = metadata
|
17
|
+
@logger = logger
|
18
|
+
@connection = connection
|
19
|
+
end
|
20
|
+
|
21
|
+
# @param [Hash<Symbol,String>] the result of the metadata call
|
22
|
+
# @return [String] job id for the background job result
|
23
|
+
def run
|
24
|
+
response = metadata_request
|
25
|
+
UnexpectedResponse.call(response) unless response.status == 202
|
26
|
+
|
27
|
+
logger.info("Response from server: #{response.body}")
|
28
|
+
|
29
|
+
JSON.parse(response.body)['jobId']
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
attr_reader :metadata, :logger, :connection
|
35
|
+
|
36
|
+
def metadata_request
|
37
|
+
json = metadata.to_json
|
38
|
+
logger.debug("Starting upload metadata: #{json}")
|
39
|
+
|
40
|
+
connection.put(path(metadata), json, 'Content-Type' => 'application/json')
|
41
|
+
end
|
42
|
+
|
43
|
+
def path(metadata)
|
44
|
+
format(DRO_PATH, id: metadata.externalIdentifier)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -7,40 +7,34 @@ module SdrClient
|
|
7
7
|
# The file uploading part of a deposit
|
8
8
|
class UploadFiles
|
9
9
|
BLOB_PATH = '/v1/direct_uploads'
|
10
|
-
# @param [
|
10
|
+
# @param [Hash<String,Files::DirectUploadRequest>] the metadata for uploading the files
|
11
11
|
# @param [Logger] logger the logger to use
|
12
12
|
# @param [Connection] connection
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
13
|
+
def self.upload(file_metadata:, logger:, connection:)
|
14
|
+
new(file_metadata: file_metadata, logger: logger, connection: connection).run
|
15
|
+
end
|
16
|
+
|
17
|
+
# @param [Hash<String,Files::DirectUploadRequest>] the metadata for uploading the files
|
18
|
+
# @param [Logger] logger the logger to use
|
19
|
+
# @param [Connection] connection
|
20
|
+
def initialize(file_metadata:, logger:, connection:)
|
21
|
+
@file_metadata = file_metadata
|
17
22
|
@logger = logger
|
18
23
|
@connection = connection
|
19
24
|
end
|
20
25
|
|
21
26
|
# @return [Array<SdrClient::Deposit::Files::DirectUploadResponse>] the responses from the server for the uploads
|
22
27
|
def run
|
23
|
-
|
24
|
-
upload_responses = upload_file_metadata(file_metadata)
|
28
|
+
upload_responses = upload_file_metadata
|
25
29
|
upload_files(upload_responses)
|
26
30
|
upload_responses.values
|
27
31
|
end
|
28
32
|
|
29
33
|
private
|
30
34
|
|
31
|
-
attr_reader :
|
32
|
-
|
33
|
-
def collect_file_metadata
|
34
|
-
files.each_with_object({}) do |path, obj|
|
35
|
-
file_name = ::File.basename(path)
|
36
|
-
obj[path] = Files::DirectUploadRequest.from_file(path,
|
37
|
-
file_name: file_name,
|
38
|
-
content_type: mime_types[file_name])
|
39
|
-
end
|
40
|
-
end
|
35
|
+
attr_reader :logger, :connection, :file_metadata
|
41
36
|
|
42
|
-
|
43
|
-
def upload_file_metadata(file_metadata)
|
37
|
+
def upload_file_metadata
|
44
38
|
Hash[file_metadata.map { |filename, metadata| [filename, direct_upload(metadata.to_json)] }]
|
45
39
|
end
|
46
40
|
|
@@ -0,0 +1,45 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'logger'
|
4
|
+
|
5
|
+
module SdrClient
|
6
|
+
module Deposit
|
7
|
+
# Collecting all the metadata about the files for a deposit
|
8
|
+
class UploadFilesMetadataBuilder
|
9
|
+
# @param [Array<String>] files a list of filepaths to upload
|
10
|
+
# @param [Hash<String,String>] mime_types a map of filenames to mime types
|
11
|
+
# @return [Hash<String, Files::DirectUploadRequest>] the metadata for uploading the files
|
12
|
+
def self.build(files:, mime_types:)
|
13
|
+
new(files: files, mime_types: mime_types).build
|
14
|
+
end
|
15
|
+
|
16
|
+
# @param [Array<String>] files a list of filepaths to upload
|
17
|
+
# @param [Hash<String,String>] mime_types a map of filenames to mime types
|
18
|
+
def initialize(files:, mime_types:)
|
19
|
+
@files = files
|
20
|
+
@mime_types = mime_types
|
21
|
+
end
|
22
|
+
|
23
|
+
attr_reader :files, :mime_types
|
24
|
+
|
25
|
+
# @return [Hash<String, Files::DirectUploadRequest>] the metadata for uploading the files
|
26
|
+
def build
|
27
|
+
files.each_with_object({}) do |path, obj|
|
28
|
+
obj[path] = Files::DirectUploadRequest.from_file(path,
|
29
|
+
file_name: filename_for(path),
|
30
|
+
content_type: mime_type_for(path))
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
# This can be overridden in the case that the file on disk has a different
|
35
|
+
# name than we want to repo to know about.
|
36
|
+
def filename_for(file_path)
|
37
|
+
::File.basename(file_path)
|
38
|
+
end
|
39
|
+
|
40
|
+
def mime_type_for(file_path)
|
41
|
+
mime_types[filename_for(file_path)]
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
data/lib/sdr_client/version.rb
CHANGED
data/sdr-client.gemspec
CHANGED
@@ -28,7 +28,7 @@ Gem::Specification.new do |spec|
|
|
28
28
|
spec.require_paths = ['lib']
|
29
29
|
|
30
30
|
spec.add_dependency 'activesupport'
|
31
|
-
spec.add_dependency 'cocina-models', '~> 0.
|
31
|
+
spec.add_dependency 'cocina-models', '~> 0.42.1'
|
32
32
|
spec.add_dependency 'dry-monads'
|
33
33
|
spec.add_dependency 'faraday', '>= 0.16'
|
34
34
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sdr-client
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.37.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Justin Coyne
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2020-
|
11
|
+
date: 2020-11-17 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -30,14 +30,14 @@ dependencies:
|
|
30
30
|
requirements:
|
31
31
|
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: 0.
|
33
|
+
version: 0.42.1
|
34
34
|
type: :runtime
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
38
|
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: 0.
|
40
|
+
version: 0.42.1
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: dry-monads
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -193,6 +193,7 @@ files:
|
|
193
193
|
- lib/sdr_client/connection.rb
|
194
194
|
- lib/sdr_client/credentials.rb
|
195
195
|
- lib/sdr_client/deposit.rb
|
196
|
+
- lib/sdr_client/deposit/create_resource.rb
|
196
197
|
- lib/sdr_client/deposit/file.rb
|
197
198
|
- lib/sdr_client/deposit/file_metadata_builder.rb
|
198
199
|
- lib/sdr_client/deposit/file_metadata_builder_operations/md5.rb
|
@@ -207,8 +208,11 @@ files:
|
|
207
208
|
- lib/sdr_client/deposit/process.rb
|
208
209
|
- lib/sdr_client/deposit/request.rb
|
209
210
|
- lib/sdr_client/deposit/single_file_grouping_strategy.rb
|
211
|
+
- lib/sdr_client/deposit/unexpected_response.rb
|
212
|
+
- lib/sdr_client/deposit/update_dro_with_file_identifiers.rb
|
213
|
+
- lib/sdr_client/deposit/update_resource.rb
|
210
214
|
- lib/sdr_client/deposit/upload_files.rb
|
211
|
-
- lib/sdr_client/deposit/
|
215
|
+
- lib/sdr_client/deposit/upload_files_metadata_builder.rb
|
212
216
|
- lib/sdr_client/login.rb
|
213
217
|
- lib/sdr_client/login_prompt.rb
|
214
218
|
- lib/sdr_client/version.rb
|
@@ -234,7 +238,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
234
238
|
- !ruby/object:Gem::Version
|
235
239
|
version: '0'
|
236
240
|
requirements: []
|
237
|
-
rubygems_version: 3.1.
|
241
|
+
rubygems_version: 3.1.4
|
238
242
|
signing_key:
|
239
243
|
specification_version: 4
|
240
244
|
summary: The CLI for https://github.com/sul-dlss/sdr-api
|