module MingleEvents::Http

Constants

MAX_RETRY_TIMES

Public Instance Methods

get(url, retry_count=0, max_retry_times=MAX_RETRY_TIMES, &block) click to toggle source

get response body for a url, a block can be passed in for request pre-processing

   # File lib/mingle_events/http.rb
 8     def get(url, retry_count=0, max_retry_times=MAX_RETRY_TIMES, &block)
 9       rsp = nil
10       retrying = lambda do
11         raise HttpError.new(rsp, url) if retry_count >= max_retry_times
12         cooldown = retry_count * 2
13         MingleEvents.log.info "Getting service error when get page at #{url}, retry after #{cooldown}s..."
14         sleep cooldown
15         get(url, retry_count + 1, max_retry_times, &block)
16       end
17 
18       begin
19         rsp = fetch_page_response(url, &block)
20       rescue EOFError => e
21         return retrying.call
22       end
23 
24       case rsp
25       when Net::HTTPSuccess
26         rsp.body
27       when Net::HTTPUnauthorized
28         raise HttpError.new(rsp, url, %{
29 If you think you are passing correct credentials, please check
30 that you have enabled Mingle for basic authentication.
31 See <http://www.thoughtworks-studios.com/mingle/3.3/help/configuring_mingle_authentication.html>.})
32       when Net::HTTPBadGateway, Net::HTTPServiceUnavailable, Net::HTTPGatewayTimeOut
33         retrying.call
34       else
35         raise HttpError.new(rsp, url)
36       end
37     end

Private Instance Methods

fetch_page_response(url) { |req| ... } click to toggle source
   # File lib/mingle_events/http.rb
40 def fetch_page_response(url, &block)
41   uri = URI.parse(url)
42   http = Net::HTTP.new(uri.host, uri.port)
43   path = uri.request_uri
44 
45   if uri.scheme == 'https'
46     http.use_ssl = true
47     http.verify_mode = OpenSSL::SSL::VERIFY_NONE
48   end
49 
50   MingleEvents.log.info "Fetching page at #{path}..."
51 
52   start = Time.now
53   req = Net::HTTP::Get.new(path)
54   yield(req) if block_given?
55   rsp = http.request(req)
56   MingleEvents.log.info "...#{path} fetched in #{Time.now - start} seconds."
57   rsp
58 end