lib/preservation/client/versioned_api_service.rb in preservation-client-3.1.0 vs lib/preservation/client/versioned_api_service.rb in preservation-client-3.2.0
- old
+ new
@@ -13,11 +13,11 @@
attr_reader :connection, :api_version
# @param path [String] path to be appended to connection url (no leading slash)
def get_json(path, object_id)
- req_url = api_version.present? ? "#{api_version}/#{path}" : path
+ req_url = "#{api_version}/#{path}"
resp = connection.get do |req|
req.url req_url
req.headers['Content-Type'] = 'application/json'
req.headers['Accept'] = 'application/json'
end
@@ -34,32 +34,55 @@
"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
- def get(path, params)
- http_response(:get, path, params)
+ # @param params [Hash] optional request parameters
+ # @param on_data [Proc] a callback to use when a streaming response is desired.
+ def get(path, params, on_data:)
+ return http_response(:get, path, params) unless on_data
+
+ connection.get("#{api_version}/#{path}", params) do |req|
+ req.options.on_data = on_data
+ end
end
# @param path [String] path to be appended to connection url (no leading slash)
- # @param params [Hash] optional params
+ # @param params [Hash] optional request parameters
def post(path, params)
http_response(:post, path, params)
end
- # @param method [Symbol] :get or :post
# @param path [String] path to be appended to connection url (no leading slash)
- # @param params [Hash] optional params
+ # @param params [Hash] optional request parameters
+ def patch(path, params)
+ http_response(:patch, path, params)
+ end
+
+ # @param path [String] path to be appended to connection url (no leading slash)
+ # @param params [Hash] optional request parameters
+ def put(path, params)
+ http_response(:put, path, params)
+ end
+
+ # @param path [String] path to be appended to connection url (no leading slash)
+ # @param params [Hash] optional request parameters
+ def delete(path, params)
+ http_response(:delete, path, params)
+ end
+
+ # @param method [Symbol] a symbol representing an HTTP method: :get, :post, :patch, :put, :delete
+ # @param path [String] path to be appended to connection url (no leading slash)
+ # @param params [Hash] optional request parameters
def http_response(method, path, params)
- req_url = api_version.present? ? "#{api_version}/#{path}" : path
+ req_url = "#{api_version}/#{path}"
resp =
case method
- when :get
- connection.get(req_url, params)
- when :post
+ when :delete, :get
+ connection.public_send(method, req_url, params)
+ when :patch, :post, :put
request_json = params.to_json if params&.any?
- connection.post(req_url, request_json, 'Content-Type' => 'application/json')
+ connection.public_send(method, req_url, request_json, 'Content-Type' => 'application/json')
end
return resp.body if resp.success?
errmsg = ResponseErrorFormatter.format(response: resp, client_method_name: caller_locations.first.label)
raise Preservation::Client::UnexpectedResponseError, errmsg