Skip to content

Commit

Permalink
Merge 73d1ba0 into 417c2e9
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Sep 10, 2019
2 parents 417c2e9 + 73d1ba0 commit 2de8906
Show file tree
Hide file tree
Showing 12 changed files with 60 additions and 73 deletions.
1 change: 0 additions & 1 deletion Gemfile
Expand Up @@ -28,7 +28,6 @@ gem 'honeybadger'
gem 'jbuilder'
gem 'jwt'
gem 'okcomputer'
gem 'rest-client'
gem 'ruby-cache', '~> 0.3.0'
# Pin net-http-persistent to avoid a problem with exhausting file handles when running under load
gem 'net-http-persistent', '~> 2.9'
Expand Down
1 change: 0 additions & 1 deletion Gemfile.lock
Expand Up @@ -477,7 +477,6 @@ DEPENDENCIES
rack-console
rails (~> 5.2.0)
rails-controller-testing
rest-client
rspec-rails
rubocop (~> 0.74.0)
rubocop-rails
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/application_controller.rb
Expand Up @@ -42,8 +42,8 @@ def http_auth_header
request.headers[TOKEN_HEADER].split(' ').last
end

def proxy_rest_client_response(response)
render status: response.code, content_type: response.headers[:content_type], body: response.body
def proxy_faraday_response(response)
render status: response.status, content_type: response.headers['Content-Type'], body: response.body
end

def load_item
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/objects_controller.rb
Expand Up @@ -75,9 +75,9 @@ def update_marc_record
# This proxies a request to the Goobi server and proxies it's response to the client.
def notify_goobi
response = Dor::Goobi.new(@item).register
proxy_rest_client_response(response)
rescue RestClient::Conflict => e
render status: :conflict, plain: e.http_body
return render status: :conflict, plain: response.body if response.status == 409

proxy_faraday_response(response)
end

# You can post a release tag as JSON in the body to add a release tag to an item.
Expand Down
37 changes: 18 additions & 19 deletions app/controllers/sdr_controller.rb
Expand Up @@ -13,40 +13,39 @@ def cm_inv_diff
query_params = { subset: params[:subset].to_s }
query_params[:version] = params[:version].to_s unless params[:version].nil?
query_string = URI.encode_www_form(query_params)
sdr_query = "objects/#{params[:druid]}/cm-inv-diff?#{query_string}"
url = "#{Settings.sdr_url}/objects/#{params[:druid]}/cm-inv-diff?#{query_string}"
sdr_response = Faraday.post(url, current_content, 'Content-Type' => 'application/xml')

sdr_response = sdr_client[sdr_query].post(current_content, content_type: 'application/xml') { |response, _request, _result| response }
proxy_rest_client_response(sdr_response)
proxy_faraday_response(sdr_response)
end

def ds_manifest
url = "objects/#{params[:druid]}/manifest/#{params[:dsname]}"
sdr_response = sdr_client[url].get { |response, _request, _result| response }
proxy_rest_client_response(sdr_response)
url = "#{Settings.sdr_url}/objects/#{params[:druid]}/manifest/#{params[:dsname]}"
sdr_response = Faraday.get url

proxy_faraday_response(sdr_response)
end

def ds_metadata
url = "objects/#{params[:druid]}/metadata/#{params[:dsname]}"
sdr_response = sdr_client[url].get { |response, _request, _result| response }
proxy_rest_client_response(sdr_response)
url = "#{Settings.sdr_url}/objects/#{params[:druid]}/metadata/#{params[:dsname]}"
sdr_response = Faraday.get url

proxy_faraday_response(sdr_response)
end

def current_version
sdr_response = sdr_client["objects/#{params[:druid]}/current_version"].get { |response, _request, _result| response }
proxy_rest_client_response(sdr_response)
url = "#{Settings.sdr_url}/objects/#{params[:druid]}/current_version"
sdr_response = Faraday.get url

proxy_faraday_response(sdr_response)
end

def file_content
query_string = URI.encode_www_form(version: params[:version].to_s)
encoded_filename = URI.encode(params[:filename])
url = "objects/#{params[:druid]}/content/#{encoded_filename}?#{query_string}"
sdr_response = sdr_client[url].get { |response, _request, _result| response }
proxy_rest_client_response(sdr_response)
end

private
url = "#{Settings.sdr_url}/objects/#{params[:druid]}/content/#{encoded_filename}?#{query_string}"
sdr_response = Faraday.get url

def sdr_client
SdrClient.create
proxy_faraday_response(sdr_response)
end
end
20 changes: 14 additions & 6 deletions app/models/dor/goobi.rb
Expand Up @@ -3,18 +3,26 @@
module Dor
# This class passes data to the Goobi server using a custom XML message that was developed by Intranda
class Goobi < ServiceItem
# Any RestClient exception that is a 500 or greater
RETRIABLE_EXCEPTIONS = RestClient::Exceptions::EXCEPTIONS_MAP.select { |k, _v| k >= 500 }.values +
[RestClient::RequestTimeout,
RestClient::ServerBrokeConnection,
RestClient::SSLCertificateNotVerified]
SERVER_ERROR_STATUSES = (500...600).freeze
class ServerError < StandardError; end

# Any status that is a 500 or greater and timeouts
RETRIABLE_EXCEPTIONS = [ServerError,
Errno::ETIMEDOUT,
'Timeout::Error',
Faraday::TimeoutError,
Faraday::RetriableResponse].freeze

