Last active
August 29, 2015 14:03
-
-
Save huacnlee/1ebb903014f26c0f9cf4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def api_request(action, params = {}) | |
Rails.logger.info "Started request #{host_root} action: #{action}" | |
# Rails.logger.info " Params: #{params.inspect}" | |
t1 = Time.now | |
n = 0 | |
begin | |
res = Faraday.post do |req| | |
req.url "http://api.kanbox.com" | |
req.body = Kanbox::Param.parse(action, params) | |
req.options[:open_timeout] = 30 | |
end | |
rescue Faraday::TimeoutError | |
sleep 0.2 | |
if n < 3 | |
n += 1 | |
retry | |
end | |
e = Exception.new("Kanbox.api_request [#{action}] request timeout. \nhost: #{host_root} \nparams: #{params.inspect}") | |
raise Faraday::TimeoutError | |
end | |
t2 = Time.now | |
Rails.logger.info "Completed (#{((t2 - t1) * 1000).round(2)}ms)" | |
JSON.parse(res.body) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment