Class: OpinionatedHTTP::Client

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

Constant Summary collapse

HTTP_RETRY_CODES =

502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout

%w[502 503 504].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(secret_config_prefix:, metric_prefix:, error_class:, logger: nil, format: nil, retry_count: 11, retry_interval: 0.01, retry_multiplier: 1.8, http_retry_codes: HTTP_RETRY_CODES.join(","), url: nil, pool_size: 100, open_timeout: 10, read_timeout: 10, idle_timeout: 300, keep_alive: 300, pool_timeout: 5, warn_timeout: 0.25, proxy: :ENV, force_retry: true, max_redirects: 10, after_connect: nil, verify_peer: false, certificate: nil, private_key: nil, header: nil) ⇒ Client

Any option supplied here can be overridden if that corresponding value is set in Secret Config.



20
21
22
23
24
25
26
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/opinionated_http/client.rb', line 20

def initialize(
  secret_config_prefix:,
  metric_prefix:,
  error_class:,
  logger: nil,
  format: nil,
  retry_count: 11,
  retry_interval: 0.01,
  retry_multiplier: 1.8,
  http_retry_codes: HTTP_RETRY_CODES.join(","),
  url: nil,
  pool_size: 100,
  open_timeout: 10,
  read_timeout: 10,
  idle_timeout: 300,
  keep_alive: 300,
  pool_timeout: 5,
  warn_timeout: 0.25,
  proxy: :ENV,
  force_retry: true,
  max_redirects: 10,
  after_connect: nil,
  verify_peer: false, # TODO: PersistentHTTP keeps returning cert expired even when it is valid.
  certificate: nil,
  private_key: nil,
  header: nil
)
  @metric_prefix = metric_prefix
  @logger        = logger || SemanticLogger[self]
  @error_class   = error_class
  @format        = format
  @after_connect = after_connect
  SecretConfig.configure(secret_config_prefix) do |config|
    @retry_count      = config.fetch("retry_count", type: :integer, default: retry_count)
    @retry_interval   = config.fetch("retry_interval", type: :float, default: retry_interval)
    @retry_multiplier = config.fetch("retry_multiplier", type: :float, default: retry_multiplier)
    @max_redirects    = config.fetch("max_redirects", type: :integer, default: max_redirects)
    http_retry_codes  = config.fetch("http_retry_codes", type: :string, default: http_retry_codes)
    @http_retry_codes = http_retry_codes.split(",").collect(&:strip)

    @url = url.nil? ? config.fetch("url") : config.fetch("url", default: url)

    @pool_size    = config.fetch("pool_size", type: :integer, default: pool_size)
    @open_timeout = config.fetch("open_timeout", type: :float, default: open_timeout)
    @read_timeout = config.fetch("read_timeout", type: :float, default: read_timeout)
    @idle_timeout = config.fetch("idle_timeout", type: :float, default: idle_timeout)
    @keep_alive   = config.fetch("keep_alive", type: :float, default: keep_alive)
    @pool_timeout = config.fetch("pool_timeout", type: :float, default: pool_timeout)
    @warn_timeout = config.fetch("warn_timeout", type: :float, default: warn_timeout)
    @proxy        = config.fetch("proxy", type: :symbol, default: proxy)
    @force_retry  = config.fetch("force_retry", type: :boolean, default: force_retry)
    @certificate  = config.fetch("certificate", type: :string, default: certificate)
    @private_key  = config.fetch("private_key", type: :string, default: private_key)
    @verify_peer  = config.fetch("verify_peer", type: :boolean, default: verify_peer)
  end

  internal_logger = OpinionatedHTTP::Logger.new(@logger)
  @driver         = PersistentHTTP.new(
    url:           @url,
    logger:        internal_logger,
    debug_output:  internal_logger,
    name:          "",
    pool_size:     @pool_size,
    open_timeout:  @open_timeout,
    read_timeout:  @read_timeout,
    idle_timeout:  @idle_timeout,
    keep_alive:    @keep_alive,
    pool_timeout:  @pool_timeout,
    warn_timeout:  @warn_timeout,
    proxy:         @proxy,
    force_retry:   @force_retry,
    after_connect: @after_connect,
    certificate:   @certificate,
    private_key:   @private_key,
    verify_mode:   @verify_peer ? OpenSSL::SSL::VERIFY_PEER | OpenSSL::SSL::VERIFY_FAIL_IF_NO_PEER_CERT : OpenSSL::SSL::VERIFY_NONE,
    header:        header
  )
