Class: RunApi::Core::HttpClient
- Inherits:
-
Object
- Object
- RunApi::Core::HttpClient
- Defined in:
- lib/runapi/core/http_client.rb
Constant Summary collapse
- STALE_CONNECTION_ERRORS =
[ Errno::EPIPE, EOFError, IOError, OpenSSL::SSL::SSLError ].freeze
Instance Method Summary collapse
-
#initialize(options) ⇒ HttpClient
constructor
A new instance of HttpClient.
- #request(method, path, body: nil, options: nil) ⇒ Object
Constructor Details
#initialize(options) ⇒ HttpClient
Returns a new instance of HttpClient.
8 9 10 11 12 13 |
# File 'lib/runapi/core/http_client.rb', line 8 def initialize() @options = @pool = ConnectionPool.new(size: 5, timeout: 5) do build_connection end end |
Instance Method Details
#request(method, path, body: nil, options: nil) ⇒ Object
15 16 17 18 19 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 |
# File 'lib/runapi/core/http_client.rb', line 15 def request(method, path, body: nil, options: nil) uri = URI.join(@options.base_url, path) req = build_request(method, uri, body, ) max_retries = &.max_retries || @options.max_retries retries = 0 stale_retried = false loop do response = begin @pool.with do |http| http.start unless http.started? http.request(req) end rescue *STALE_CONNECTION_ERRORS unless stale_retried stale_retried = true next end raise NetworkError, "Connection lost" rescue ::Net::OpenTimeout, ::Net::ReadTimeout => e raise TimeoutError, e. rescue ::SocketError, ::Errno::ECONNREFUSED, ::Errno::ECONNRESET => e raise NetworkError, e. end return parse_body(response.body) if response.is_a?(Net::HTTPSuccess) error = Error.from_response(response, response.body) if retryable?(method, response.code.to_i) && retries < max_retries retries += 1 sleep(retry_delay(retries, error)) stale_retried = false next end raise error end end |