2012-02-10 27 views
0

我使用的ActiveResource對非Rails的REST API ......其實即便是「休息」的一部分是值得懷疑的,但他們的嘗試:消費非Rails的REST API使用的ActiveResource

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.

無論如何,我需要發出一個GET到「身份驗證點/身份驗證」以獲得用戶身份驗證並取回cookie。只是不知道該怎麼做。這是我有,但我得到一個404錯誤:

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 

回答

2

我有完全相同的問題。我終於必須把所有東西都放在控制器中,才能與ALM交談。這不是最好的,但它的工作原理。下面是在ReleaseCycles控制器Index操作:

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 

我創建的ApplicationController中的get 「getAuthenticatedCurl」。它看起來像:

$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 

它不漂亮,但它的工作原理,它的速度很快。我的下一步是做一個ActiveResource相同的事情。希望它有幫助,祝你好運!

+0

瘋狂道具!謝謝。 – ScottJShea 2012-02-24 00:10:47