如果服务器当时不可用,我想实现多次重试!。有可能吗?。
require 'httparty'
require 'pry'
require_relative '../../env.rb'
RSpec.describe 'Validar a api de usuários' do
it 'Deve retornar 200 para API DRIVE THRU' do
response = HttParty.get("#{URLS['drive_thru']}/v1/health/live")
expect(response.code).to eql(200)
expect(response.parsed_response).to be_nil
end
end
通用重试系统可能如下所示:
tries_left = 3
loop do
break if tries_left == 0
begin
result = do_thing
rescue SomeError => e
tries_left -= 1
end
end
您必须将do_thing
和SomeError
替换为HTTParty调用,无论出现什么错误。
另请参阅如何使用HTTParty处理错误?