end

Instance Attribute Details

#after_connectObject (readonly)

Returns the value of attribute after_connect.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def after_connect
  @after_connect
end

#driverObject (readonly)

Returns the value of attribute driver.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def driver
  @driver
end

#error_classObject (readonly)

Returns the value of attribute error_class.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def error_class
  @error_class
end

#force_retryObject (readonly)

Returns the value of attribute force_retry.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def force_retry
  @force_retry
end

#formatObject (readonly)

Returns the value of attribute format.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def format
  @format
end

#http_retry_codesObject (readonly)

Returns the value of attribute http_retry_codes.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def http_retry_codes
  @http_retry_codes
end

#idle_timeoutObject (readonly)

Returns the value of attribute idle_timeout.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def idle_timeout
  @idle_timeout
end

#keep_aliveObject (readonly)

Returns the value of attribute keep_alive.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def keep_alive
  @keep_alive
end

#loggerObject (readonly)

Returns the value of attribute logger.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def logger
  @logger
end

#max_redirectsObject (readonly)

Returns the value of attribute max_redirects.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def max_redirects
  @max_redirects
end

#metric_prefixObject (readonly)

Returns the value of attribute metric_prefix.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def metric_prefix
  @metric_prefix
end

#open_timeoutObject (readonly)

Returns the value of attribute open_timeout.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def open_timeout
  @open_timeout
end

#pool_sizeObject (readonly)

Returns the value of attribute pool_size.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def pool_size
  @pool_size
end

#pool_timeoutObject (readonly)

Returns the value of attribute pool_timeout.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def pool_timeout
  @pool_timeout
end

#proxyObject (readonly)

Returns the value of attribute proxy.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def proxy
  @proxy
end

#read_timeoutObject (readonly)

Returns the value of attribute read_timeout.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def read_timeout
  @read_timeout
end

#retry_countObject (readonly)

Returns the value of attribute retry_count.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def retry_count
  @retry_count
end

#retry_intervalObject (readonly)

Returns the value of attribute retry_interval.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def retry_interval
  @retry_interval
end

#retry_multiplierObject (readonly)

Returns the value of attribute retry_multiplier.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def retry_multiplier
  @retry_multiplier
end

#secret_config_prefixObject (readonly)

Returns the value of attribute secret_config_prefix.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def secret_config_prefix
  @secret_config_prefix
end

#urlObject (readonly)

Returns the value of attribute url.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def url
  @url
end

#warn_timeoutObject (readonly)

Returns the value of attribute warn_timeout.



13
14
15
# File 'lib/opinionated_http/client.rb', line 13

def warn_timeout
  @warn_timeout
end

Instance Method Details

#delete(request: nil, **args) ⇒ Object



121
122
123
124
125
126
# File 'lib/opinionated_http/client.rb', line 121

def delete(request: nil, **args)
  request       ||= Request.new(**args)
  request.verb  = "Delete"
  http_response = request(request)
  Response.new(http_response, request)
end

#get(request: nil, **args) ⇒ Object



99
100
101
102
103
104
# File 'lib/opinionated_http/client.rb', line 99

def get(request: nil, **args)
  request       ||= Request.new(**args)
  request.verb  = "Get"
  http_response = request(request)
  Response.new(http_response, request)
end

#patch(request: nil, **args) ⇒ Object



128
129
130
131
132
133
# File 'lib/opinionated_http/client.rb', line 128

def patch(request: nil, **args)
  request       ||= Request.new(**args)
  request.verb  = "Patch"
  http_response = request(request)
  Response.new(http_response, request)
end

#post(request: nil, json: nil, body: nil, **args) ⇒ Object

Raises:

  • (ArgumentError)


106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/opinionated_http/client.rb', line 106

def post(request: nil, json: nil, body: nil, **args)
  raise(ArgumentError, "Either set :json or :body") if json && body

  request       ||= Request.new(**args)
  if json
    request.format = :json
    request.body   = json
  else
    request.body = body
  end
  request.verb  = "Post"
  http_response = request(request)
  Response.new(http_response, request)
end

#request(request) ⇒ Object

Returns [Response] after submitting the [Request]



136
137
138
139
140
141
142
# File 'lib/opinionated_http/client.rb', line 136

def request(request)
  request.metric_prefix ||= metric_prefix
  request.format        ||= format
  request.error_class   ||= error_class
  request.logger        ||= logger
  request_with_retry(action: request.action, request: request.http_request)
end