mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-08-10 00:01:27 -04:00
using the new OptionsMethods way for defining internal plugin options
This commit is contained in:
parent
5c2ad6b588
commit
5604501d01
@ -141,14 +141,6 @@ module HTTPX
|
||||
end
|
||||
|
||||
class << self
|
||||
def extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:sigv4_signer, <<-OUT)
|
||||
value.is_a?(#{Signer}) ? value : #{Signer}.new(value)
|
||||
OUT
|
||||
end.new(options)
|
||||
end
|
||||
|
||||
def load_dependencies(klass)
|
||||
require "digest/sha2"
|
||||
require "openssl"
|
||||
@ -157,6 +149,12 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
def option_sigv4_signer(value)
|
||||
value.is_a?(Signer) ? value : Signer.new(value)
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
def aws_sigv4_authentication(**options)
|
||||
with(sigv4_signer: Signer.new(**options))
|
||||
|
@ -23,21 +23,22 @@ module HTTPX
|
||||
encodings = Module.new do
|
||||
extend Registry
|
||||
end
|
||||
options.merge(encodings: encodings)
|
||||
end
|
||||
end
|
||||
|
||||
Class.new(options.class) do
|
||||
def_option(:compression_threshold_size, <<-OUT)
|
||||
bytes = Integer(value)
|
||||
raise TypeError, ":expect_threshold_size must be positive" unless bytes.positive?
|
||||
module OptionsMethods
|
||||
def option_compression_threshold_size(value)
|
||||
bytes = Integer(value)
|
||||
raise TypeError, ":expect_threshold_size must be positive" unless bytes.positive?
|
||||
|
||||
bytes
|
||||
OUT
|
||||
bytes
|
||||
end
|
||||
|
||||
def_option(:encodings, <<-OUT)
|
||||
raise TypeError, ":encodings must be a registry" unless value.respond_to?(:registry)
|
||||
def option_encodings(value)
|
||||
raise TypeError, ":encodings must be a registry" unless value.respond_to?(:registry)
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(encodings: encodings)
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -18,12 +18,10 @@ module HTTPX
|
||||
require "httpx/plugins/cookies/set_cookie_parser"
|
||||
end
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:cookies, <<-OUT)
|
||||
value.is_a?(#{Jar}) ? value : #{Jar}.new(value)
|
||||
OUT
|
||||
end.new(options)
|
||||
module OptionsMethods
|
||||
def option_cookies(value)
|
||||
value.is_a?(Jar) ? value : Jar.new(value)
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -14,19 +14,23 @@ module HTTPX
|
||||
|
||||
DigestError = Class.new(Error)
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:digest, <<-OUT)
|
||||
raise TypeError, ":digest must be a Digest" unless value.is_a?(#{Digest})
|
||||
class << self
|
||||
def extra_options(options)
|
||||
options.merge(max_concurrent_requests: 1)
|
||||
end
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(max_concurrent_requests: 1)
|
||||
def load_dependencies(*)
|
||||
require "securerandom"
|
||||
require "digest"
|
||||
end
|
||||
end
|
||||
|
||||
def self.load_dependencies(*)
|
||||
require "securerandom"
|
||||
require "digest"
|
||||
module OptionsMethods
|
||||
def option_digest(value)
|
||||
raise TypeError, ":digest must be a Digest" unless value.is_a?(Digest)
|
||||
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -10,26 +10,30 @@ module HTTPX
|
||||
module Expect
|
||||
EXPECT_TIMEOUT = 2
|
||||
|
||||
def self.no_expect_store
|
||||
@no_expect_store ||= []
|
||||
class << self
|
||||
def no_expect_store
|
||||
@no_expect_store ||= []
|
||||
end
|
||||
|
||||
def extra_options(options)
|
||||
options.merge(expect_timeout: EXPECT_TIMEOUT)
|
||||
end
|
||||
end
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:expect_timeout, <<-OUT)
|
||||
seconds = Integer(value)
|
||||
raise TypeError, ":expect_timeout must be positive" unless seconds.positive?
|
||||
module OptionsMethods
|
||||
def option_expect_timeout(value)
|
||||
seconds = Integer(value)
|
||||
raise TypeError, ":expect_timeout must be positive" unless seconds.positive?
|
||||
|
||||
seconds
|
||||
OUT
|
||||
seconds
|
||||
end
|
||||
|
||||
def_option(:expect_threshold_size, <<-OUT)
|
||||
bytes = Integer(value)
|
||||
raise TypeError, ":expect_threshold_size must be positive" unless bytes.positive?
|
||||
def option_expect_threshold_size(value)
|
||||
bytes = Integer(value)
|
||||
raise TypeError, ":expect_threshold_size must be positive" unless bytes.positive?
|
||||
|
||||
bytes
|
||||
OUT
|
||||
end.new(options).merge(expect_timeout: EXPECT_TIMEOUT)
|
||||
bytes
|
||||
end
|
||||
end
|
||||
|
||||
module RequestMethods
|
||||
|
@ -17,17 +17,17 @@ module HTTPX
|
||||
MAX_REDIRECTS = 3
|
||||
REDIRECT_STATUS = (300..399).freeze
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:max_redirects, <<-OUT)
|
||||
num = Integer(value)
|
||||
raise TypeError, ":max_redirects must be positive" if num.negative?
|
||||
module OptionsMethods
|
||||
def option_max_redirects(value)
|
||||
num = Integer(value)
|
||||
raise TypeError, ":max_redirects must be positive" if num.negative?
|
||||
|
||||
num
|
||||
OUT
|
||||
num
|
||||
end
|
||||
|
||||
def_option(:follow_insecure_redirects)
|
||||
end.new(options)
|
||||
def option_follow_insecure_redirects(value)
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -61,36 +61,7 @@ module HTTPX
|
||||
end
|
||||
|
||||
def extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:grpc_service, <<-OUT)
|
||||
String(value)
|
||||
OUT
|
||||
|
||||
def_option(:grpc_compression, <<-OUT)
|
||||
case value
|
||||
when true, false
|
||||
value
|
||||
else
|
||||
value.to_s
|
||||
end
|
||||
OUT
|
||||
|
||||
def_option(:grpc_rpcs, <<-OUT)
|
||||
Hash[value]
|
||||
OUT
|
||||
|
||||
def_option(:grpc_deadline, <<-OUT)
|
||||
raise TypeError, ":grpc_deadline must be positive" unless value.positive?
|
||||
|
||||
value
|
||||
OUT
|
||||
|
||||
def_option(:call_credentials, <<-OUT)
|
||||
raise TypeError, ":call_credentials must respond to #call" unless value.respond_to?(:call)
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(
|
||||
options.merge(
|
||||
fallback_protocol: "h2",
|
||||
http2_settings: { wait_for_handshake: false },
|
||||
grpc_rpcs: {}.freeze,
|
||||
@ -100,6 +71,37 @@ module HTTPX
|
||||
end
|
||||
end
|
||||
|
||||
module OptionsMethods
|
||||
def option_grpc_service(value)
|
||||
String(value)
|
||||
end
|
||||
|
||||
def option_grpc_compression(value)
|
||||
case value
|
||||
when true, false
|
||||
value
|
||||
else
|
||||
value.to_s
|
||||
end
|
||||
end
|
||||
|
||||
def option_grpc_rpcs(value)
|
||||
Hash[value]
|
||||
end
|
||||
|
||||
def option_grpc_deadline(value)
|
||||
raise TypeError, ":grpc_deadline must be positive" unless value.positive?
|
||||
|
||||
value
|
||||
end
|
||||
|
||||
def option_call_credentials(value)
|
||||
raise TypeError, ":call_credentials must respond to #call" unless value.respond_to?(:call)
|
||||
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
module ResponseMethods
|
||||
attr_reader :trailing_metadata
|
||||
|
||||
|
@ -15,13 +15,15 @@ module HTTPX
|
||||
end
|
||||
|
||||
def extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:ntlm, <<-OUT)
|
||||
raise TypeError, ":ntlm must be a #{NTLMParams}" unless value.is_a?(#{NTLMParams})
|
||||
options.merge(max_concurrent_requests: 1)
|
||||
end
|
||||
end
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(max_concurrent_requests: 1)
|
||||
module OptionsMethods
|
||||
def option_ntlm(value)
|
||||
raise TypeError, ":ntlm must be a #{NTLMParams}" unless value.is_a?(NTLMParams)
|
||||
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -64,13 +64,11 @@ module HTTPX
|
||||
klass.plugin(:"proxy/socks4")
|
||||
klass.plugin(:"proxy/socks5")
|
||||
end
|
||||
end
|
||||
|
||||
def extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:proxy, <<-OUT)
|
||||
value.is_a?(#{Parameters}) ? value : Hash[value]
|
||||
OUT
|
||||
end.new(options)
|
||||
module OptionsMethods
|
||||
def option_proxy(value)
|
||||
value.is_a?(Parameters) ? value : Hash[value]
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -6,16 +6,16 @@ module HTTPX
|
||||
module Plugins
|
||||
module Proxy
|
||||
module SSH
|
||||
def self.load_dependencies(*)
|
||||
require "net/ssh/gateway"
|
||||
class << self
|
||||
def load_dependencies(*)
|
||||
require "net/ssh/gateway"
|
||||
end
|
||||
end
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:proxy, <<-OUT)
|
||||
Hash[value]
|
||||
OUT
|
||||
end.new(options)
|
||||
module OptionsMethods
|
||||
def option_proxy(value)
|
||||
Hash[value]
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -24,32 +24,36 @@ module HTTPX
|
||||
Errno::ETIMEDOUT].freeze
|
||||
|
||||
def self.extra_options(options)
|
||||
Class.new(options.class) do
|
||||
def_option(:retry_after, <<-OUT)
|
||||
# return early if callable
|
||||
unless value.respond_to?(:call)
|
||||
value = Integer(value)
|
||||
raise TypeError, ":retry_after must be positive" unless value.positive?
|
||||
end
|
||||
options.merge(max_retries: MAX_RETRIES)
|
||||
end
|
||||
|
||||
value
|
||||
OUT
|
||||
module OptionsMethods
|
||||
def option_retry_after(value)
|
||||
# return early if callable
|
||||
unless value.respond_to?(:call)
|
||||
value = Integer(value)
|
||||
raise TypeError, ":retry_after must be positive" unless value.positive?
|
||||
end
|
||||
|
||||
def_option(:max_retries, <<-OUT)
|
||||
num = Integer(value)
|
||||
raise TypeError, ":max_retries must be positive" unless num.positive?
|
||||
value
|
||||
end
|
||||
|
||||
num
|
||||
OUT
|
||||
def option_max_retries(value)
|
||||
num = Integer(value)
|
||||
raise TypeError, ":max_retries must be positive" unless num.positive?
|
||||
|
||||
def_option(:retry_change_requests)
|
||||
num
|
||||
end
|
||||
|
||||
def_option(:retry_on, <<-OUT)
|
||||
raise ":retry_on must be called with the response" unless value.respond_to?(:call)
|
||||
def option_retry_change_requests(v)
|
||||
v
|
||||
end
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(max_retries: MAX_RETRIES)
|
||||
def option_retry_on(value)
|
||||
raise ":retry_on must be called with the response" unless value.respond_to?(:call)
|
||||
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
module InstanceMethods
|
||||
|
@ -18,14 +18,15 @@ module HTTPX
|
||||
upgrade_handlers = Module.new do
|
||||
extend Registry
|
||||
end
|
||||
options.merge(upgrade_handlers: upgrade_handlers)
|
||||
end
|
||||
end
|
||||
|
||||
Class.new(options.class) do
|
||||
def_option(:upgrade_handlers, <<-OUT)
|
||||
raise TypeError, ":upgrade_handlers must be a registry" unless value.respond_to?(:registry)
|
||||
module OptionsMethods
|
||||
def option_upgrade_handlers(value)
|
||||
raise TypeError, ":upgrade_handlers must be a registry" unless value.respond_to?(:registry)
|
||||
|
||||
value
|
||||
OUT
|
||||
end.new(options).merge(upgrade_handlers: upgrade_handlers)
|
||||
value
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -4,7 +4,7 @@ module HTTPX
|
||||
MAX_RETRIES: Integer
|
||||
IDEMPOTENT_METHODS: Array[verb]
|
||||
RETRYABLE_ERRORS: Array[singleton(StandardError)]
|
||||
|
||||
|
||||
interface _RetryCallback
|
||||
def call: (response) -> bool?
|
||||
end
|
||||
@ -16,13 +16,13 @@ module HTTPX
|
||||
def max_retries: () -> Integer?
|
||||
def max_retries=: (int) -> Integer
|
||||
|
||||
def retry_change_requests: () -> bool?
|
||||
def retry_change_requests=: (bool) -> bool
|
||||
def retry_change_requests: () -> boolish
|
||||
def retry_change_requests=: (boolish) -> boolish
|
||||
|
||||
def retry_on: () -> _RetryCallback?
|
||||
def retry_on=: (_RetryCallback) -> _RetryCallback
|
||||
end
|
||||
|
||||
|
||||
def self.extra_options: (Options) -> (Options & _RetriesOptions)
|
||||
|
||||
module InstanceMethods
|
||||
|
Loading…
x
Reference in New Issue
Block a user