使用 ActiveResource 使用非 Rails REST API [英] Consume non-Rails REST API with ActiveResource
问题描述
我正在针对非 Rails REST API 使用 ActiveResource……事实上,即使是Rest"部分也值得怀疑,但他们尝试了:
I am using ActiveResource against a non-Rails REST API... in fact even the "Rest" part is doubtful but they tried:
虽然 RESTful 应用程序在理想情况下是无状态的,但 ALM 平台需要会话来管理锁定、客户端生命周期和执行其他基本任务.使用 cookie 执行会话管理名为 QCSession.
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 错误:
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
推荐答案
我遇到了完全相同的问题.我终于不得不把所有东西都放在控制器中,让它与 ALM 对话.这不是最好的,但它有效.这是 ReleaseCycles 控制器中的 Index 操作:
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
我在 ApplicationController 中创建了 getgetAuthenticatedCurl".它看起来像:
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
它并不漂亮,但它有效而且速度很快.我的下一步是做与 ActiveResource 相同的事情.希望能帮到你,祝你好运!
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!
这篇关于使用 ActiveResource 使用非 Rails REST API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!