class Loggie::Request

Makes external HTTP request, with a retry mechanism for polling long running queries on remote server

Attributes

retry_mechanism[R]

Public Class Methods

new(retry_mechanism: ) click to toggle source
# File lib/loggie/request.rb, line 10
def initialize(retry_mechanism: )
  @retry_mechanism = retry_mechanism || Retry.new
end

Public Instance Methods

call(url, method: :get, options: nil) click to toggle source
# File lib/loggie/request.rb, line 14
def call(url, method: :get, options: nil)
  retry_mechanism.call(url, method, options) do |url, method, options|
    request(url, method: method, options: options)
  end
end

Private Instance Methods

request(url, method:, options: nil) click to toggle source
# File lib/loggie/request.rb, line 24
def request(url, method:, options: nil)
  encoded_options = URI.encode_www_form(options) if options

  url = if method == :get
          URI([url, encoded_options].compact.join("?"))
        else
          URI(url)
        end

  http = Net::HTTP.new(url.host, url.port)
  http.use_ssl = true
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE

  request = if method == :get
              Net::HTTP::Get.new(url)
            else
              Net::HTTP::Post.new(url)
            end
  request["x-api-key"] = Loggie.configuration.read_token

  if method == :post
    request["content-type"] = 'application/json'
    request.body = options.to_json
  end

  http.request(request)
end