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

- old
+ new

@@ -27,10 +27,11 @@ }.freeze # Basic and token authentication header names BASIC_AUTH_HEADER = "Authorization" TOKEN_AUTH_HEADER = "X-Auth-Token" + LOCATION_HEADER = "Location" # Create new connector. # # By default, connector performs no caching. If caching is desired, # Hash should be used as a cache implementation. @@ -216,12 +217,24 @@ "UserName" => @username, "Password" => @password) r = @connection.request(params) raise_invalid_auth_error unless r.status == 201 - token = r.data[:headers][TOKEN_AUTH_HEADER] - add_headers(TOKEN_AUTH_HEADER => token) - @session_oid = JSON.parse(r.data[:body])["@odata.id"] + body = JSON.parse(r.data[:body]) + headers = r.data[:headers] + + add_headers(TOKEN_AUTH_HEADER => headers[TOKEN_AUTH_HEADER]) + save_session_oid!(body, headers) + end + + def save_session_oid!(body, headers) + @session_oid = body["@odata.id"] if body.key?("@odata.id") + return if @session_oid + + return unless headers.key?(LOCATION_HEADER) + + location = URI.parse(headers[LOCATION_HEADER]) + @session_oid = [location.path, location.query].compact.join("?") end def basic_login payload = Base64.encode64("#{@username}:#{@password}").strip add_headers(BASIC_AUTH_HEADER => "Basic #{payload}")