sdr-client 0.25.0 → 0.28.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/pull_request_template.md +8 -1
- data/bin/console +1 -1
- data/exe/sdr +18 -30
- data/lib/sdr_client.rb +1 -1
- data/lib/sdr_client/background_job_results.rb +21 -0
- data/lib/sdr_client/cli.rb +69 -5
- data/lib/sdr_client/credentials.rb +9 -1
- data/lib/sdr_client/deposit.rb +15 -0
- data/lib/sdr_client/deposit/file_metadata_builder_operations/mime_type.rb +3 -2
- data/lib/sdr_client/deposit/process.rb +1 -0
- data/lib/sdr_client/deposit/upload_resource.rb +2 -2
- data/lib/sdr_client/version.rb +1 -1
- data/sdr-client.gemspec +1 -1
- metadata +5 -5
- data/lib/sdr_client/model_deposit.rb +0 -22
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 50a6b81aebe70d387a5be6aebb0851f82a14e67dacefe260af79c374a4a95a77
|
4
|
+
data.tar.gz: 4b21258044406e8cc46783b7554620bf5c7cd986600d0f3ecfddb1e88cdaf564
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 64e6a60ee1e652b20ba4482f66a98b87ccdf7651933b99dc6abfa1d067f71d5e07a3d099fab72ecf33518a4ba6d33521fa2b340845a5e88b8139a6799648b364
|
7
|
+
data.tar.gz: 3f1c03720980a469de93ecfb60aa88b24652f647eb6929b0cda15d9702388b9a6ae19e93ea2e73439705bf1f96891378121725f48f909b6d6c29898796417eb8
|
data/bin/console
CHANGED
data/exe/sdr
CHANGED
@@ -6,37 +6,13 @@ require 'optparse'
|
|
6
6
|
require 'sdr_client'
|
7
7
|
|
8
8
|
options = {}
|
9
|
+
|
9
10
|
global = OptionParser.new do |opts|
|
10
11
|
opts.on('--service-url URL', 'Connect to the host at this URL') do |url|
|
11
12
|
options[:url] = url
|
12
13
|
end
|
13
14
|
opts.on('-h', '--help', 'Display this screen') do
|
14
|
-
|
15
|
-
DESCRIPTION:
|
16
|
-
The SDR Command Line Interface is a tool to interact with the Stanford Digital Repository.
|
17
|
-
|
18
|
-
SYNOPSIS:
|
19
|
-
sdr [options] <command>
|
20
|
-
|
21
|
-
OPTIONS:
|
22
|
-
--service-url (string)
|
23
|
-
Override the command's default URL with the given URL.
|
24
|
-
|
25
|
-
-h, --help
|
26
|
-
Displays this screen
|
27
|
-
|
28
|
-
COMMANDS
|
29
|
-
deposit
|
30
|
-
accession object into the SDR
|
31
|
-
|
32
|
-
register
|
33
|
-
create a draft object in SDR
|
34
|
-
|
35
|
-
login
|
36
|
-
Will prompt for email & password and exchange it for an login token, which it saves in ~/.sdr/token
|
37
|
-
|
38
|
-
HELP
|
39
|
-
exit
|
15
|
+
SdrClient::CLI.help
|
40
16
|
end
|
41
17
|
end
|
42
18
|
|
@@ -44,6 +20,7 @@ global.order!
|
|
44
20
|
command = ARGV.shift
|
45
21
|
|
46
22
|
deposit_options = OptionParser.new do |opts|
|
23
|
+
opts.banner = "Usage: sdr #{command} [options]"
|
47
24
|
opts.on('--label LABEL', 'The object label') do |label|
|
48
25
|
options[:label] = label
|
49
26
|
end
|
@@ -54,8 +31,8 @@ deposit_options = OptionParser.new do |opts|
|
|
54
31
|
|
55
32
|
opts.on('--type TYPE', 'The object type to create. ' \
|
56
33
|
'One of: "image", "book", "document", "map", "manuscript", "media", ' \
|
57
|
-
'"three_dimensional", "collection", or "admin_policy"') do |type|
|
58
|
-
if %w[image book document map manuscript media three_dimensional collection admin_policy].include?(type)
|
34
|
+
'"three_dimensional", "object", "collection", or "admin_policy"') do |type|
|
35
|
+
if %w[image book document map manuscript media three_dimensional object collection admin_policy].include?(type)
|
59
36
|
options[:type] = "http://cocina.sul.stanford.edu/models/#{type}.jsonld"
|
60
37
|
end
|
61
38
|
end
|
@@ -114,6 +91,8 @@ deposit_options = OptionParser.new do |opts|
|
|
114
91
|
end
|
115
92
|
end
|
116
93
|
|
94
|
+
SdrClient::CLI.help unless command
|
95
|
+
|
117
96
|
subcommands = {
|
118
97
|
'deposit' => deposit_options,
|
119
98
|
'register' => deposit_options,
|
@@ -122,10 +101,19 @@ subcommands = {
|
|
122
101
|
|
123
102
|
unless subcommands.key?(command)
|
124
103
|
puts "unknown command '#{command}'"
|
125
|
-
|
104
|
+
SdrClient::CLI.help
|
126
105
|
end
|
127
106
|
|
128
107
|
subcommands[command].order!
|
129
108
|
|
130
109
|
options[:files] = ARGV unless ARGV.empty?
|
131
|
-
|
110
|
+
options[:url] ||= 'https://sdr-api-prod.stanford.edu'
|
111
|
+
|
112
|
+
begin
|
113
|
+
SdrClient::CLI.start(command, options)
|
114
|
+
rescue StandardError => e
|
115
|
+
warn "There was a problem making your request:\n\n"
|
116
|
+
warn e.message
|
117
|
+
puts
|
118
|
+
puts subcommands[command].help
|
119
|
+
end
|
data/lib/sdr_client.rb
CHANGED
@@ -9,12 +9,12 @@ require 'cocina/models'
|
|
9
9
|
|
10
10
|
require 'sdr_client/version'
|
11
11
|
require 'sdr_client/deposit'
|
12
|
-
require 'sdr_client/model_deposit'
|
13
12
|
require 'sdr_client/credentials'
|
14
13
|
require 'sdr_client/login'
|
15
14
|
require 'sdr_client/login_prompt'
|
16
15
|
require 'sdr_client/cli'
|
17
16
|
require 'sdr_client/connection'
|
17
|
+
require 'sdr_client/background_job_results'
|
18
18
|
|
19
19
|
module SdrClient
|
20
20
|
class Error < StandardError; end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'json'
|
4
|
+
|
5
|
+
module SdrClient
|
6
|
+
# API calls around background job results from dor-services-app
|
7
|
+
module BackgroundJobResults
|
8
|
+
# Get status/result of a background job
|
9
|
+
# @param url [String] url for the service
|
10
|
+
# @param job_id [String] required string representing a job identifier
|
11
|
+
# @return [Hash] result of background job
|
12
|
+
def self.show(url:, job_id:)
|
13
|
+
connection = Connection.new(url: "#{url}/v1/background_job_results/#{job_id}").connection
|
14
|
+
resp = connection.get
|
15
|
+
|
16
|
+
raise "unexpected response: #{resp.status} #{resp.body}" unless resp.success?
|
17
|
+
|
18
|
+
JSON.parse(resp.body).with_indifferent_access
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/sdr_client/cli.rb
CHANGED
@@ -3,15 +3,46 @@
|
|
3
3
|
module SdrClient
|
4
4
|
# The command line interface
|
5
5
|
module CLI
|
6
|
+
HELP = <<~HELP
|
7
|
+
DESCRIPTION:
|
8
|
+
The SDR Command Line Interface is a tool to interact with the Stanford Digital Repository.
|
9
|
+
|
10
|
+
SYNOPSIS:
|
11
|
+
sdr [options] <command>
|
12
|
+
|
13
|
+
To see help text for each command you can run:
|
14
|
+
|
15
|
+
sdr [options] <command> help
|
16
|
+
|
17
|
+
OPTIONS:
|
18
|
+
--service-url (string)
|
19
|
+
Override the command's default URL with the given URL.
|
20
|
+
|
21
|
+
-h, --help
|
22
|
+
Displays this screen
|
23
|
+
|
24
|
+
|
25
|
+
COMMANDS:
|
26
|
+
deposit
|
27
|
+
Accession an object into the SDR
|
28
|
+
|
29
|
+
register
|
30
|
+
Create a draft object in SDR and retrieve a Druid identifier.
|
31
|
+
|
32
|
+
login
|
33
|
+
Will prompt for email & password and exchange it for an login token, which it saves in ~/.sdr/token
|
34
|
+
|
35
|
+
HELP
|
36
|
+
|
6
37
|
def self.start(command, options)
|
7
38
|
case command
|
8
|
-
when 'deposit'
|
9
|
-
|
10
|
-
|
11
|
-
|
39
|
+
when 'deposit', 'register'
|
40
|
+
display_errors(validate_deposit_options(options))
|
41
|
+
job_id = SdrClient::Deposit.run(accession: command == 'deposit', **options)
|
42
|
+
poll_for_job_complete(job_id: job_id, url: options[:url]) # TODO: add an option that skips this
|
12
43
|
when 'login'
|
13
44
|
status = SdrClient::Login.run(options)
|
14
|
-
puts status.
|
45
|
+
puts status.failure if status.failure?
|
15
46
|
else
|
16
47
|
raise "Unknown command #{command}"
|
17
48
|
end
|
@@ -19,5 +50,38 @@ module SdrClient
|
|
19
50
|
puts 'Log in first'
|
20
51
|
exit(1)
|
21
52
|
end
|
53
|
+
|
54
|
+
def self.display_errors(errors)
|
55
|
+
return if errors.empty?
|
56
|
+
|
57
|
+
raise errors.map { |k, v| "#{k} #{v}" }.join("\n")
|
58
|
+
end
|
59
|
+
|
60
|
+
def self.validate_deposit_options(options)
|
61
|
+
{}.tap do |errors|
|
62
|
+
errors['admin-policy'] = 'is a required argument' unless options[:apo]
|
63
|
+
errors['source-id'] = 'is a required argument' unless options[:source_id]
|
64
|
+
end
|
65
|
+
end
|
66
|
+
|
67
|
+
def self.help
|
68
|
+
puts HELP
|
69
|
+
exit
|
70
|
+
end
|
71
|
+
|
72
|
+
def self.poll_for_job_complete(job_id:, url:)
|
73
|
+
result = nil
|
74
|
+
1.upto(5) do |_n|
|
75
|
+
result = SdrClient::BackgroundJobResults.show(url: url, job_id: job_id)
|
76
|
+
break unless %w[pending processing].include? result['status']
|
77
|
+
|
78
|
+
sleep 5
|
79
|
+
end
|
80
|
+
if result['status'] == 'complete'
|
81
|
+
puts result.dig('output', 'druid')
|
82
|
+
else
|
83
|
+
warn "Job #{job_id} did not complete\n#{result.inspect}"
|
84
|
+
end
|
85
|
+
end
|
22
86
|
end
|
23
87
|
end
|
@@ -10,6 +10,7 @@ module SdrClient
|
|
10
10
|
json = JSON.parse(body)
|
11
11
|
Dir.mkdir(credentials_path, 0o700) unless Dir.exist?(credentials_path)
|
12
12
|
File.open(credentials_file, 'w', 0o600) do |file|
|
13
|
+
file.flock(File::LOCK_EX)
|
13
14
|
file.write(json.fetch('token'))
|
14
15
|
end
|
15
16
|
end
|
@@ -17,7 +18,14 @@ module SdrClient
|
|
17
18
|
def self.read
|
18
19
|
raise NoCredentialsError unless ::File.exist?(credentials_file)
|
19
20
|
|
20
|
-
|
21
|
+
creds = nil
|
22
|
+
File.open(credentials_file, 'r') do |file|
|
23
|
+
file.flock(File::LOCK_SH)
|
24
|
+
creds = file.readlines(chomp: true).first
|
25
|
+
end
|
26
|
+
raise NoCredentialsError if creds.nil?
|
27
|
+
|
28
|
+
creds
|
21
29
|
end
|
22
30
|
|
23
31
|
def self.credentials_path
|
data/lib/sdr_client/deposit.rb
CHANGED
@@ -8,6 +8,7 @@ module SdrClient
|
|
8
8
|
BOOK_TYPE = 'http://cocina.sul.stanford.edu/models/book.jsonld'
|
9
9
|
# rubocop:disable Metrics/ParameterLists
|
10
10
|
# rubocop:disable Metrics/MethodLength
|
11
|
+
# @return [String] job id for the background job result
|
11
12
|
def self.run(label: nil,
|
12
13
|
type: BOOK_TYPE,
|
13
14
|
viewing_direction: nil,
|
@@ -50,6 +51,19 @@ module SdrClient
|
|
50
51
|
end
|
51
52
|
# rubocop:enable Metrics/MethodLength
|
52
53
|
# rubocop:enable Metrics/ParameterLists
|
54
|
+
|
55
|
+
def self.model_run(request_dro:,
|
56
|
+
files: [],
|
57
|
+
url:,
|
58
|
+
accession:,
|
59
|
+
logger: Logger.new(STDOUT))
|
60
|
+
connection = Connection.new(url: url)
|
61
|
+
ModelProcess.new(request_dro: request_dro,
|
62
|
+
connection: connection,
|
63
|
+
files: files,
|
64
|
+
logger: logger,
|
65
|
+
accession: accession).run
|
66
|
+
end
|
53
67
|
end
|
54
68
|
end
|
55
69
|
require 'json'
|
@@ -62,6 +76,7 @@ require 'sdr_client/deposit/file_metadata_builder'
|
|
62
76
|
require 'sdr_client/deposit/file_set'
|
63
77
|
require 'sdr_client/deposit/request'
|
64
78
|
require 'sdr_client/deposit/metadata_builder'
|
79
|
+
require 'sdr_client/deposit/model_process'
|
65
80
|
require 'sdr_client/deposit/process'
|
66
81
|
require 'sdr_client/deposit/upload_files'
|
67
82
|
require 'sdr_client/deposit/upload_resource'
|
@@ -1,6 +1,6 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
|
-
require '
|
3
|
+
require 'shellwords'
|
4
4
|
|
5
5
|
module SdrClient
|
6
6
|
module Deposit
|
@@ -9,7 +9,8 @@ module SdrClient
|
|
9
9
|
class MimeType
|
10
10
|
NAME = 'mime_type'
|
11
11
|
def self.for(file_path:, **)
|
12
|
-
|
12
|
+
argv = Shellwords.escape(file_path)
|
13
|
+
`file --mime-type -b #{argv}`.chomp
|
13
14
|
end
|
14
15
|
end
|
15
16
|
end
|
@@ -21,14 +21,14 @@ module SdrClient
|
|
21
21
|
end
|
22
22
|
|
23
23
|
# @param [Hash<Symbol,String>] the result of the metadata call
|
24
|
-
# @return [
|
24
|
+
# @return [String] job id for the background job result
|
25
25
|
def run
|
26
26
|
response = metadata_request
|
27
27
|
unexpected_response(response) unless response.status == 201
|
28
28
|
|
29
29
|
logger.info("Response from server: #{response.body}")
|
30
30
|
|
31
|
-
|
31
|
+
JSON.parse(response.body)['jobId']
|
32
32
|
end
|
33
33
|
|
34
34
|
private
|
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.33.0'
|
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.28.1
|
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-06-23 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.33.0
|
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.33.0
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: dry-monads
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
@@ -188,6 +188,7 @@ files:
|
|
188
188
|
- exe/sdr
|
189
189
|
- lib/sdr-client.rb
|
190
190
|
- lib/sdr_client.rb
|
191
|
+
- lib/sdr_client/background_job_results.rb
|
191
192
|
- lib/sdr_client/cli.rb
|
192
193
|
- lib/sdr_client/connection.rb
|
193
194
|
- lib/sdr_client/credentials.rb
|
@@ -210,7 +211,6 @@ files:
|
|
210
211
|
- lib/sdr_client/deposit/upload_resource.rb
|
211
212
|
- lib/sdr_client/login.rb
|
212
213
|
- lib/sdr_client/login_prompt.rb
|
213
|
-
- lib/sdr_client/model_deposit.rb
|
214
214
|
- lib/sdr_client/version.rb
|
215
215
|
- sdr-client.gemspec
|
216
216
|
homepage: https://github.com/sul-dlss/sdr-client
|
@@ -1,22 +0,0 @@
|
|
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
|
-
accession:,
|
12
|
-
logger: Logger.new(STDOUT))
|
13
|
-
connection = Connection.new(url: url)
|
14
|
-
ModelProcess.new(request_dro: request_dro,
|
15
|
-
connection: connection,
|
16
|
-
files: files,
|
17
|
-
logger: logger,
|
18
|
-
accession: accession).run
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
require 'sdr_client/deposit/model_process'
|