sdr-client 0.16.0 → 0.18.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 51cea2d7a34db14a8fbef003f8e02b8db1483344724e2d1201fce2b3075f7fb7
4
- data.tar.gz: 0654575a55dca1ee8bc9764c02a5d512e90cba6ad257780f1938643968e8dd58
3
+ metadata.gz: 8e763b0d86f9518951eff07ede0ff51629423c8b6855f98eb04225dbdf5b050b
4
+ data.tar.gz: e1fe1931e7e31cf0d35e647a606a01c7613a1ffcdaf2e4632c0a2291fa03636f
5
5
  SHA512:
6
- metadata.gz: 6b304533c8b61c34b03a4e40875b2bd3c8d2e50bf5fba6c104327a769248aaaaace4151e5a8033f2b214e87316f8065b63c880057a0a631adb94a74ff779097a
7
- data.tar.gz: b43e02b8160340b06bebf30642f65f885f73c25398d5f5190bd7453f0a99939f1d824535eefe83601b572f7e221b79069459d2959bef7401bd45bf5600ef2fd9
6
+ metadata.gz: 4ee835ce52cdf35db4b1216ceac46b8bba2cae28f4c6ae4dec0dfeb58d1a258cb9070b3187d96ae367788f6d69084279a401dd5f72e2f57f5fbb26d9e39ee84c
7
+ data.tar.gz: bfb5da340dd85568daa1c367e374619d12a871900242da31b68f404ff38afc8068a8999fced8f965eafb47d7a4cfbe65e26e10fbea8cdbf8853313176a554f88
@@ -13,6 +13,7 @@ Layout/LineLength:
13
13
  Metrics/BlockLength:
14
14
  Exclude:
15
15
  - 'spec/**/*'
16
+ - 'sdr-client.gemspec'
16
17
  ExcludedMethods:
17
18
  - 'OptionParser.new'
18
19
 
@@ -2,9 +2,12 @@
2
2
 
3
3
  require 'dry/monads'
4
4
  require 'faraday'
5
+ require 'active_support'
6
+ require 'active_support/core_ext/object/json'
5
7
 
6
8
  require 'sdr_client/version'
7
9
  require 'sdr_client/deposit'
10
+ require 'sdr_client/model_deposit'
8
11
  require 'sdr_client/credentials'
9
12
  require 'sdr_client/login'
10
13
  require 'sdr_client/login_prompt'
@@ -5,10 +5,11 @@ require 'logger'
5
5
  module SdrClient
6
6
  # The namespace for the "deposit" command
7
7
  module Deposit
8
+ BOOK_TYPE = 'http://cocina.sul.stanford.edu/models/book.jsonld'
8
9
  # rubocop:disable Metrics/ParameterLists
9
10
  # rubocop:disable Metrics/MethodLength
10
11
  def self.run(label: nil,
11
- type: 'http://cocina.sul.stanford.edu/models/book.jsonld',
12
+ type: BOOK_TYPE,
12
13
  viewing_direction: nil,
13
14
  access: 'dark',
14
15
  use_statement: nil,
@@ -16,6 +16,8 @@ module SdrClient
16
16
  @grouping_strategy = grouping_strategy
17
17
  end
18
18
 
19
+ # @param [UploadFiles] upload_responses the uploaded file information
20
+ # @return [Request] the metadata with fileset information added in.
19
21
  def with_uploads(upload_responses)
20
22
  file_sets = build_filesets(uploads: upload_responses)
21
23
  metadata.with_file_sets(file_sets)
@@ -32,7 +34,16 @@ module SdrClient
32
34
  grouped_uploads.map.with_index(1) do |upload_group, i|
33
35
  FileSet.new(uploads: upload_group,
34
36
  uploads_metadata: metadata_group(upload_group),
35
- label: "Object #{i}")
37
+ label: label(i))
38
+ end
39
+ end
40
+
41
+ def label(index)
42
+ case metadata.type
43
+ when BOOK_TYPE
44
+ "Page #{index}"
45
+ else
46
+ "Object #{index}"
36
47
  end
37
48
  end
38
49
 
