lib/redfish_client/connector.rb in redfish_client-0.2.1 vs lib/redfish_client/connector.rb in redfish_client-0.2.2

- old
+ new

@@ -23,74 +23,78 @@ # # @param url [String] base url of the Redfish service # @param verify [Boolean] verify SSL certificate of the service def initialize(url, verify = true) @url = url - @verify = verify @headers = DEFAULT_HEADERS.dup - @connection = create_connection + middlewares = Excon.defaults[:middlewares] + + [Excon::Middleware::RedirectFollower] + @connection = Excon.new(@url, + ssl_verify_peer: verify, + middlewares: middlewares) end # Add HTTP headers to the requests made by the connector. # # @param headers [Hash<String, String>] headers to be added def add_headers(headers) @headers.merge!(headers) - @connection = create_connection end # Remove HTTP headers from requests made by the connector. # # Headers that are not currently set are silently ignored and no error is # raised. # # @param headers [List<String>] headers to remove def remove_headers(headers) headers.each { |h| @headers.delete(h) } - @connection = create_connection end # Issue GET request to service. # # @param path [String] path to the resource, relative to the base url # @return [Excon::Response] response object def get(path) - @connection.get(path: path) + @connection.get(path: path, headers: @headers) end # Issue POST requests to the service. # # @param path [String] path to the resource, relative to the base - # @param body [String] data to be sent over the socket + # @param data [Hash] data to be sent over the socket, JSON encoded # @return [Excon::Response] response object - def post(path, body = nil) - params = { path: path } - params[:body] = body if body - @connection.post(params) + def post(path, data = nil) + @connection.post(prepare_request_params(path, data)) end # Issue PATCH requests to the service. # # @param path [String] path to the resource, relative to the base - # @param body [String] data to be sent over the socket + # @param data [Hash] data to be sent over the socket # @return [Excon::Response] response object - def patch(path, body = nil) - params = { path: path } - params[:body] = body if body - @connection.patch(params) + def patch(path, data = nil) + @connection.patch(prepare_request_params(path, data)) end # Issue DELETE requests to the service. # # @param path [String] path to the resource, relative to the base # @return [Excon::Response] response object def delete(path) - @connection.delete(path: path) + @connection.delete(path: path, headers: @headers) end private - def create_connection - Excon.new(@url, headers: @headers, ssl_verify_peer: @verify) + def prepare_request_params(path, data) + params = { path: path } + if data + params[:body] = data.to_json + params[:headers] = @headers.merge("Content-Type" => "application/json") + else + params[:headers] = @headers + end + params end end end