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
-
#upload(url, headers:, body:) ⇒ Object
PUT bytes straight to a pre-authorized upload URL with the exact headers issued for it.
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 54 55 56 57 58 59 60 61 |
# 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 if response.is_a?(Net::HTTPSuccess) body = parse_body(response.body) return nil if body.nil? return body unless body.is_a?(Hash) || body.is_a?(Array) return Response.new(body:, headers: response_headers(response)) end 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 ensure close_multipart_files(req) end |
#upload(url, headers:, body:) ⇒ Object
PUT bytes straight to a pre-authorized upload URL with the exact headers issued for it. Skips the base URL, auth, and retries: the URL is single-use and the body is not safe to replay.
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/runapi/core/http_client.rb', line 66 def upload(url, headers:, body:) uri = URI.parse(url) http = Net::HTTP.new(uri.host, uri.port) http.use_ssl = (uri.scheme == "https") http.open_timeout = @options.timeout http.read_timeout = @options.timeout req = Net::HTTP::Put.new(uri.request_uri) headers.each { |key, value| req[key.to_s] = value } req.body = body response = http.start { |connection| connection.request(req) } return if response.is_a?(Net::HTTPSuccess) raise Error.from_response(response, response.body) rescue ::Net::OpenTimeout, ::Net::ReadTimeout => e raise TimeoutError, e. rescue ::SocketError, ::Errno::ECONNREFUSED, ::Errno::ECONNRESET => e raise NetworkError, e. end |