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
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: -
Is there a good pattern in Ruby for this type of problem?
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
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.