added support for HTTP#with_ methods

these will reapply options accordingly (i.e. HTTPX.with_headers(...)).

Because we now have these, HTTPX.headers and HTTPX.timeout have been
deprecated.
This commit is contained in:
HoneyryderChuck 2020-03-09 23:13:04 +00:00
parent aebadc8bc9
commit 108d9257c8
13 changed files with 40 additions and 35 deletions

View File

@ -153,7 +153,7 @@ module Faraday
proxy_options = { uri: env.request.proxy }
session = @session.with(options_from_env(env))
session = session.plugin(:proxy).with_proxy(proxy_options) if env.request.proxy
session = session.plugin(:proxy).with(proxy: proxy_options) if env.request.proxy
responses = session.request(requests)
Array(responses).each_with_index do |response, index|
@ -192,7 +192,7 @@ module Faraday
meth, uri, request_options = build_request(env)
session = @session.with(options_from_env(env))
session = session.plugin(:proxy).with_proxy(proxy_options) if env.request.proxy
session = session.plugin(:proxy).with(proxy: proxy_options) if env.request.proxy
response = session.__send__(meth, uri, **request_options)
response.raise_for_status unless response.is_a?(::HTTPX::Response)
save_response(env, response.status, response.body.to_s, response.headers, response.reason) do |response_headers|

View File

@ -12,16 +12,20 @@ module HTTPX
branch(default_options).request(verb, uri, **options)
end
# :nocov:
def timeout(**args)
branch(default_options.with_timeout(args))
warn ":#{__method__} is deprecated, use :with_timeout instead"
branch(default_options.with(timeout: args))
end
def headers(headers)
branch(default_options.with_headers(headers))
warn ":#{__method__} is deprecated, use :with_headers instead"
branch(default_options.with(headers: headers))
end
# :nocov:
def accept(type)
headers("accept" => String(type))
with(headers: { "accept" => String(type) })
end
def wrap(&blk)
@ -59,5 +63,18 @@ module HTTPX
Session.new(options, &blk)
end
def method_missing(meth, *args, **options)
if meth =~ /\Awith_(.+)/
option = Regexp.last_match(1).to_sym
with(option => (args.first || options))
else
super
end
end
def respond_to_missing?(meth, *args)
default_options.respond_to?(meth, *args) || super
end
end
end

View File

@ -11,7 +11,7 @@ module HTTPX
module Authentication
module InstanceMethods
def authentication(token)
headers("authorization" => token)
with(headers: { "authorization" => token })
end
end
end

View File

@ -82,10 +82,6 @@ module HTTPX
super({ cookies: Store.new }.merge(options), &blk)
end
def with_cookies(cookies)
branch(default_options.with_cookies(cookies))
end
def wrap
return super unless block_given?
@ -94,7 +90,7 @@ module HTTPX
begin
yield session
ensure
@options = @options.with_cookies(old_cookies_store)
@options = @options.with(cookies: old_cookies_store)
end
end
end

View File

@ -79,10 +79,6 @@ module HTTPX
end
module InstanceMethods
def with_proxy(*args)
branch(default_options.with_proxy(*args))
end
private
def proxy_uris(uri, options)

View File

@ -19,10 +19,6 @@ module HTTPX
end
module InstanceMethods
def with_proxy(*args)
branch(default_options.with_proxy(*args))
end
private
def send_requests(*requests, options)

View File

@ -61,7 +61,7 @@ class SessionTest < Minitest::Test
# def test_session_timeout_connect_timeout
# sleep 2
# uri = build_uri("/", origin("127.0.0.1:#{CONNECT_TIMEOUT_PORT}"))
# session = HTTPX.timeout(connect_timeout: 0.5, operation_timeout: 30, total_timeout: 2)
# session = HTTPX.with_timeout(connect_timeout: 0.5, operation_timeout: 30, total_timeout: 2)
# response = session.get(uri)
# assert response.is_a?(HTTPX::ErrorResponse), "response should have failed (#{response.class})"
# assert response.error.is_a?(HTTPX::ConnectTimeoutError),
@ -71,7 +71,7 @@ class SessionTest < Minitest::Test
# def test_http_timeouts_operation_timeout
# uri = build_uri("/delay/2")
# session = HTTPX.timeout(operation_timeout: 1)
# session = HTTPX.with_timeout(operation_timeout: 1)
# response = session.get(uri)
# assert response.is_a?(HTTPX::ErrorResponse), "response should have failed"
# assert response.error =~ /timed out while waiting/, "response should have timed out"

View File

@ -9,7 +9,7 @@ module Requests
assert body.key?("headers"), "no headers"
assert body["headers"]["Accept"] == "*/*", "unexpected accept"
response = HTTPX.headers("accept" => "text/css").get(uri)
response = HTTPX.with_headers("accept" => "text/css").get(uri)
body = json_body(response)
verify_header(body["headers"], "Accept", "text/css")
end

