lib/preservation/client/versioned_api_service.rb in preservation-client-2.0.0 vs lib/preservation/client/versioned_api_service.rb in preservation-client-2.1.0
- old
+ new
@@ -1,11 +1,9 @@
# frozen_string_literal: true
module Preservation
class Client
- DEFAULT_API_VERSION = 'v1'
-
# @abstract API calls to a versioned endpoint
class VersionedApiService
def initialize(connection:, api_version: DEFAULT_API_VERSION)
@connection = connection
@api_version = api_version
@@ -23,23 +21,19 @@
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = 'application/json'
end
return JSON.parse(resp.body).with_indifferent_access if resp.success?
- if resp.status == 404
- errmsg = "#{object_id} not found in Preservation at #{connection.url_prefix}#{req_url}"
- raise Preservation::Client::NotFoundError, errmsg
- else
- errmsg = ResponseErrorFormatter
- .format(response: resp, object_id: object_id, client_method_name: caller_locations.first.label)
- raise Preservation::Client::UnexpectedResponseError, errmsg
- end
- rescue Faraday::ResourceNotFound => e
- errmsg = "HTTP GET to #{connection.url_prefix}#{req_url} failed with #{e.class}: #{e.message}"
+ errmsg = ResponseErrorFormatter
+ .format(response: resp, object_id: object_id, client_method_name: caller_locations.first.label)
+ raise Preservation::Client::UnexpectedResponseError, errmsg
+ rescue Faraday::ResourceNotFound
+ errmsg = "#{object_id} not found in Preservation at #{connection.url_prefix}#{req_url}"
raise Preservation::Client::NotFoundError, errmsg
rescue Faraday::Error => e
- errmsg = "HTTP GET to #{connection.url_prefix}#{req_url} failed with #{e.class}: #{e.message}"
+ errmsg = "Preservation::Client.#{caller_locations.first.label} for #{object_id} " \
+ "got #{e.response[:status]} from Preservation at #{req_url}: #{e.response[:body]}"
raise Preservation::Client::UnexpectedResponseError, errmsg
end
# @param path [String] path to be appended to connection url (no leading slash)
# @param params [Hash] optional params
@@ -55,22 +49,22 @@
# @param method [Symbol] :get or :post
# @param path [String] path to be appended to connection url (no leading slash)
# @param params [Hash] optional params
def http_response(method, path, params)
- req_path = api_version.present? ? "#{api_version}/#{path}" : path
- resp = connection.send(method, req_path, params)
+ req_url = api_version.present? ? "#{api_version}/#{path}" : path
+ resp = connection.send(method, req_url, params)
return resp.body if resp.success?
errmsg = ResponseErrorFormatter.format(response: resp, client_method_name: caller_locations.first.label)
- raise Preservation::Client::NotFoundError, errmsg if resp.status == 404
-
raise Preservation::Client::UnexpectedResponseError, errmsg
rescue Faraday::ResourceNotFound => e
- errmsg = "HTTP #{method.to_s.upcase} to #{connection.url_prefix}#{path} failed with #{e.class}: #{e.message}"
+ errmsg = "Preservation::Client.#{caller_locations.first.label} " \
+ "got #{e.response[:status]} from Preservation at #{req_url}: #{e.response[:body]}"
raise Preservation::Client::NotFoundError, errmsg
rescue Faraday::Error => e
- errmsg = "HTTP #{method.to_s.upcase} to #{connection.url_prefix}#{path} failed with #{e.class}: #{e.message}"
+ errmsg = "Preservation::Client.#{caller_locations.first.label} " \
+ "got #{e.response[:status]} from Preservation at #{req_url}: #{e.response[:body]}"
raise Preservation::Client::UnexpectedResponseError, errmsg
end
end
end
end