def register
with_retries(max_tries: Settings.goobi.max_tries,
base_sleep_seconds: Settings.goobi.base_sleep_seconds,
max_sleep_seconds: Settings.goobi.max_sleep_seconds,
rescue: RETRIABLE_EXCEPTIONS) do |_attempt|
RestClient.post(Settings.goobi.url, xml_request, content_type: 'application/xml')
response = Faraday.post(Settings.goobi.url, xml_request, 'Content-Type' => 'application/xml')
# When we upgrade to Faraday 1.0, we can rely on Faraday::ServerError
raise ServerError, status: response.status, body: response.body if SERVER_ERROR_STATUSES.include?(response.status)

response
end
end

Expand Down
14 changes: 6 additions & 8 deletions app/services/publish_metadata_service.rb
Expand Up @@ -60,23 +60,21 @@ def purl_druid
# When publishing a PURL, we notify purl-fetcher of changes.
#
def publish_notify_on_success
id = item.pid.gsub(/^druid:/, '')

rest_client["purls/#{id}"].post ''
Faraday.post(purl_services_url)
end

##
# When deleting a PURL, we notify purl-fetcher of changes.
#
def publish_delete_on_success
id = item.pid.gsub(/^druid:/, '')

rest_client["purls/#{id}"].delete
Faraday.delete(purl_services_url)
end

def rest_client
def purl_services_url
id = item.pid.gsub(/^druid:/, '')

raise 'You have not configured perl-fetcher (Settings.purl_services_url).' unless Settings.purl_services_url

RestClient::Resource.new(Settings.purl_services_url)
"#{Settings.purl_services_url}/purls/#{id}"
end
end
17 changes: 9 additions & 8 deletions app/services/sdr_client.rb
Expand Up @@ -2,22 +2,23 @@

# A client for talking to sdr-services-app
class SdrClient
def self.create
RestClient::Resource.new(Settings.sdr_url, {})
end

# @raises [RestClient::NotFound] if SDR doesn't know about the object (i.e. 404 response code)
# @raises [Dor::Exception] if SDR doesn't know about the object (i.e. 404 response code)
# @raises [StandardError] if the response from SDR can't be parsed
def self.current_version(druid)
xml = create["objects/#{druid}/current_version"].get
response = Faraday.get "#{Settings.sdr_url}/objects/#{druid}/current_version"

if response.status == 404
raise Dor::Exception, 'SDR is not yet answering queries about this object. ' \
"We've seen that when an object has been transfered, SDR isn't immediately ready to answer queries"
end

begin
doc = Nokogiri::XML xml
doc = Nokogiri::XML response.body
raise if doc.root.name != 'currentVersion'

return Integer(doc.text)
rescue StandardError
raise "Unable to parse XML from SDR current_version API call: #{xml}"
raise "Unable to parse XML from SDR current_version API call: #{response.body}"
end
end
end
7 changes: 1 addition & 6 deletions app/services/version_service.rb
Expand Up @@ -99,12 +99,7 @@ def try_to_get_current_version(assume_accessioned = false)
# The submitted milestone is part of the accessionWF.
raise Dor::Exception, 'Object currently being accessioned' if accessioning?

begin
return SdrClient.current_version work.pid
rescue RestClient::NotFound
raise Dor::Exception, 'SDR is not yet answering queries about this object. ' \
"We've seen that when an object has been transfered, SDR isn't immediately ready to answer queries"
end
SdrClient.current_version work.pid
end

# Checks if current version has any incomplete wf steps and there is a versionWF
Expand Down
8 changes: 4 additions & 4 deletions spec/dor/goobi_spec.rb
Expand Up @@ -141,20 +141,20 @@
end

it 'makes a call to the goobi server with the appropriate xml params' do
expect(response.code).to eq 201
expect(response.status).to eq 201
end
end

context 'with a 409 response' do
before do
allow(RestClient).to receive(:post).and_call_original
allow(Faraday).to receive(:post).and_call_original
stub_request(:post, Settings.goobi.url)
.to_return(body: '<somexml/>', headers: { 'Content-Type' => 'text/xml' }, status: 409)
end

it 'makes a call to the goobi server with the appropriate xml params' do
expect { response }.to raise_error(RestClient::Conflict)
expect(RestClient).to have_received(:post).once # Don't retry request errors
expect(response.status).to eq 409
expect(Faraday).to have_received(:post).once # Don't retry request errors
end
end
end
Expand Down
14 changes: 1 addition & 13 deletions spec/services/sdr_client_spec.rb
Expand Up @@ -37,19 +37,7 @@
end

it 'raises an error' do
expect { current_version }.to raise_error RestClient::NotFound
end
end
end

describe '.create' do
context 'with SDR configuration' do
before do
allow(Settings).to receive(:sdr_url).and_return('http://example.com')
end

it 'is configured to use SDR' do
expect(described_class.create.url).to eq 'http://example.com'
expect { current_version }.to raise_error Dor::Exception
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions spec/services/version_service_spec.rb
Expand Up @@ -96,7 +96,7 @@
context "when sdr-services-app doesn't know about the object" do
before do
allow(Dor::Config.workflow).to receive(:client).and_return(workflow_client)
allow(SdrClient).to receive(:current_version).and_raise(RestClient::NotFound)
allow(SdrClient).to receive(:current_version).and_raise(Dor::Exception, 'SDR is not yet answering queries about this object')
end

let(:workflow_client) do
Expand Down Expand Up @@ -172,7 +172,7 @@

context "when sdr-services-app doesn't know about the object" do
before do
allow(SdrClient).to receive(:current_version).and_raise(RestClient::NotFound)
allow(SdrClient).to receive(:current_version).and_raise(Dor::Exception)
end

it 'returns false' do
Expand Down

0 comments on commit 2de8906

Please sign in to comment.