sdr-client 0.17.0 → 0.20.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: 065c4f605e60c45bb76c98eac741d67f5b7e848a5b59402396b4c7273ba80334
4
- data.tar.gz: f826dad9e9e758242672f92baffde17f65c347f05013d7420a5ff69459344935
3
+ metadata.gz: e40c3f7aec5b647a05dd4c2e3289a7ed3173c2b18ac57fb74dd218cb7226db0f
4
+ data.tar.gz: 9c5029ae7bdcde2e2fd2d9261a4a282b46fc6dff09be20c7622f80413bbccb01
5
5
  SHA512:
6
- metadata.gz: '06295472bb1577491a3ea0f40a89cd904c14bb19c2064c226e129487d74828c0335109b2818bd280eed1d80af266d2352a72bc4b4838fa1fea7ff9830f81f1d8'
7
- data.tar.gz: 6830c8249d6ae651fdbf1c6a62e582db254f663fe6d4316de84c7a1d6a2ba17fd516a129991ca8634a98ccd5857fe59ba56b12341412d0f7732232e93e78fa2d
6
+ metadata.gz: 9d270769997103bac784faaba63e49616c980170696de6aaa6d5c428f3fa97a5e6d14ab53572cb0b61a05efb1398d819b3b5d1caa116c7138555f4f95f6bcf99
7
+ data.tar.gz: 791d45aa87675605a7facb2ccee548780e9a6863c4c62f8f428d0b182dfdbc4c087b546e71c60a868765ab47736acb048d23fdcc46b572b47ab769946d9c19cb
@@ -2,6 +2,8 @@
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,6 +12,7 @@ require 'sdr_client/credentials'
10
12
  require 'sdr_client/login'
11
13
  require 'sdr_client/login_prompt'
12
14
  require 'sdr_client/cli'
15
+ require 'sdr_client/connection'
13
16
 
14
17
  module SdrClient
15
18
  class Error < StandardError; end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SdrClient
4
+ # The connection to the server
5
+ class Connection
6
+ include Dry::Monads[:result]
7
+
8
+ def initialize(url:, token: Credentials.read)
9
+ @url = url
10
+ @token = token
11
+ end
12
+
13
+ def connection
14
+ @connection ||= Faraday.new(url: url) do |conn|
15
+ conn.authorization :Bearer, token
16
+ conn.adapter :net_http
17
+ end
18
+ end
19
+
20
+ # This is only available to certain blessed accounts (argo) as it gives the
21
+ # token that allows you to act as any other user. Thus the caller must authenticate
22
+ # the user (e.g. using Shibboleth) before calling this method with their email address.
23
+ # @param [String] the email address of the person to proxy to.
24
+ # @return [Result] the token for the account
25
+ def proxy(to)
26
+ response = connection.post("/v1/auth/proxy?to=#{to}")
27
+ case response.status
28
+ when 200
29
+ Success(response.body)
30
+ else
31
+ Failure("Status: #{response.status}\n#{response.body}")
32
+ end
33
+ end
34
+
35
+ delegate :put, :post, to: :connection
36
+
37
+ private
38
+
39
+ attr_reader :url, :token
40
+ end
41
+ end
@@ -25,8 +25,6 @@ module SdrClient
25
25
  files_metadata: {},
26
26
  grouping_strategy: SingleFileGroupingStrategy,
27
27
  logger: Logger.new(STDOUT))
28
- token = Credentials.read
29
-
30
28
  augmented_metadata = FileMetadataBuilder.build(files: files, files_metadata: files_metadata)
31
29
  metadata = Request.new(label: label,
32
30
  type: type,
@@ -41,7 +39,8 @@ module SdrClient
41
39
  embargo_access: embargo_access,
42
40
  viewing_direction: viewing_direction,
43
41
  files_metadata: augmented_metadata)
