Module: DeepL::Utils::HTTPClientBuilder

Extended by:
HTTPClientBuilder
Included in:
HTTPClientBuilder
Defined in:
lib/deepl/utils/http_client_builder.rb

Constant Summary collapse

PROXY_SCHEMES =
%w[http https].freeze

Instance Method Summary collapse

Instance Method Details

#build(host, options) ⇒ Net::HTTP

Builds an unstarted Net::HTTP client for host configured with options. Net::HTTP does not retry on its own (+max_retries+ 0): only the library's retries apply.

Parameters:

Returns:

  • (Net::HTTP)


20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deepl/utils/http_client_builder.rb', line 20

def build(host, options) # rubocop:disable Metrics/AbcSize, Metrics/MethodLength
  uri = URI(host)
  http = Net::HTTP.new(uri.host, uri.port, *proxy_arguments(options.proxy, uri))
  http.use_ssl = uri.scheme == 'https'
  disable_lenient_defaults(http)
  http.verify_mode = OpenSSL::SSL::VERIFY_NONE unless options.enable_ssl_verification
  http.ca_file = options.cert_path if options.cert_path
  http.open_timeout = options.open_timeout unless options.open_timeout.nil?
  http.read_timeout = options.read_timeout unless options.read_timeout.nil?
  http.write_timeout = options.write_timeout unless options.write_timeout.nil?
  http.ssl_timeout = options.ssl_timeout unless options.ssl_timeout.nil?
  http
end

#disable_lenient_defaults(http) ⇒ Object

Net::HTTP would re-send idempotent requests once on its own, outside max_retries, and would return a body shorter than its Content-Length as complete (+ignore_eof+, net-http 0.3+).



37
38
39
40
41
# File 'lib/deepl/utils/http_client_builder.rb', line 37

def disable_lenient_defaults(http)
  http.max_retries = 0
  http.ignore_eof = false if http.respond_to?(:ignore_eof=)
  http
end