代码之家  ›  专栏  ›  技术社区  ›  ADMAT Bandara

在Rails中等待HTTP响应的最佳实践是什么?

  •  0
  • ADMAT Bandara  · 技术社区  · 8 年前

    我正在使用HTTPARTY gem处理HTTP请求和响应。 我的客户正在使用一个名为Freja eID的身份验证代理,在向他们发送请求后,我已经为他们的响应持续了2分钟。

    这就是我所做的

    # 2) get Auth Request
      1.upto(24) do |n|
        response = user.getOneAuthResultReuqest(authRef)
        response_status = response['status']
        if response_status == "APPROVED"
          @response_status = response_status
          break
        elsif response_status == "CANCELED"
          @response_status = response_status
          break
        elsif response_status == "EXPIRED"
          @response_status = response_status
          break
        elsif response_status == "FAILED"
          @response_status = response_status
          break
        end
        sleep 5
      end
    

    这个getOneAuthResultReuqest()将调用用户模型中的方法,并开始调用我创建的用于访问Freja API的另一个方法。

      # Get the result by sending the authRef
      def getOneAuthResultReuqest(authRef)
        data = {authRef: authRef}
        enc_data = Base64.urlsafe_encode64(data.to_json)
    
        response = JSON.parse(self.class.post("/users/1.0/getOneAuthResult", body: {getOneAuthResultRequest: enc_data}).response.body
      end
    

    在这里,我只是使用了一个循环睡眠5秒来等待2分钟。 我认为这不是一个好方法,

    请帮我找出这方面的最佳做法。

    1 回复  |  直到 8 年前
        1
  •  0
  •   ADMAT Bandara    8 年前

    完成这类任务的最佳方法是使用客户端。 我想通过客户端完成这个过程。

    function GetAuthResult(authRef, user_login, remember_me){
      var closeInSeconds = 120,
      displayText = "Du har 2 minuter att logga in tills sessionen löperut",
     timer;
    
    swal({
     title: "Öppna Freja eID-appen",
     text: displayText,
     timer: closeInSeconds * 1000,
     showConfirmButton: false
    });
    
    timer = setInterval(function() {
     closeInSeconds--;
    
     if (closeInSeconds < 0) {
         clearInterval(timer);
         swal ( "Hoppsan!" ,  "Din inloggningstid har löpt ut!" ,  "error" );
     }
    
     $.ajax("/api/v3/request_auth_result", {
         type: "POST",
         dataType: 'json',
         data: {
           authRef: authRef
         },
    
    推荐文章