Sha256: ccc25ec76d3d70ff209249b2d67979da6cd4e964b2f1520219d6886ffa683365

Contents?: true

Size: 1.18 KB

Versions: 5

Compression:

Stored size: 1.18 KB

Contents

# frozen_string_literal: true

class EzClient::Client
  REQUEST_OPTION_KEYS = %i[
    api_auth
    basic_auth
    headers
    keep_alive
    max_retries
    on_complete
    on_error
    on_retry
    retry_exceptions
    ssl_context
    timeout
  ].freeze

  def initialize(options = {})
    self.request_options = options
    self.clients = {}
    EzClient::CheckOptions.call(options, REQUEST_OPTION_KEYS)
  end

  def request(verb, url, **options)
    options = { **request_options, **options }

    keep_alive_timeout = options.delete(:keep_alive)
    api_auth = options.delete(:api_auth)

    if keep_alive_timeout
      client = persistent_client_for(url, timeout: keep_alive_timeout)
    else
      client = HTTP::Client.new
    end

    EzClient::Request.new(verb, url, client: client, **options).tap do |request|
      request.api_auth!(*api_auth) if api_auth
    end
  end

  def perform(*args)
    request(*args).perform
  end

  def perform!(*args)
    request(*args).perform!
  end

  private

  attr_accessor :request_options, :clients

  def persistent_client_for(url, timeout: 600)
    uri = HTTP::URI.parse(url)
    clients[uri.origin] ||= HTTP.persistent(uri.origin, timeout: timeout)
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
ezclient-0.13.1 lib/ezclient/client.rb
ezclient-0.13.0 lib/ezclient/client.rb
ezclient-0.12.0 lib/ezclient/client.rb
ezclient-0.11.0 lib/ezclient/client.rb
ezclient-0.10.0 lib/ezclient/client.rb