mirror of
https://github.com/HoneyryderChuck/httpx.git
synced 2025-12-08 00:00:58 -05:00
move session callbacks to plugin
This commit is contained in:
parent
7878595460
commit
e29a91e7f7
@ -10,19 +10,6 @@ module HTTPX
|
|||||||
MOD
|
MOD
|
||||||
end
|
end
|
||||||
|
|
||||||
%i[
|
|
||||||
connection_opened connection_closed
|
|
||||||
request_error
|
|
||||||
request_started request_body_chunk request_completed
|
|
||||||
response_started response_body_chunk response_completed
|
|
||||||
].each do |meth|
|
|
||||||
class_eval(<<-MOD, __FILE__, __LINE__ + 1)
|
|
||||||
def on_#{meth}(&blk) # def on_connection_opened(&blk)
|
|
||||||
on(:#{meth}, &blk) # on(:connection_opened, &blk)
|
|
||||||
end # end
|
|
||||||
MOD
|
|
||||||
end
|
|
||||||
|
|
||||||
def request(*args, **options)
|
def request(*args, **options)
|
||||||
branch(default_options).request(*args, **options)
|
branch(default_options).request(*args, **options)
|
||||||
end
|
end
|
||||||
@ -46,12 +33,6 @@ module HTTPX
|
|||||||
branch(default_options.merge(options), &blk)
|
branch(default_options.merge(options), &blk)
|
||||||
end
|
end
|
||||||
|
|
||||||
protected
|
|
||||||
|
|
||||||
def on(*args, &blk)
|
|
||||||
branch(default_options).on(*args, &blk)
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def default_options
|
def default_options
|
||||||
|
|||||||
67
lib/httpx/plugins/callbacks.rb
Normal file
67
lib/httpx/plugins/callbacks.rb
Normal file
@ -0,0 +1,67 @@
|
|||||||
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
module HTTPX
|
||||||
|
module Plugins
|
||||||
|
module Callbacks
|
||||||
|
module InstanceMethods
|
||||||
|
include HTTPX::Callbacks
|
||||||
|
|
||||||
|
%i[
|
||||||
|
connection_opened connection_closed
|
||||||
|
request_error
|
||||||
|
request_started request_body_chunk request_completed
|
||||||
|
response_started response_body_chunk response_completed
|
||||||
|
].each do |meth|
|
||||||
|
class_eval(<<-MOD, __FILE__, __LINE__ + 1)
|
||||||
|
def on_#{meth}(&blk) # def on_connection_opened(&blk)
|
||||||
|
on(:#{meth}, &blk) # on(:connection_opened, &blk)
|
||||||
|
end # end
|
||||||
|
MOD
|
||||||
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def init_connection(type, uri, options)
|
||||||
|
connection = super
|
||||||
|
connection.on(:open) do
|
||||||
|
emit(:connection_opened, connection.origin, connection.io.socket)
|
||||||
|
end
|
||||||
|
connection.on(:close) do
|
||||||
|
emit(:connection_closed, connection.origin) if connection.used?
|
||||||
|
end
|
||||||
|
|
||||||
|
connection
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_request_callbacks(request)
|
||||||
|
super
|
||||||
|
|
||||||
|
request.on(:headers) do
|
||||||
|
emit(:request_started, request)
|
||||||
|
end
|
||||||
|
request.on(:body_chunk) do |chunk|
|
||||||
|
emit(:request_body_chunk, request, chunk)
|
||||||
|
end
|
||||||
|
request.on(:done) do
|
||||||
|
emit(:request_completed, request)
|
||||||
|
end
|
||||||
|
|
||||||
|
request.on(:response_started) do |res|
|
||||||
|
if res.is_a?(Response)
|
||||||
|
emit(:response_started, request, res)
|
||||||
|
res.on(:chunk_received) do |chunk|
|
||||||
|
emit(:response_body_chunk, request, res, chunk)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
emit(:request_error, request, res.error)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
request.on(:response) do |res|
|
||||||
|
emit(:response_completed, request, res)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
register_plugin :callbacks, Callbacks
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -85,7 +85,7 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
|
|
||||||
class SocksParser
|
class SocksParser
|
||||||
include Callbacks
|
include HTTPX::Callbacks
|
||||||
|
|
||||||
def initialize(buffer, options)
|
def initialize(buffer, options)
|
||||||
@buffer = buffer
|
@buffer = buffer
|
||||||
|
|||||||
@ -137,7 +137,7 @@ module HTTPX
|
|||||||
end
|
end
|
||||||
|
|
||||||
class SocksParser
|
class SocksParser
|
||||||
include Callbacks
|
include HTTPX::Callbacks
|
||||||
|
|
||||||
def initialize(buffer, options)
|
def initialize(buffer, options)
|
||||||
@buffer = buffer
|
@buffer = buffer
|
||||||
|
|||||||
@ -8,7 +8,6 @@ module HTTPX
|
|||||||
class Session
|
class Session
|
||||||
include Loggable
|
include Loggable
|
||||||
include Chainable
|
include Chainable
|
||||||
include Callbacks
|
|
||||||
|
|
||||||
EMPTY_HASH = {}.freeze
|
EMPTY_HASH = {}.freeze
|
||||||
|
|
||||||
@ -85,33 +84,7 @@ module HTTPX
|
|||||||
options = @options.merge(options) unless options.is_a?(Options)
|
options = @options.merge(options) unless options.is_a?(Options)
|
||||||
request = rklass.new(verb, uri, options)
|
request = rklass.new(verb, uri, options)
|
||||||
request.persistent = @persistent
|
request.persistent = @persistent
|
||||||
request.on(:response, &method(:on_response).curry(2)[request])
|
set_request_callbacks(request)
|
||||||
request.on(:promise, &method(:on_promise))
|
|
||||||
|
|
||||||
request.on(:headers) do
|
|
||||||
emit(:request_started, request)
|
|
||||||
end
|
|
||||||
request.on(:body_chunk) do |chunk|
|
|
||||||
emit(:request_body_chunk, request, chunk)
|
|
||||||
end
|
|
||||||
request.on(:done) do
|
|
||||||
emit(:request_completed, request)
|
|
||||||
end
|
|
||||||
|
|
||||||
request.on(:response_started) do |res|
|
|
||||||
if res.is_a?(Response)
|
|
||||||
emit(:response_started, request, res)
|
|
||||||
res.on(:chunk_received) do |chunk|
|
|
||||||
emit(:response_body_chunk, request, res, chunk)
|
|
||||||
end
|
|
||||||
else
|
|
||||||
emit(:request_error, request, res.error)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
request.on(:response) do |res|
|
|
||||||
emit(:response_completed, request, res)
|
|
||||||
end
|
|
||||||
|
|
||||||
request
|
request
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -236,6 +209,11 @@ module HTTPX
|
|||||||
requests
|
requests
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def set_request_callbacks(request)
|
||||||
|
request.on(:response, &method(:on_response).curry(2)[request])
|
||||||
|
request.on(:promise, &method(:on_promise))
|
||||||
|
end
|
||||||
|
|
||||||
# returns a new HTTPX::Connection object for the given +uri+ and set of +options+.
|
# returns a new HTTPX::Connection object for the given +uri+ and set of +options+.
|
||||||
def build_connection(uri, options)
|
def build_connection(uri, options)
|
||||||
type = options.transport || begin
|
type = options.transport || begin
|
||||||
@ -253,13 +231,6 @@ module HTTPX
|
|||||||
|
|
||||||
def init_connection(type, uri, options)
|
def init_connection(type, uri, options)
|
||||||
connection = options.connection_class.new(type, uri, options)
|
connection = options.connection_class.new(type, uri, options)
|
||||||
connection.on(:open) do
|
|
||||||
emit(:connection_opened, connection.origin, connection.io.socket)
|
|
||||||
# only run close callback if it opened
|
|
||||||
end
|
|
||||||
connection.on(:close) do
|
|
||||||
emit(:connection_closed, connection.origin, connection.io.socket) if connection.used?
|
|
||||||
end
|
|
||||||
catch(:coalesced) do
|
catch(:coalesced) do
|
||||||
pool.init_connection(connection, options)
|
pool.init_connection(connection, options)
|
||||||
connection
|
connection
|
||||||
|
|||||||
@ -34,6 +34,7 @@ module HTTPX
|
|||||||
| (:response_cache, ?options) -> Plugins::sessionResponseCache
|
| (:response_cache, ?options) -> Plugins::sessionResponseCache
|
||||||
| (:circuit_breaker, ?options) -> Plugins::sessionCircuitBreaker
|
| (:circuit_breaker, ?options) -> Plugins::sessionCircuitBreaker
|
||||||
| (:oauth, ?options) -> Plugins::sessionOAuth
|
| (:oauth, ?options) -> Plugins::sessionOAuth
|
||||||
|
| (:callbacks, ?options) -> Plugins::sessionCallbacks
|
||||||
| (Symbol | Module, ?options) { (Class) -> void } -> Session
|
| (Symbol | Module, ?options) { (Class) -> void } -> Session
|
||||||
| (Symbol | Module, ?options) -> Session
|
| (Symbol | Module, ?options) -> Session
|
||||||
|
|
||||||
|
|||||||
31
sig/plugins/callbacks.rbs
Normal file
31
sig/plugins/callbacks.rbs
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
module HTTPX
|
||||||
|
module Plugins
|
||||||
|
module Callbacks
|
||||||
|
module InstanceMethods
|
||||||
|
include HTTPX::Callbacks
|
||||||
|
|
||||||
|
type socket = TCPSocket | SSLSocket | UNIXSocket
|
||||||
|
|
||||||
|
def on_connection_opened: () { (http_uri origin, socket sock) -> void } -> self
|
||||||
|
|
||||||
|
def on_connection_opened: () { (http_uri origin) -> void } -> self
|
||||||
|
|
||||||
|
def on_request_error: () { (Request request, StandardError error) -> void } -> self
|
||||||
|
|
||||||
|
def on_request_started: () { (Request request) -> void } -> self
|
||||||
|
|
||||||
|
def on_request_body_chunk: () { (Request request, String chunk) -> void } -> self
|
||||||
|
|
||||||
|
def on_request_completed: () { (Request request) -> void } -> self
|
||||||
|
|
||||||
|
def on_response_started: () { (Request request, Response response) -> void } -> self
|
||||||
|
|
||||||
|
def on_response_body_chunk: () { (Request request, Response response, String chunk) -> void } -> self
|
||||||
|
|
||||||
|
def on_response_completed: () { (Request request, response response) -> void } -> self
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
type sessionCallbacks = Session & Callbacks::InstanceMethods
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -33,6 +33,8 @@ module HTTPX
|
|||||||
|
|
||||||
def set_connection_callbacks: (Connection connection, Array[Connection] connections, Options options) -> void
|
def set_connection_callbacks: (Connection connection, Array[Connection] connections, Options options) -> void
|
||||||
|
|
||||||
|
def set_request_callbacks: (Request request) -> void
|
||||||
|
|
||||||
def build_altsvc_connection: (Connection existing_connection, Array[Connection] connections, URI::Generic alt_origin, String origin, Hash[String, String] alt_params, Options options) -> (Connection & AltSvc::ConnectionMixin)?
|
def build_altsvc_connection: (Connection existing_connection, Array[Connection] connections, URI::Generic alt_origin, String origin, Hash[String, String] alt_params, Options options) -> (Connection & AltSvc::ConnectionMixin)?
|
||||||
|
|
||||||
def build_requests: (verb, uri, options) -> Array[Request]
|
def build_requests: (verb, uri, options) -> Array[Request]
|
||||||
|
|||||||
@ -10,7 +10,7 @@ module Requests
|
|||||||
origin = ip = nil
|
origin = ip = nil
|
||||||
opened = 0
|
opened = 0
|
||||||
|
|
||||||
response = HTTPX.plugin(SessionWithPool).on_connection_opened do |o, sock|
|
response = HTTPX.plugin(SessionWithPool).plugin(:callbacks).on_connection_opened do |o, sock|
|
||||||
origin = o
|
origin = o
|
||||||
ip = sock.to_io.remote_address.ip_address
|
ip = sock.to_io.remote_address.ip_address
|
||||||
opened += 1
|
opened += 1
|
||||||
@ -30,7 +30,7 @@ module Requests
|
|||||||
origin = nil
|
origin = nil
|
||||||
closed = 0
|
closed = 0
|
||||||
|
|
||||||
response = HTTPX.plugin(SessionWithPool).on_connection_closed do |o|
|
response = HTTPX.plugin(SessionWithPool).plugin(:callbacks).on_connection_closed do |o|
|
||||||
origin = o
|
origin = o
|
||||||
closed += 1
|
closed += 1
|
||||||
end.get(uri)
|
end.get(uri)
|
||||||
@ -45,7 +45,7 @@ module Requests
|
|||||||
uri = URI(build_uri("/get"))
|
uri = URI(build_uri("/get"))
|
||||||
error = nil
|
error = nil
|
||||||
|
|
||||||
http = HTTPX.on_request_error { |_, err| error = err }
|
http = HTTPX.plugin(:callbacks).on_request_error { |_, err| error = err }
|
||||||
|
|
||||||
response = http.get(uri)
|
response = http.get(uri)
|
||||||
verify_status(response, 200)
|
verify_status(response, 200)
|
||||||
@ -66,7 +66,8 @@ module Requests
|
|||||||
started = completed = false
|
started = completed = false
|
||||||
chunks = 0
|
chunks = 0
|
||||||
|
|
||||||
http = HTTPX.on_request_started { |_| started = true }
|
http = HTTPX.plugin(:callbacks)
|
||||||
|
.on_request_started { |_| started = true }
|
||||||
.on_request_body_chunk { |_, _chunk| chunks += 1 }
|
.on_request_body_chunk { |_, _chunk| chunks += 1 }
|
||||||
.on_request_completed { |_| completed = true }
|
.on_request_completed { |_| completed = true }
|
||||||
|
|
||||||
@ -83,7 +84,8 @@ module Requests
|
|||||||
started = completed = false
|
started = completed = false
|
||||||
chunks = 0
|
chunks = 0
|
||||||
|
|
||||||
http = HTTPX.on_response_started { |_, _| started = true }
|
http = HTTPX.plugin(:callbacks)
|
||||||
|
.on_response_started { |_, _| started = true }
|
||||||
.on_response_body_chunk { |_, _, _chunk| chunks += 1 }
|
.on_response_body_chunk { |_, _, _chunk| chunks += 1 }
|
||||||
.on_response_completed { |_, _| completed = true }
|
.on_response_completed { |_, _| completed = true }
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user