sdr-client 0.16.1 → 0.17.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: d6d44bbb8b087e0045328a2b8625654ad9f7eaadf7c968c5ee5402683dc04d26
4
- data.tar.gz: 9808626f7cad4548ccce883253245c80fc18fac9cd14353dcb008cb8c1cf365a
3
+ metadata.gz: 065c4f605e60c45bb76c98eac741d67f5b7e848a5b59402396b4c7273ba80334
4
+ data.tar.gz: f826dad9e9e758242672f92baffde17f65c347f05013d7420a5ff69459344935
5
5
  SHA512:
6
- metadata.gz: e14bf4032db046e886a7ef02cc1183fab23806bf0b450527ad8e33f396d3cac1f2d254644e9f9e2179d38c04d200a4cdf09eb07515aabf0e8a7a2c7ad9367fd1
7
- data.tar.gz: 39e311b8bb036dd2abff468086103be8e55516b1490d219c4c34e5b68b0c80322af47efd0dcc198ee11e3e24e484d7d206102f20e415ccfa171abd7d61d6fde5
6
+ metadata.gz: '06295472bb1577491a3ea0f40a89cd904c14bb19c2064c226e129487d74828c0335109b2818bd280eed1d80af266d2352a72bc4b4838fa1fea7ff9830f81f1d8'
7
+ data.tar.gz: 6830c8249d6ae651fdbf1c6a62e582db254f663fe6d4316de84c7a1d6a2ba17fd516a129991ca8634a98ccd5857fe59ba56b12341412d0f7732232e93e78fa2d
@@ -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
 
@@ -5,6 +5,7 @@ require 'faraday'
5
5
 
6
6
  require 'sdr_client/version'
7
7
  require 'sdr_client/deposit'
8
+ require 'sdr_client/model_deposit'
8
9
  require 'sdr_client/credentials'
9
10
  require 'sdr_client/login'
10
11
  require 'sdr_client/login_prompt'
@@ -0,0 +1,84 @@
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
+ UploadFiles.new(files: files,
27
+ logger: logger,
28
+ connection: connection,
29
+ mime_types: mime_types).run
30
+ upload_request_dro
31
+ end
32
+
33
+ private
34
+
35
+ attr_reader :request_dro, :files, :url, :token, :logger
36
+
37
+ def check_files_exist
38
+ logger.info('checking to see if files exist')
39
+ files.each do |file_name|
40
+ raise Errno::ENOENT, file_name unless ::File.exist?(file_name)
41
+ end
42
+ end
43
+
44
+ # @return [Hash<Symbol,String>] the result of the metadata call
45
+ # rubocop:disable Metrics/AbcSize
46
+ def upload_request_dro
47
+ request_json = request_dro.to_json
48
+ logger.info("Starting upload metadata: #{request_json}")
49
+ response = connection.post(DRO_PATH, request_json, 'Content-Type' => 'application/json')
50
+ unexpected_response(response) unless response.status == 201
51
+
52
+ logger.info("Response from server: #{response.body}")
53
+
54
+ { druid: JSON.parse(response.body)['druid'], background_job: response.headers['Location'] }
55
+ end
56
+ # rubocop:enable Metrics/AbcSize
57
+
58
+ def unexpected_response(response)
59
+ raise "There was an error with your request: #{response.body}" if response.status == 400
60
+ raise 'There was an error with your credentials. Perhaps they have expired?' if response.status == 401
61
+
62
+ raise "unexpected response: #{response.status} #{response.body}"
63
+ end
64
+
65
+ def connection
66
+ @connection ||= Faraday.new(url: url) do |conn|
67
+ conn.authorization :Bearer, token
68
+ conn.adapter :net_http
69
+ end
70
+ end
71
+
72
+ def mime_types
73
+ @mime_types ||=
74
+ Hash[
75
+ request_dro.structural.contains.map do |file_set|
76
+ file_set.structural.contains.map do |file|
77
+ [file.filename, file.hasMimeType || 'application/octet-stream']
78
+ end
79
+ end.flatten(1)
80
+ ]
81
+ end
82
+ end
83
+ end
84
+ 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
@@ -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.1'
4
+ VERSION = '0.17.0'
5
5
  end
@@ -31,6 +31,7 @@ Gem::Specification.new do |spec|
31
31
  spec.add_dependency 'faraday', '>= 0.16'
32
32
 
33
33
  spec.add_development_dependency 'bundler', '~> 2.0'
34
+ spec.add_development_dependency 'cocina-models'
34
35
  spec.add_development_dependency 'rake', '~> 13.0'
35
36
  spec.add_development_dependency 'rspec', '~> 3.0'
36
37
  spec.add_development_dependency 'rubocop', '~> 0.79.0'
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.16.1
4
+ version: 0.17.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-09 00:00:00.000000000 Z
11
+ date: 2020-03-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-monads
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: cocina-models
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: rake
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -173,12 +187,14 @@ files:
173
187
  - lib/sdr_client/deposit/files/direct_upload_response.rb
174
188
  - lib/sdr_client/deposit/matching_file_grouping_strategy.rb
175
189
  - lib/sdr_client/deposit/metadata_builder.rb
190
+ - lib/sdr_client/deposit/model_process.rb
176
191
  - lib/sdr_client/deposit/process.rb
177
192
  - lib/sdr_client/deposit/request.rb
178
193
  - lib/sdr_client/deposit/single_file_grouping_strategy.rb
179
194
  - lib/sdr_client/deposit/upload_files.rb
180
195
  - lib/sdr_client/login.rb
181
196
  - lib/sdr_client/login_prompt.rb
197
+ - lib/sdr_client/model_deposit.rb
182
198
  - lib/sdr_client/version.rb
183
199
  - sdr-client.gemspec
184
200
  homepage: https://github.com/sul-dlss/sdr-client