Class: EzClient::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/ezclient/client.rb

Constant Summary collapse

REQUEST_OPTION_KEYS =
%i[
  api_auth
  basic_auth
  cookies
  headers
  keep_alive
  max_retries
  on_complete
  on_error
  on_retry
  retry_exceptions
  ssl_context
  timeout
].freeze

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



19
20
21
22
23
# File 'lib/ezclient/client.rb', line 19

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

Instance Method Details

#perform(*args) ⇒ Object



42
43
44
# File 'lib/ezclient/client.rb', line 42

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

#perform!(*args) ⇒ Object



46
47
48
# File 'lib/ezclient/client.rb', line 46

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

#request(verb, url, **options) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ezclient/client.rb', line 25

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