【问题标题】:Consume non-Rails REST API with ActiveResource使用 ActiveResource 使用非 Rails REST API
【发布时间】:2012-02-24 07:43:59
【问题描述】:

我正在针对非 Rails REST API 使用 ActiveResource...事实上,即使是“Rest”部分也值得怀疑,但他们尝试了:

虽然 RESTful 应用程序在理想情况下是无状态的,但 ALM 平台 需要会话来管理锁定、客户端生命周期和执行 其他基本任务。使用 cookie 执行会话管理 命名为 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

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3


    【解决方案1】:

    我遇到了完全相同的问题。我最后不得不把所有东西都放在控制器中,让它与 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 相同的事情。希望对您有所帮助,祝您好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多