lib/httpx/options.rb in httpx-0.24.7 vs lib/httpx/options.rb in httpx-1.0.0

- old
+ new

@@ -5,15 +5,14 @@ module HTTPX class Options BUFFER_SIZE = 1 << 14 WINDOW_SIZE = 1 << 14 # 16K MAX_BODY_THRESHOLD_SIZE = (1 << 10) * 112 # 112K - CONNECT_TIMEOUT = 60 - OPERATION_TIMEOUT = 60 KEEP_ALIVE_TIMEOUT = 20 SETTINGS_TIMEOUT = 10 - READ_TIMEOUT = WRITE_TIMEOUT = REQUEST_TIMEOUT = Float::INFINITY + CONNECT_TIMEOUT = READ_TIMEOUT = WRITE_TIMEOUT = 60 + REQUEST_TIMEOUT = OPERATION_TIMEOUT = Float::INFINITY # https://github.com/ruby/resolv/blob/095f1c003f6073730500f02acbdbc55f83d70987/lib/resolv.rb#L408 ip_address_families = begin list = Socket.ip_address_list if list.any? { |a| a.ipv6? && !a.ipv6_loopback? && !a.ipv6_linklocal? && !a.ipv6_unique_local? } @@ -29,10 +28,13 @@ :debug => ENV.key?("HTTPX_DEBUG") ? $stderr : nil, :debug_level => (ENV["HTTPX_DEBUG"] || 1).to_i, :ssl => {}, :http2_settings => { settings_enable_push: 0 }, :fallback_protocol => "http/1.1", + :supported_compression_formats => %w[gzip deflate], + :decompress_response_body => true, + :compress_request_body => true, :timeout => { connect_timeout: CONNECT_TIMEOUT, settings_timeout: SETTINGS_TIMEOUT, operation_timeout: OPERATION_TIMEOUT, keep_alive_timeout: KEEP_ALIVE_TIMEOUT, @@ -50,40 +52,17 @@ :request_body_class => Class.new(Request::Body), :response_body_class => Class.new(Response::Body), :connection_class => Class.new(Connection), :options_class => Class.new(self), :transport => nil, - :transport_options => nil, :addresses => nil, :persistent => false, :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, :resolver_options => { cache: true }, :ip_families => ip_address_families, }.freeze - begin - module HashExtensions - refine Hash do - def >=(other) - Hash[other] <= self - end - - def <=(other) - other = Hash[other] - return false unless size <= other.size - - each do |k, v| - v2 = other.fetch(k) { return false } - return false unless v2 == v - end - true - end - end - end - using HashExtensions - end unless Hash.method_defined?(:>=) - class << self def new(options = {}) # let enhanced options go through return options if self == Options && options.class < self return options if options.is_a?(self) @@ -98,52 +77,25 @@ optname = Regexp.last_match(1).to_sym attr_reader(optname) end - - def def_option(optname, *args, &block) - if args.empty? && !block - class_eval(<<-OUT, __FILE__, __LINE__ + 1) - def option_#{optname}(v); v; end # def option_smth(v); v; end - OUT - return - end - - deprecated_def_option(optname, *args, &block) - end - - def deprecated_def_option(optname, layout = nil, &interpreter) - warn "DEPRECATION WARNING: using `def_option(#{optname})` for setting options is deprecated. " \ - "Define module OptionsMethods and `def option_#{optname}(val)` instead." - - if layout - class_eval(<<-OUT, __FILE__, __LINE__ + 1) - def option_#{optname}(value) # def option_origin(v) - #{layout} # URI(v) - end # end - OUT - elsif interpreter - define_method(:"option_#{optname}") do |value| - instance_exec(value, &interpreter) - end - end - end end def initialize(options = {}) - __initialize__(options) + do_initialize(options) freeze end def freeze super @origin.freeze @base_path.freeze @timeout.freeze @headers.freeze @addresses.freeze + @supported_compression_formats.freeze end def option_origin(value) URI(value) end @@ -155,18 +107,15 @@ def option_headers(value) Headers.new(value) end def option_timeout(value) - timeouts = Hash[value] + Hash[value] + end - if timeouts.key?(:loop_timeout) - warn ":loop_timeout is deprecated, use :operation_timeout instead" - timeouts[:operation_timeout] = timeouts.delete(:loop_timeout) - end - - timeouts + def option_supported_compression_formats(value) + Array(value).map(&:to_s) end def option_max_concurrent_requests(value) raise TypeError, ":max_concurrent_requests must be positive" unless value.positive? @@ -194,11 +143,14 @@ value end def option_body_threshold_size(value) - Integer(value) + bytes = Integer(value) + raise TypeError, ":body_threshold_size must be positive" unless bytes.positive? + + bytes end def option_transport(value) transport = value.to_s raise TypeError, "#{transport} is an unsupported transport type" unless %w[unix].include?(transport) @@ -216,14 +168,17 @@ %i[ params form json xml body ssl http2_settings request_class response_class headers_class request_body_class response_body_class connection_class options_class - io fallback_protocol debug debug_level transport_options resolver_class resolver_options + io fallback_protocol debug debug_level resolver_class resolver_options + compress_request_body decompress_response_body persistent ].each do |method_name| - def_option(method_name) + class_eval(<<-OUT, __FILE__, __LINE__ + 1) + def option_#{method_name}(v); v; end # def option_smth(v); v; end + OUT end REQUEST_IVARS = %i[@params @form @xml @json @body].freeze private_constant :REQUEST_IVARS @@ -268,33 +223,18 @@ instance_variables.each_with_object({}) do |ivar, hs| hs[ivar[1..-1].to_sym] = instance_variable_get(ivar) end end - if RUBY_VERSION > "2.4.0" - def initialize_dup(other) - instance_variables.each do |ivar| - instance_variable_set(ivar, other.instance_variable_get(ivar).dup) - end + def initialize_dup(other) + instance_variables.each do |ivar| + instance_variable_set(ivar, other.instance_variable_get(ivar).dup) end - else - def initialize_dup(other) - instance_variables.each do |ivar| - value = other.instance_variable_get(ivar) - value = case value - when Symbol, Numeric, TrueClass, FalseClass - value - else - value.dup - end - instance_variable_set(ivar, value) - end - end end private - def __initialize__(options = {}) + def do_initialize(options = {}) defaults = DEFAULT_OPTIONS.merge(options) defaults.each do |k, v| next if v.nil? begin