【问题标题】:How to verify the API response status in Rails如何在 Rails 中验证 API 响应状态
【发布时间】:2019-06-04 11:41:21
【问题描述】:

我正在使用 HTTParty gem 在我的 Rails 应用程序中发出请求。

  1. 发出 POST 请求以生成报告
  2. 发出 GET 请求以检查报告状态
  3. 报告状态完成后下载报告

我需要进行上述 GET 请求,直到报告状态完成。报告状态需要一些时间来更新状态。

如何有效检查报告状态?

def get_report(id)
// making requests
end

report = get_report(3)
report['status']

【问题讨论】:

  • while get_report(3)['status'] == 'pending'; sleep(10); end; 然后执行post 请求
  • 我想在报告状态完成后下载报告。你的意思是在while循环之后执行get请求吗
  • 我建议您: 1- 调用生成报告; 2-每10秒调用一次循环检查报告的状态; 3-一旦报告为done,您调用下载api;除非下载 API 与状态检查相同,否则您需要使用 API 响应更新答案,以便我们为您提供更多帮助。

标签: ruby-on-rails api httparty


【解决方案1】:

您应该使用 ActiveJob 来执行此类过程。用法如下。

 class ReportingGenerationJob < ActiveJob::Base
  queue_as :default

  after_perform do |job|
    ReportGenrator.notify_ready_to_download(job.arguments.first)
  end

  def perform(report_id)
    ReportGenerator.generate(report_id) #Your ReportGenerator has to expose api for get the current status
  end
end

【讨论】:

    猜你喜欢
    • 2023-02-03
    • 2017-02-23
    • 2012-11-25
    • 2019-07-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-19
    • 1970-01-01
    相关资源
    最近更新 更多