Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion archivesspace-client.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ Gem::Specification.new do |spec|
spec.add_development_dependency "webmock", "~> 3.24"

spec.add_dependency "dry-cli", "< 2.0"
spec.add_dependency "httparty", "~> 0.14"
spec.add_dependency "httparty", "~> 0.16"
spec.add_dependency "json", "~> 2.0"
spec.add_dependency "jbuilder", "~> 2.12"
end
53 changes: 53 additions & 0 deletions examples/upload_ead.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# frozen_string_literal: true

$LOAD_PATH.unshift File.expand_path("../lib", __dir__)
require "archivesspace/client"

# usage: ruby examples/upload_ead.rb path/to/ead.xml
path = ARGV.fetch(0) { abort "usage: ruby #{$PROGRAM_NAME} path/to/ead.xml" }

# official sandbox, config boilerplate
config = ArchivesSpace::Configuration.new(
{
base_uri: "https://test.archivesspace.org/staff/api",
username: "admin",
password: "admin",
page_size: 50,
throttle: 0,
verify_ssl: false
}
)

client = ArchivesSpace::Client.new(config).login

job = {
jsonmodel_type: "job",
job: {
jsonmodel_type: "import_job",
import_type: "ead_xml",
filenames: [File.basename(path)]
}
}

client.repository 2 do
response = File.open(path) do |file|
client.post_multipart("jobs_with_files", {job: job.to_json, files: [file]})
end
abort "upload failed (#{response.status_code}): #{response.body}" unless
response.status_code == 200

job_id = response.parsed["id"]
puts "created import job #{job_id}"

# poll until the import finishes, but not for more than ~60s
status = nil
30.times do
status = client.get("jobs/#{job_id}").parsed["status"]
puts "status: #{status}"
break if %w[completed failed canceled].include?(status)

sleep 2
end

puts client.get("jobs/#{job_id}/log").body
end
8 changes: 7 additions & 1 deletion lib/archivesspace/client/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@ def post(path, payload, params = {})
request "POST", path, {body: payload.to_json, query: params}
end

# Allow passing a file object (multipart = true)
def post_multipart(path, payload, params = {})
body = payload.transform_values { |v| v.is_a?(Hash) ? v.to_json : v }
request "POST", path, {body: body, query: params, multipart: true}
end

def put(path, payload, params = {})
request "PUT", path, {body: payload.to_json, query: params}
end
Expand Down Expand Up @@ -76,7 +82,7 @@ def use_global_repository
def request(method, path, options = {})
sleep config.throttle
options = options.dup
options[:headers] = {TOKEN => token} if token
options[:headers] = options.fetch(:headers, {}).merge(TOKEN => token) if token
Request.new(context, config, method, path, options).execute
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/archivesspace/client/request.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ def initialize(context, config, method = "GET", path = "", options = {})
@options = options.dup

@options[:headers] = DEFAULT_HEADERS.merge(@options.fetch(:headers, {}))
@options[:headers].delete("Content-Type") if @options[:multipart]
@options[:headers]["User-Agent"] = "#{Client::NAME}/#{Client::VERSION}"

@options[:verify] = config.verify_ssl
Expand Down
18 changes: 18 additions & 0 deletions spec/archivesspace/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,24 @@
client.get("resources", opts)
expect(opts).to eq(caller_opts)
end

it "posts a file (as multipart form data) with session token" do
client.token = "abc123"
stub = stub_request(:post, "#{DEFAULT_BASE_URI}/repositories/2/jobs_with_files")
.with { |req|
req.headers["Content-Type"].start_with?("multipart/form-data; boundary=") &&
req.headers["X-Archivesspace-Session"] == "abc123" &&
req.body.include?('name="files[]"; filename="client_spec.rb"') &&
req.body.include?('name="job"')
}
.to_return(status: 200, body: "{}", headers: {"Content-Type" => "application/json"})
File.open(__FILE__) do |file|
client.repository(2) do
client.post_multipart("jobs_with_files", {job: {jsonmodel_type: "job"}, files: [file]})
end
end
expect(stub).to have_been_requested
end
end

describe "URL construction" do
Expand Down
Loading