Class: Net::Hippie::Client

Inherits:
Object
  • Object
show all
Includes:
TlsParser
Defined in:
lib/net/hippie/client.rb

Overview

HTTP client with connection pooling, DNS caching, and retry logic.

Constant Summary collapse

DEFAULT_HEADERS =
{
  'Accept' => 'application/json',
  'Content-Type' => 'application/json',
  'User-Agent' => "net/hippie #{VERSION}"
}.freeze
JITTER =
Random.new.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from TlsParser

#parse_cert, #parse_key

Constructor Details

#initialize(options = {}) ⇒ Client

Returns a new instance of Client.



22
23
24
25
26
27
28
29
30
# File 'lib/net/hippie/client.rb', line 22

def initialize(options = {})
  @options = options
  @mapper = options.fetch(:mapper, ContentTypeMapper.new)
  @logger = options.fetch(:logger, Net::Hippie.logger)
  @follow_redirects = options.fetch(:follow_redirects, 0)
  @default_headers = options.fetch(:headers, DEFAULT_HEADERS).freeze
  configure_pool(options)
  configure_tls(options)
end

Instance Attribute Details

#follow_redirectsObject (readonly)

Returns the value of attribute follow_redirects.



20
21
22
# File 'lib/net/hippie/client.rb', line 20

def follow_redirects
  @follow_redirects
end

#loggerObject (readonly)

Returns the value of attribute logger.



20
21
22
# File 'lib/net/hippie/client.rb', line 20

def logger
  @logger
end

#mapperObject (readonly)

Returns the value of attribute mapper.



20
21
22
# File 'lib/net/hippie/client.rb', line 20

def mapper
  @mapper
end

Instance Method Details

#close_allObject



65
66
67
68
# File 'lib/net/hippie/client.rb', line 65

def close_all
  @pool.close_all
  @dns_cache.clear
end

#execute(uri, request, limit: follow_redirects, stream: false, &block) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/net/hippie/client.rb', line 44

def execute(uri, request, limit: follow_redirects, stream: false, &block)
  conn = connection_for(uri)
  return conn.run(request) { |res| res.read_body(&block) } if stream && block

  return execute_with_block(conn, request, &block) if block

  response = conn.run(request)
  limit.positive? && response.is_a?(Net::HTTPRedirection) ? follow_redirect(uri, response, limit) : response
end

#with_retry(retries: 3) ⇒ Object



54
55
56
57
58
59
60
61
62
63
# File 'lib/net/hippie/client.rb', line 54

def with_retry(retries: 3)
  retries = [retries.to_i, 0].max
  0.upto(retries) do |attempt|
    return yield self
  rescue *CONNECTION_ERRORS => error
    raise if attempt == retries

    sleep_with_backoff(attempt, retries, error)
  end
end