lib/redfish_client/connector.rb in redfish_client-0.5.0 vs lib/redfish_client/connector.rb in redfish_client-0.5.1

- old
+ new

@@ -80,17 +80,15 @@ # @param mathod [Symbol] HTTP method (:get, :post, :patch or :delete) # @param path [String] path to the resource, relative to the base # @param data [Hash] data to be sent over the socket # @return [Response] response object def request(method, path, data = nil) - params = prepare_request_params(method, path, data) - r = @connection.request(params) - if r.status == 401 - login - r = @connection.request(params) + return @cache[path] if method == :get && @cache[path] + + do_request(method, path, data).tap do |r| + @cache[path] = r if method == :get && r.status == 200 end - Response.new(r.status, downcase_headers(r.data[:headers]), r.data[:body]) end # Issue GET request to service. # # This method will first try to return cached response if available. If @@ -98,13 +96,11 @@ # remote and then cached, but only if the response has an OK (200) status. # # @param path [String] path to the resource, relative to the base url # @return [Response] response object def get(path) - return @cache[path] if @cache[path] - - request(:get, path).tap { |r| @cache[path] = r if r.status == 200 } + request(:get, path) end # Issue POST requests to the service. # # @param path [String] path to the resource, relative to the base @@ -185,9 +181,19 @@ end remove_headers([BASIC_AUTH_HEADER, TOKEN_AUTH_HEADER]) end private + + def do_request(method, path, data) + params = prepare_request_params(method, path, data) + r = @connection.request(params) + if r.status == 401 + login + r = @connection.request(params) + end + Response.new(r.status, downcase_headers(r.data[:headers]), r.data[:body]) + end def downcase_headers(headers) headers.each_with_object({}) { |(k, v), obj| obj[k.downcase] = v } end