[英]How to fork and merge a REST API call that takes a list of id's as query param?
我必须调用第三方 GET API,它采用逗号分隔的 ID 列表并返回 JSON 响应。 像这样:
def call(sysid)
# RestClient GET https://test.com?ids=sysid.join(',')&....
# parse response and returns
# rescue - any error handling
end
但是 sysid 是一个输入,有时可以有 300+ 个元素,并且由于 GET 的限制,我的 api 调用错误(可能是因为 URL 长度变得太大)
我如何有效地发送 API 次调用(小批量)并将结果合并回来。 在这样做时: -
Ruby 中是否有针对此类问题的良好模式?
我不知道好的 ruby 模式,但我们可以通过以下代码解决问题
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
解释
您可以返回响应 object 或如果状态为 false 则发送特定响应
这是伪代码,但我认为它可能会解决您的问题。
谢谢你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.