@@ -0,0 +1,124 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module SdrClient
6
+ module Deposit
7
+ # The process for doing a deposit from a Cocina Model
8
+ class ModelProcess
9
+ DRO_PATH = '/v1/resources'
10
+ # @param [Cocina::Model::RequestDRO] request_dro for depositing
11
+ # @param [String] url the server to send to
12
+ # @param [String] token the bearer auth token for the server
13
+ # @param [Array<String>] files a list of file names to upload
14
+ # @param [Logger] logger the logger to use
15
+ def initialize(request_dro:, url:,
16
+ token:, files: [], logger: Logger.new(STDOUT))
17
+ @files = files
18
+ @url = url
19
+ @token = token
20
+ @request_dro = request_dro
21
+ @logger = logger
22
+ end
23
+
24
+ def run
25
+ check_files_exist
26
+ child_files_match
27
+
28
+ upload_responses = UploadFiles.new(files: files,
29
+ logger: logger,
30
+ connection: connection,
31
+ mime_types: mime_types).run
32
+ new_request_dro = with_external_identifiers(upload_responses)
33
+ upload_request_dro(new_request_dro.to_json)
34
+ end
35
+
36
+ private
37
+
38
+ attr_reader :request_dro, :files, :url, :token, :logger
39
+
40
+ def check_files_exist
41
+ logger.info('checking to see if files exist')
42
+ files.each do |file_name|
43
+ raise Errno::ENOENT, file_name unless ::File.exist?(file_name)
44
+ end
45
+ end
46
+
47
+ def child_files_match
48
+ # Files without request files.
49
+ files.each do |filepath|
50
+ filename = ::File.basename(filepath)
51
+
52
+ raise "Request file not provided for #{filepath}" if request_files[filename].nil?
53
+ end
54
+
55
+ # Request files without files
56
+ filenames = files.map { |filepath| ::File.basename(filepath) }
57
+ request_files.keys.each do |request_filename|
58
+ raise "File not provided for request file #{request_filename}" unless filenames.include?(request_filename)
59
+ end
60
+ end
61
+
62
+ # @return [Hash<Symbol,String>] the result of the metadata call
63
+ def upload_request_dro(request_json)
64
+ logger.info("Starting upload metadata: #{request_json}")
65
+ response = connection.post(DRO_PATH, request_json, 'Content-Type' => 'application/json')
66
+ unexpected_response(response) unless response.status == 201
67
+
68
+ logger.info("Response from server: #{response.body}")
69
+
70
+ { druid: JSON.parse(response.body)['druid'], background_job: response.headers['Location'] }
71
+ end
72
+
73
+ def unexpected_response(response)
74
+ raise "There was an error with your request: #{response.body}" if response.status == 400
75
+ raise 'There was an error with your credentials. Perhaps they have expired?' if response.status == 401
76
+
77
+ raise "unexpected response: #{response.status} #{response.body}"
78
+ end
79
+
80
+ def connection
81
+ @connection ||= Faraday.new(url: url) do |conn|
82
+ conn.authorization :Bearer, token
83
+ conn.adapter :net_http
84
+ end
85
+ end
86
+
87
+ # Map of filenames to mimetypes
88
+ def mime_types
89
+ @mime_types ||=
90
+ Hash[
91
+ request_files.map do |filename, file|
92
+ [filename, file.hasMimeType || 'application/octet-stream']
93
+ end
94
+ ]
95
+ end
96
+
97
+ # Map of filenames to request files
98
+ def request_files
99
+ @request_files ||=
100
+ Hash[
101
+ request_dro.structural.contains.map do |file_set|
102
+ file_set.structural.contains.map do |file|
103
+ [file.filename, file]
104
+ end
105
+ end.flatten(1)
106
+ ]
107
+ end
108
+
109
+ def with_external_identifiers(upload_responses)
110
+ signed_id_map = Hash[upload_responses.map { |response| [response.filename, response.signed_id] }]
111
+
112
+ # Manipulating request_dro as hash since immutable
113
+ request_dro_hash = request_dro.to_h
114
+ request_dro_hash[:structural][:contains].each do |file_set|
115
+ file_set[:structural][:contains].each do |file|
116
+ file[:externalIdentifier] = signed_id_map[file[:filename]]
117
+ end
118
+ end
119
+
120
+ Cocina::Models::RequestDRO.new(request_dro_hash)
121
+ end
122
+ end
123
+ end
124
+ end
@@ -30,7 +30,7 @@ module SdrClient
30
30
  upload_responses = UploadFiles.new(files: files,
31
31
  logger: logger,
32
32
  connection: connection,
33
- metadata: metadata).run
33
+ mime_types: mime_types).run
34
34
  metadata_builder = MetadataBuilder.new(metadata: metadata,
35
35
  grouping_strategy: grouping_strategy,
36
36
  logger: logger)
@@ -74,6 +74,16 @@ module SdrClient
74
74
  conn.adapter :net_http
75
75
  end
76
76
  end
