如何分叉和合并采用 id 列表作为查询参数的 REST API 调用?

发布于 2025-01-16 16:59:50 字数 713 浏览 2 评论 0原文

我必须调用第三方 GET API,它采用逗号分隔的 id 列表并返回 JSON 响应。 像这样:

def call(sysid)
   # RestClient GET  https://test.com?ids=sysid.join(',')&....
   # parse response and returns
   # rescue - any error handling
end

RestClient

但是 sysid 是一个输入,有时可能有 300 多个元素,并且由于由于 GET 的限制,我的 api 调用出错(可能是因为 URL 长度变得太大)

我如何有效地发送 API 调用(小批量)&将结果合并回来。这样做时: -

  • 我应该使用任何 并行
    • 如果其中一个批量调用失败,可能会添加重试
      • 如果仍然失败,请停止整个过程并重新启动。从 call() 返回错误

Ruby 中有针对此类问题的良好模式吗?

I have to call a third party GET API which takes a comma seperated list of ids and returns a JSON response.
Like so:

def call(sysid)
   # RestClient GET  https://test.com?ids=sysid.join(',')&....
   # parse response and returns
   # rescue - any error handling
end

RestClient

But sysid is an input and can sometimes have 300+ elements and due to the limitation of GET, my api call errors out (probably because URL length becomes too big)

How can I efficiently send API calls (in small batches) & merge results back. In doing that:
-

  • should I use any Parallelism
    • if one of the batch call fails, may be add retries
      • if it still fail, stop the whole process & return an error from call()

Is there a good pattern in Ruby for this type of problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

诗笺 2025-01-23 16:59:50

我不知道好的红宝石模式,但我们可以通过下面的代码

def call(sysid)
  fetch_next = true
  response = {status: true, result: []}
  while fetch_next
    param_value_id = sysid[0..n-1]
    while sysid.present?
      begin
        call API with FARADAY/RestClient with its params as param_value_id.join(',')
        if success
          parse response and append in responses result
        else
          fetch_next = false
          response[:status] = false
          response[:result] = []
        end
      rescue Exception => e
        Rails.logger.warn "Exception raised due to #{e}"
        fetch_next = false
        response[:status] = false
        response[:result] = []
      end
    end
    sysid = sysid.drop(n)
    if sysid.empty?
      fetch_next = false
    end
  end
  response
end

说明

  1. fetch_next 设置为 true 和从调用返回的 response 哈希 来解决问题方法。
  2. 直到 fetch_next 为 true,我们将从 sysid 获取 n 个 id,
  3. 直到 sysid 存在,我们将调用第三方 api< /strong> 带有预期参数
  4. ,如果响应成功,我们将其附加响应的结果键 哈希值(
  5. 如果有)是失败异常,我们会将fetch_next设置为false状态 response 对 false 和 empty result 键的键
  6. 最后,我们从 sysid 数组中删除 n 个值,并将设置fetch_next 为 false 如果系统 ID 为空。

如果状态为 false,您可以返回响应对象或发送特定响应

这是伪代码,但我认为它可能会解决您的问题。

谢谢。

I am not aware of good ruby pattern but we can solve issue by below code

def call(sysid)
  fetch_next = true
  response = {status: true, result: []}
  while fetch_next
    param_value_id = sysid[0..n-1]
    while sysid.present?
      begin
        call API with FARADAY/RestClient with its params as param_value_id.join(',')
        if success
          parse response and append in responses result
        else
          fetch_next = false
          response[:status] = false
          response[:result] = []
        end
      rescue Exception => e
        Rails.logger.warn "Exception raised due to #{e}"
        fetch_next = false
        response[:status] = false
        response[:result] = []
      end
    end
    sysid = sysid.drop(n)
    if sysid.empty?
      fetch_next = false
    end
  end
  response
end

EXPLANATION

  1. setting fetch_next to true and response hash to be returned from call method.
  2. till fetch_next is true, we'll fetch n ids from sysid
  3. till sysid is there, we'll call third party api with intended params
  4. if response is success, we append it in result key of response hash
  5. if there is failure or an exception, we'll set fetch_next to false, status key of response to false and empty result key
  6. In the end we drop n values from sysid array and will set fetch_next to false if sysid is empty.

you can return response object or send particular response if status is false

This is pseudo code but i think it might solve your problem.

Thank you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文