44
- Process.new(metadata: metadata, url: url, token: token, files: files,
42
+ connection = Connection.new(url: url)
43
+ Process.new(metadata: metadata, connection: connection, files: files,
45
44
  grouping_strategy: grouping_strategy, logger: logger).run
46
45
  end
47
46
  # rubocop:enable Metrics/MethodLength
@@ -8,31 +8,31 @@ module SdrClient
8
8
  class ModelProcess
9
9
  DRO_PATH = '/v1/resources'
10
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
11
+ # @param [Connection] connection the connection to use
13
12
  # @param [Array<String>] files a list of file names to upload
14
13
  # @param [Logger] logger the logger to use
15
- def initialize(request_dro:, url:,
16
- token:, files: [], logger: Logger.new(STDOUT))
14
+ def initialize(request_dro:, connection:, files: [], logger: Logger.new(STDOUT))
17
15
  @files = files
18
- @url = url
19
- @token = token
16
+ @connection = connection
20
17
  @request_dro = request_dro
21
18
  @logger = logger
22
19
  end
23
20
 
24
21
  def run
25
22
  check_files_exist
26
- UploadFiles.new(files: files,
27
- logger: logger,
28
- connection: connection,
29
- mime_types: mime_types).run
30
- upload_request_dro
23
+ child_files_match
24
+
25
+ upload_responses = UploadFiles.new(files: files,
26
+ logger: logger,
27
+ connection: connection,
28
+ mime_types: mime_types).run
29
+ new_request_dro = with_external_identifiers(upload_responses)
30
+ upload_request_dro(new_request_dro.to_json)
31
31
  end
32
32
 
33
33
  private
34
34
 
35
- attr_reader :request_dro, :files, :url, :token, :logger
35
+ attr_reader :request_dro, :files, :logger, :connection
36
36
 
37
37
  def check_files_exist
38
38
  logger.info('checking to see if files exist')
@@ -41,10 +41,23 @@ module SdrClient
41
41
  end
42
42
  end
43
43
 
44
+ def child_files_match
45
+ # Files without request files.
46
+ files.each do |filepath|
47
+ filename = ::File.basename(filepath)
48
+
49
+ raise "Request file not provided for #{filepath}" if request_files[filename].nil?
50
+ end
51
+
52
+ # Request files without files
53
+ filenames = files.map { |filepath| ::File.basename(filepath) }
54
+ request_files.keys.each do |request_filename|
55
+ raise "File not provided for request file #{request_filename}" unless filenames.include?(request_filename)
56
+ end
57
+ end
58
+
44
59
  # @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
60
+ def upload_request_dro(request_json)
48
61
  logger.info("Starting upload metadata: #{request_json}")
49
62
  response = connection.post(DRO_PATH, request_json, 'Content-Type' => 'application/json')
50
63
  unexpected_response(response) unless response.status == 201
@@ -53,7 +66,6 @@ module SdrClient
53
66
 
54
67
  { druid: JSON.parse(response.body)['druid'], background_job: response.headers['Location'] }
55
68
  end
56
- # rubocop:enable Metrics/AbcSize
57
69
 
58
70
  def unexpected_response(response)
59
71
  raise "There was an error with your request: #{response.body}" if response.status == 400
@@ -62,23 +74,41 @@ module SdrClient
62
74
  raise "unexpected response: #{response.status} #{response.body}"
63
75
  end
64
76
 
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
-
77
+ # Map of filenames to mimetypes
72
78
  def mime_types
73
79
  @mime_types ||=
74
80
  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)
81
+ request_files.map do |filename, file|
82
+ [filename, file.hasMimeType || 'application/octet-stream']
83
+ end
80
84
  ]
81
85
  end
86
+
87
+ # Map of filenames to request files
88
+ def request_files
89
+ @request_files ||=
90
+ Hash[
91
+ request_dro.structural.contains.map do |file_set|
92
+ file_set.structural.contains.map do |file|
93
+ [file.filename, file]
94
+ end
95
+ end.flatten(1)
96
+ ]
97
+ end
98
+
99
+ def with_external_identifiers(upload_responses)
100
+ signed_id_map = Hash[upload_responses.map { |response| [response.filename, response.signed_id] }]
101
+
102
+ # Manipulating request_dro as hash since immutable
103
+ request_dro_hash = request_dro.to_h
104
+ request_dro_hash[:structural][:contains].each do |file_set|
105
+ file_set[:structural][:contains].each do |file|
106
+ file[:externalIdentifier] = signed_id_map[file[:filename]]
107
+ end
108
+ end
109
+
110
+ Cocina::Models::RequestDRO.new(request_dro_hash)
111
+ end
82
112
  end
83
113
  end
84
114
  end
@@ -9,21 +9,17 @@ module SdrClient
9
9
  DRO_PATH = '/v1/resources'
10
10
  # @param [Request] metadata information about the object
11
11
  # @param [Class] grouping_strategy class whose run method groups an array of uploads
12
- # @param [String] url the server to send to
13
- # @param [String] token the bearer auth token for the server
12
+ # @param [String] connection the server connection to use
14
13
  # @param [Array<String>] files a list of file names to upload
15
14
  # @param [Logger] logger the logger to use