77
+
78
+ def mime_types
79
+ @mime_types ||=
80
+ Hash[
81
+ files.map do |filepath|
82
+ filename = ::File.basename(filepath)
83
+ [filename, metadata.for(filename)['mime_type']]
84
+ end
85
+ ]
86
+ end
77
87
  end
78
88
  end
79
89
  end
@@ -79,10 +79,12 @@ module SdrClient
79
79
  files_metadata.fetch(filename, {})
80
80
  end
81
81
 
82
+ attr_reader :type
83
+
82
84
  private
83
85
 
84
86
  attr_reader :access, :label, :file_sets, :source_id, :catkey, :apo, :collection,
85
- :type, :files_metadata, :embargo_release_date, :embargo_access,
87
+ :files_metadata, :embargo_release_date, :embargo_access,
86
88
  :viewing_direction, :use_statement, :copyright
87
89
 
88
90
  def administrative
@@ -7,13 +7,13 @@ module SdrClient
7
7
  # The file uploading part of a deposit
8
8
  class UploadFiles
9
9
  BLOB_PATH = '/v1/direct_uploads'
10
- # @param [Array<String>] files a list of file names to upload
10
+ # @param [Array<String>] files a list of filepaths to upload
11
11
  # @param [Logger] logger the logger to use
12
12
  # @param [Faraday::Connection] connection
13
- # @param [Request] metadata information about the object
14
- def initialize(files:, metadata:, logger:, connection:)
13
+ # @param [Hash<String,String] mime_types a map of filenames to mime types
14
+ def initialize(files:, mime_types:, logger:, connection:)
15
15
  @files = files
16
- @metadata = metadata
16
+ @mime_types = mime_types
17
17
  @logger = logger
18
18
  @connection = connection
19
19
  end
@@ -28,14 +28,14 @@ module SdrClient
28
28
 
29
29
  private
30
30
 
31
- attr_reader :files, :metadata, :logger, :connection
31
+ attr_reader :files, :mime_types, :logger, :connection
32
32
 
33
33
  def collect_file_metadata
34
34
  files.each_with_object({}) do |path, obj|
35
35
  file_name = ::File.basename(path)
36
36
  obj[path] = Files::DirectUploadRequest.from_file(path,
37
37
  file_name: file_name,
38
- content_type: metadata.for(file_name)['mime_type'])
38
+ content_type: mime_types[file_name])
39
39
  end
40
40
  end
41
41
 
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module SdrClient
6
+ # The namespace for the "deposit" command
7
+ module Deposit
8
+ def self.model_run(request_dro:,
9
+ files: [],
10
+ url:,
11
+ logger: Logger.new(STDOUT))
12
+ token = Credentials.read
13
+
14
+ ModelProcess.new(request_dro: request_dro, url: url, token: token, files: files, logger: logger).run
15
+ end
16
+ end
17
+ end
18
+ require 'sdr_client/deposit/model_process'
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrClient
4
- VERSION = '0.16.0'
4
+ VERSION = '0.18.0'
5
5
  end
@@ -27,6 +27,8 @@ Gem::Specification.new do |spec|
27
27
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
28
28
  spec.require_paths = ['lib']
29
29
 
30
+ spec.add_dependency 'activesupport'
31
+ spec.add_dependency 'cocina-models', '~> 0.29.0'
30
32
  spec.add_dependency 'dry-monads'
31
33
  spec.add_dependency 'faraday', '>= 0.16'
32
34
 
metadata CHANGED
@@ -1,15 +1,43 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sdr-client
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.16.0
4
+ version: 0.18.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-03-05 00:00:00.000000000 Z
11
+ date: 2020-03-13 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cocina-models
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: 0.29.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: 0.29.0
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: dry-monads
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -173,12 +201,14 @@ files:
173
201
  - lib/sdr_client/deposit/files/direct_upload_response.rb
174
202
  - lib/sdr_client/deposit/matching_file_grouping_strategy.rb
175
203
  - lib/sdr_client/deposit/metadata_builder.rb
204
+ - lib/sdr_client/deposit/model_process.rb
176
205
  - lib/sdr_client/deposit/process.rb
177
206
  - lib/sdr_client/deposit/request.rb
178
207
  - lib/sdr_client/deposit/single_file_grouping_strategy.rb
179
208
  - lib/sdr_client/deposit/upload_files.rb
180
209
  - lib/sdr_client/login.rb
181
210
  - lib/sdr_client/login_prompt.rb
211
+ - lib/sdr_client/model_deposit.rb
182
212
  - lib/sdr_client/version.rb
183
213
  - sdr-client.gemspec
184
214
  homepage: https://github.com/sul-dlss/sdr-client