Module: HTTP

Defined in:
lib/HTTP/VERSION.rb,
lib/HTTP/RETRY.rb,
lib/HTTP/METHODS.rb,
lib/HTTP/request.rb,
lib/HTTP/request_with_body.rb,
lib/HTTP/request_without_body.rb

Overview

HTTP/VERSION.rb HTTP::VERSION

Defined Under Namespace

Modules: METHODS, RETRY

Constant Summary collapse

VERBS =

Deprecated alias for METHODS; to be removed in 2.0.0.

METHODS
VERSION =
'1.3.0'

Class Method Summary collapse

Class Method Details

.backoff_delay(base, attempt) ⇒ Object



62
63
64
# File 'lib/HTTP/RETRY.rb', line 62

def backoff_delay(base, attempt)
  base * (2 ** (attempt - 1)) * (1 + (rand - 0.5) * 0.4)
end

.request(uri, request_object, headers = {}, options = {}, &block) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/HTTP/request.rb', line 15

def request(uri, request_object, headers = {}, options = {}, &block)
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
  http = Net::HTTP.new(uri.host, uri.port)
  no_redirect = options.delete(:no_redirect)
  username = options.delete(:username)
  password = options.delete(:password)
  config = retry_config(options)
  http.options = options.merge(
    use_ssl: (options[:use_ssl] || uri.use_ssl?),
    verify_mode: (options[:verify_mode] || OpenSSL::SSL::VERIFY_PEER)
  )
  request_object.headers = headers
  if username
    request_object.basic_auth(username, password)
  elsif uri.user
    request_object.basic_auth(uri.user, uri.password)
  end
  request_method = request_object.method.downcase.to_sym
  response = (
    if config[:retries] > 0 && config[:methods].include?(request_method)
      with_retries(http, request_object, config)
    else
      http.request(request_object)
    end
  )
  if response.code =~ /^3/
    if block_given? && no_redirect
      yield response
    elsif no_redirect
      return response
    end
    redirect_uri = uri.merge(response['location'])
    if response.code =~ /^30[78]$/
      data = METHODS::WITH_BODY.include?(request_method) ? request_object.body : {}
      response = send(request_method, redirect_uri.to_s, data, headers, options, &block)
    else
      response = get(redirect_uri.to_s, {}, {}, options, &block)
    end
  end
  if block_given?
    yield response
  else
    response
  end
end

.request_with_body(method, uri, data = {}, headers = {}, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/HTTP/request_with_body.rb', line 10

def request_with_body(method, uri, data = {}, headers = {}, options = {}, &block)
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
  request_object = Net::HTTP.const_get(method.to_s.capitalize).new(uri.request_uri)
  content_type = headers.find{|k, v| k.downcase == 'content-type'}&.last.to_s
  if data.is_a?(String)
    request_object.body = data
  elsif content_type.start_with?('application/json')
    request_object.body = JSON.dump(data)
  else
    request_object.form_data = data
  end
  request(uri, request_object, headers, options, &block)
end

.request_without_body(method, uri, args = {}, headers = {}, options = {}, &block) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/HTTP/request_without_body.rb', line 10

def request_without_body(method, uri, args = {}, headers = {}, options = {}, &block)
  uri = uri.is_a?(URI) ? uri : URI.parse(uri)
  request_uri = uri.request_uri
  unless args.empty?
    request_uri += '?' + args.x_www_form_urlencode
  end
  request_object = Net::HTTP.const_get(method.to_s.capitalize).new(request_uri)
  request(uri, request_object, headers, options, &block)
end

.retry_after(response) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/HTTP/RETRY.rb', line 67

def retry_after(response)
  header = response['Retry-After']
  return nil unless header
  if header =~ /\A\d+\z/
    header.to_i
  else
    # Malformed HTTP-date — fall through to caller's backoff.
    delta = Time.httpdate(header) - Time.now rescue nil
    delta && [delta, 0].max
  end
end

.retry_config(options) ⇒ Object



31
32
33
34
35
36
37
38
39
# File 'lib/HTTP/RETRY.rb', line 31

def retry_config(options)
  {
    retries: options.delete(:retries) || 0,
    delay: options.delete(:retry_delay) || 1.0,
    status_codes: options.delete(:retry_status_codes) || RETRY::STATUS_CODES,
    exceptions: options.delete(:retry_exceptions) || RETRY::EXCEPTIONS,
    methods: [options.delete(:retry_methods), options.delete(:retry_verbs)].compact.first || RETRY::METHODS
  }
end

.with_retries(http, request_object, config) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/HTTP/RETRY.rb', line 42

def with_retries(http, request_object, config)
  attempt = 0
  loop do
    begin
      response = http.request(request_object)
      if config[:status_codes].include?(response.code.to_i) && attempt < config[:retries]
        attempt += 1
        RETRY.sleep(retry_after(response) || backoff_delay(config[:delay], attempt))
        next
      end
      return response
    rescue *config[:exceptions]
      raise unless attempt < config[:retries]
      attempt += 1
      RETRY.sleep(backoff_delay(config[:delay], attempt))
    end
  end
end