0

I am using ActiveResource against a non-Rails REST API... in fact even the "Rest" part is doubtful but they tried:

Although RESTful applications are ideally stateless, the ALM platform requires sessions to manage locking, client life time, and perform other basic tasks. Session management is performed using a cookie named QCSession.

Anyway, I need to issue one GET to "authentication-point/authenticate" to get a user authenticated and take a cookie back. Just not sure how to do that. Here is what I have but I am getting a 404 error:

class AlmActiveResource < ActiveResource::Base
  attr_accessor :lwsso_cookie, :qcsession_cookie

  self.site     = "http://alm_url/qcbin/"
  self.user     = "name"
  self.password = "pw"

  def self.authentication
    @auth_point    = "authentication-point/authenticate"
    self.prefix(@auth_point)
    meow = self.get(:authenticate)
    Rails.logger.debug("Meow: #{meow.inspect}")

  end
end
ScottJShea
  • 7,041
  • 11
  • 44
  • 67

1 Answers1

2

I had the exact same problem. I finally had to put everything in the controller to get it to talk to ALM. It's not the best but it works. Here's the Index action in the ReleaseCycles controller:

 def index
    conn=getAuthenticatedCurl
    conn.url="#{$HPQC_REST_URL}/release-cycles"
    conn.perform
    results=conn.response_code
    hash=Hash.from_xml(conn.body_str)
    respond_to do |format|
        format.html { render :xml => hash }
        format.xml  { render :xml => hash }
        format.json { render :json => hash }
    end
    conn.url=$HPQC_LOGOUT_URL
    conn.perform
    conn.close
    return results
end

I created the get "getAuthenticatedCurl" in the ApplicationController. It looks like:

  $HPQC_HOST = "http://<your_alm_server>:8080"
  $HPQC_REST_URL = "#{$HPQC_HOST}/qcbin/rest/domains/<DOMAIN>/projects/<PROJECT>"
  $HPQC_LOGIN_URL = "#{$HPQC_HOST}/qcbin/authentication-point/authenticate"
  $HPQC_LOGOUT_URL = "#{$HPQC_HOST}/qcbin/authentication-point/logout"

  def getAuthenticatedCurl
    @_conn = Curl::Easy.new($HPQC_LOGIN_URL)
    @_conn.verbose = true
    @_conn.http_auth_types = :basic
    @_conn.userpwd = '<username>:<password>'
    @_conn.enable_cookies = true
    @_conn.cookiejar = 'cookies.txt'
    @_conn.perform #creates the first cookie instance, which allows subsequent calls to the HPQC API
    puts "connected...."
    return @_conn
  end

It's not pretty but it works and it's fast. My next step is to do the same thing as an ActiveResource. Hope it helps and good luck!

braingraze
  • 51
  • 2