View File

@ -20,7 +20,7 @@ module Requests
end
def test_plugin_digest_authentication
session = HTTPX.plugin(:digest_authentication).headers("cookie" => "fake=fake_value")
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
response = session.digest_authentication(user, pass).get(digest_auth_uri)
verify_status(response, 200)
body = json_body(response)
@ -30,7 +30,7 @@ module Requests
%w[SHA1 SHA2 SHA256 SHA384 SHA512 RMD160].each do |alg|
define_method "test_plugin_digest_authentication_#{alg}" do
session = HTTPX.plugin(:digest_authentication).headers("cookie" => "fake=fake_value")
session = HTTPX.plugin(:digest_authentication).with_headers("cookie" => "fake=fake_value")
response = session.digest_authentication(user, pass).get("#{digest_auth_uri}/#{alg}")
verify_status(response, 200)
body = json_body(response)

View File

@ -29,7 +29,7 @@ module Requests
def test_plugin_compression_gzip_post
session = HTTPX.plugin(:compression)
uri = build_uri("/post")
response = session.headers("content-encoding" => "gzip")
response = session.with_headers("content-encoding" => "gzip")
.post(uri, body: "a" * 8012)
verify_status(response, 200)
body = json_body(response)
@ -50,7 +50,7 @@ module Requests
def test_plugin_compression_deflate_post
session = HTTPX.plugin(:compression)
uri = build_uri("/post")
response = session.headers("content-encoding" => "deflate")
response = session.with_headers("content-encoding" => "deflate")
.post(uri, body: "a" * 8012)
verify_status(response, 200)
body = json_body(response)
@ -71,7 +71,7 @@ module Requests
def test_plugin_compression_brotli_post
session = HTTPX.plugin(:"compression/brotli")
uri = build_uri("/post")
response = session.headers("content-encoding" => "br")
response = session.with_headers("content-encoding" => "br")
.post(uri, body: "a" * 8012)
verify_status(response, 200)
body = json_body(response)

View File

@ -4,14 +4,14 @@ module Requests
module Plugins
module Retries
def test_plugin_retries
no_retries_session = HTTPX.plugin(RequestInspector).timeout(total_timeout: 3)
no_retries_session = HTTPX.plugin(RequestInspector).with_timeout(total_timeout: 3)
no_retries_response = no_retries_session.get(build_uri("/delay/10"))
assert no_retries_response.is_a?(HTTPX::ErrorResponse)
assert no_retries_session.calls.zero?, "expect request to be built 1 times (was #{no_retries_session.calls})"
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries)
.timeout(total_timeout: 3)
.with_timeout(total_timeout: 3)
retries_response = retries_session.get(build_uri("/delay/10"))
assert retries_response.is_a?(HTTPX::ErrorResponse)
assert retries_session.calls == 3, "expect request to be built 4 times (was #{retries_session.calls})"
@ -21,7 +21,7 @@ module Requests
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries)
.timeout(total_timeout: 3)
.with_timeout(total_timeout: 3)
.max_retries(2)
retries_response = retries_session.get(build_uri("/delay/10"))
assert retries_response.is_a?(HTTPX::ErrorResponse)
@ -38,7 +38,7 @@ module Requests
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries, retry_on: retry_callback)
.timeout(total_timeout: 3)
.with_timeout(total_timeout: 3)
.max_retries(2)
retries_response = retries_session.get(build_uri("/delay/10"))
@ -51,7 +51,7 @@ module Requests
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries, retry_after: 2)
.timeout(total_timeout: 3)
.with(timeout: { total_timeout: 3 })
.max_retries(1)
retries_response = retries_session.get(build_uri("/delay/10"))
after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)
@ -68,7 +68,7 @@ module Requests
retries_session = HTTPX
.plugin(RequestInspector)
.plugin(:retries, retry_after: exponential)
.timeout(total_timeout: 3)
.with_timeout(total_timeout: 3)
.max_retries(2)
retries_response = retries_session.get(build_uri("/delay/10"))
after_time = Process.clock_gettime(Process::CLOCK_MONOTONIC, :second)

View File

@ -23,7 +23,7 @@ module Requests
define_method :"test_#{meth}_expect_100_form_params" do
uri = build_uri("/#{meth}")
response = HTTPX.headers("expect" => "100-continue")
response = HTTPX.with_headers("expect" => "100-continue")
.send(meth, uri, form: { "foo" => "bar" })
verify_status(response, 200)
body = json_body(response)

View File

@ -5,7 +5,7 @@ module Requests
%w[post put patch delete].each do |meth|
define_method :"test_#{meth}_chunked_body_params" do
uri = build_uri("/#{meth}")
response = HTTPX.headers("transfer-encoding" => "chunked")
response = HTTPX.with_headers("transfer-encoding" => "chunked")
.send(meth, uri, body: %w[this is a chunked response])
verify_status(response, 200)
body = json_body(response)