16
- # rubocop:disable Metrics/ParameterLists
17
- def initialize(metadata:, grouping_strategy: SingleFileGroupingStrategy, url:,
18
- token:, files: [], logger: Logger.new(STDOUT))
15
+ def initialize(metadata:, grouping_strategy: SingleFileGroupingStrategy,
16
+ connection:, files: [], logger: Logger.new(STDOUT))
19
17
  @files = files
20
- @url = url
21
- @token = token
18
+ @connection = connection
22
19
  @metadata = metadata
23
20
  @logger = logger
24
21
  @grouping_strategy = grouping_strategy
25
22
  end
26
- # rubocop:enable Metrics/ParameterLists
27
23
 
28
24
  def run
29
25
  check_files_exist
@@ -40,7 +36,7 @@ module SdrClient
40
36
 
41
37
  private
42
38
 
43
- attr_reader :metadata, :files, :url, :token, :logger, :grouping_strategy
39
+ attr_reader :metadata, :files, :connection, :logger, :grouping_strategy
44
40
 
45
41
  def check_files_exist
46
42
  logger.info('checking to see if files exist')
@@ -68,13 +64,6 @@ module SdrClient
68
64
  raise "unexpected response: #{response.status} #{response.body}"
69
65
  end
70
66
 
71
- def connection
72
- @connection ||= Faraday.new(url: url) do |conn|
73
- conn.authorization :Bearer, token
74
- conn.adapter :net_http
75
- end
76
- end
77
-
78
67
  def mime_types
79
68
  @mime_types ||=
80
69
  Hash[
@@ -9,7 +9,7 @@ module SdrClient
9
9
  BLOB_PATH = '/v1/direct_uploads'
10
10
  # @param [Array<String>] files a list of filepaths to upload
11
11
  # @param [Logger] logger the logger to use
12
- # @param [Faraday::Connection] connection
12
+ # @param [Connection] connection
13
13
  # @param [Hash<String,String] mime_types a map of filenames to mime types
14
14
  def initialize(files:, mime_types:, logger:, connection:)
15
15
  @files = files
@@ -7,12 +7,12 @@ module SdrClient
7
7
  extend Dry::Monads[:result]
8
8
 
9
9
  # @return [Result] the status of the call
10
- def self.run(url:, login_service: LoginPrompt)
10
+ def self.run(url:, login_service: LoginPrompt, credential_store: Credentials)
11
11
  request_json = JSON.generate(login_service.run)
12
12
  response = Faraday.post(url + LOGIN_PATH, request_json, 'Content-Type' => 'application/json')
13
13
  case response.status
14
14
  when 200
15
- Credentials.write(response.body)
15
+ credential_store.write(response.body)
16
16
  Success()
17
17
  when 400
18
18
  Failure('Email address is not a valid email')
@@ -9,9 +9,8 @@ module SdrClient
9
9
  files: [],
10
10
  url:,
11
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
12
+ connection = Connection.new(url: url)
13
+ ModelProcess.new(request_dro: request_dro, connection: connection, files: files, logger: logger).run
15
14
  end
16
15
  end
17
16
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module SdrClient
4
- VERSION = '0.17.0'
4
+ VERSION = '0.20.0'
5
5
  end
@@ -27,11 +27,12 @@ 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.31.0'
30
32
  spec.add_dependency 'dry-monads'
31
33
  spec.add_dependency 'faraday', '>= 0.16'
32
34
 
33
35
  spec.add_development_dependency 'bundler', '~> 2.0'
34
- spec.add_development_dependency 'cocina-models'
35
36
  spec.add_development_dependency 'rake', '~> 13.0'
36
37
  spec.add_development_dependency 'rspec', '~> 3.0'
37
38
  spec.add_development_dependency 'rubocop', '~> 0.79.0'
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.17.0
4
+ version: 0.20.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-10 00:00:00.000000000 Z
11
+ date: 2020-03-24 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.31.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.31.0
13
41
  - !ruby/object:Gem::Dependency
14
42
  name: dry-monads
15
43
  requirement: !ruby/object:Gem::Requirement
@@ -52,20 +80,6 @@ dependencies:
52
80
  - - "~>"
53
81
  - !ruby/object:Gem::Version
54
82
  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'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement
@@ -175,6 +189,7 @@ files:
175
189
  - lib/sdr-client.rb
176
190
  - lib/sdr_client.rb
177
191
  - lib/sdr_client/cli.rb
192
+ - lib/sdr_client/connection.rb
178
193
  - lib/sdr_client/credentials.rb
179
194
  - lib/sdr_client/deposit.rb
180
195
  - lib/sdr_client/deposit/file.rb