Module: Optimizely::Helpers::HttpUtils

Defined in:
lib/optimizely/helpers/http_utils.rb

Class Method Summary collapse

Class Method Details

.make_request(url, http_method, request_body = nil, headers = {}, read_timeout = nil, proxy_config = nil) ⇒ Object

rubocop:disable Metrics/ParameterLists

Raises:



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
60
61
62
63
64
65
# File 'lib/optimizely/helpers/http_utils.rb', line 27

def make_request(url, http_method, request_body = nil, headers = {}, read_timeout = nil, proxy_config = nil) # rubocop:disable Metrics/ParameterLists
  # makes http/https GET/POST request and returns response
  #
  uri = URI.parse(url)

  raise HTTPUriError unless uri.respond_to?(:request_uri)

  case http_method
  when :get
    request = Net::HTTP::Get.new(uri.request_uri)
  when :post
    request = Net::HTTP::Post.new(uri.request_uri)
    request.body = request_body if request_body
  else
    return nil
  end

  # set headers
  headers&.each do |key, val|
    request[key] = val
  end

  # do not try to make request with proxy unless we have at least a host
  http_class = if proxy_config&.host
                 Net::HTTP::Proxy(
                   proxy_config.host,
                   proxy_config.port,
                   proxy_config.username,
                   proxy_config.password
                 )
               else
                 Net::HTTP
               end

  http = http_class.new(uri.host, uri.port)
  http.read_timeout = read_timeout if read_timeout
  http.use_ssl = uri.scheme == 'https'
  http.request(request)
end