Skip to content

Instantly share code, notes, and snippets.

@huacnlee
Last active August 29, 2015 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huacnlee/1ebb903014f26c0f9cf4 to your computer and use it in GitHub Desktop.
Save huacnlee/1ebb903014f26c0f9cf4 to your computer and use it in GitHub Desktop.
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