lib/httpx/options.rb in httpx-0.13.2 vs lib/httpx/options.rb in httpx-0.14.0

- old
+ new

@@ -2,24 +2,68 @@ module HTTPX class Options WINDOW_SIZE = 1 << 14 # 16K MAX_BODY_THRESHOLD_SIZE = (1 << 10) * 112 # 112K + CONNECT_TIMEOUT = 60 + OPERATION_TIMEOUT = 60 + KEEP_ALIVE_TIMEOUT = 20 + DEFAULT_OPTIONS = { + :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", + :timeout => { + connect_timeout: CONNECT_TIMEOUT, + operation_timeout: OPERATION_TIMEOUT, + keep_alive_timeout: KEEP_ALIVE_TIMEOUT, + }, + :headers => {}, + :window_size => WINDOW_SIZE, + :body_threshold_size => MAX_BODY_THRESHOLD_SIZE, + :request_class => Class.new(Request), + :response_class => Class.new(Response), + :headers_class => Class.new(Headers), + :request_body_class => Class.new(Request::Body), + :response_body_class => Class.new(Response::Body), + :connection_class => Class.new(Connection), + :transport => nil, + :transport_options => nil, + :addresses => nil, + :persistent => false, + :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, + :resolver_options => { cache: true }, + }.freeze + 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) super end - def def_option(name, &interpreter) + def def_option(name, layout = nil, &interpreter) attr_reader name - if interpreter + if layout + class_eval(<<-OUT, __FILE__, __LINE__ + 1) + def #{name}=(value) + return if value.nil? + + value = begin + #{layout} + end + + @#{name} = value + end + OUT + + elsif interpreter define_method(:"#{name}=") do |value| return if value.nil? instance_variable_set(:"@#{name}", instance_exec(value, &interpreter)) end @@ -30,83 +74,74 @@ protected :"#{name}=" end end def initialize(options = {}) - defaults = { - :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", - :timeout => Timeout.new, - :headers => {}, - :window_size => WINDOW_SIZE, - :body_threshold_size => MAX_BODY_THRESHOLD_SIZE, - :request_class => Class.new(Request), - :response_class => Class.new(Response), - :headers_class => Class.new(Headers), - :request_body_class => Class.new(Request::Body), - :response_body_class => Class.new(Response::Body), - :connection_class => Class.new(Connection), - :transport => nil, - :transport_options => nil, - :addresses => nil, - :persistent => false, - :resolver_class => (ENV["HTTPX_RESOLVER"] || :native).to_sym, - :resolver_options => { cache: true }, - } - - defaults.merge!(options) + defaults = DEFAULT_OPTIONS.merge(options) defaults.each do |(k, v)| next if v.nil? - __send__(:"#{k}=", v) + begin + __send__(:"#{k}=", v) + rescue NoMethodError + raise Error, "unknown option: #{k}" + end end end - def_option(:headers) do |headers| + def_option(:origin, <<-OUT) + URI(value) + OUT + + def_option(:headers, <<-OUT) if self.headers - self.headers.merge(headers) + self.headers.merge(value) else - Headers.new(headers) + Headers.new(value) end - end + OUT - def_option(:timeout) do |opts| - Timeout.new(opts) - end + def_option(:timeout, <<-OUT) + timeouts = Hash[value] - def_option(:max_concurrent_requests) do |num| - raise Error, ":max_concurrent_requests must be positive" unless num.positive? + if timeouts.key?(:loop_timeout) + warn ":loop_timeout is deprecated, use :operation_timeout instead" + timeouts[:operation_timeout] = timeouts.delete(:loop_timeout) + end - num - end + timeouts + OUT - def_option(:max_requests) do |num| - raise Error, ":max_requests must be positive" unless num.positive? + def_option(:max_concurrent_requests, <<-OUT) + raise Error, ":max_concurrent_requests must be positive" unless value.positive? - num - end + value + OUT - def_option(:window_size) do |num| - Integer(num) - end + def_option(:max_requests, <<-OUT) + raise Error, ":max_requests must be positive" unless value.positive? - def_option(:body_threshold_size) do |num| - Integer(num) - end + value + OUT - def_option(:transport) do |tr| - transport = tr.to_s - raise Error, "#{transport} is an unsupported transport type" unless IO.registry.key?(transport) + def_option(:window_size, <<-OUT) + Integer(value) + OUT + def_option(:body_threshold_size, <<-OUT) + Integer(value) + OUT + + def_option(:transport, <<-OUT) + transport = value.to_s + raise Error, "\#{transport} is an unsupported transport type" unless IO.registry.key?(transport) + transport - end + OUT - def_option(:addresses) do |addrs| - Array(addrs) - end + def_option(:addresses, <<-OUT) + Array(value) + OUT %w[ params form json body ssl http2_settings request_class response_class headers_class request_body_class response_body_class connection_class io fallback_protocol debug debug_level transport_options resolver_class resolver_options