lib/httpx/plugins/stream.rb in httpx-1.1.3 vs lib/httpx/plugins/stream.rb in httpx-1.1.4
- old
+ new
@@ -1,36 +1,25 @@
# frozen_string_literal: true
module HTTPX
class StreamResponse
- def initialize(request, session, connections)
+ def initialize(request, session)
@request = request
@session = session
- @connections = connections
end
def each(&block)
return enum_for(__method__) unless block
@request.stream = self
begin
@on_chunk = block
- if @request.response
- # if we've already started collecting the payload, yield it first
- # before proceeding
- body = @request.response.body
-
- body.each do |chunk|
- on_chunk(chunk)
- end
- end
-
response.raise_for_status
- response.close
ensure
+ response.close if @response
@on_chunk = nil
end
end
def each_line
@@ -67,13 +56,13 @@
end
private
def response
- @session.__send__(:receive_requests, [@request], @connections) until @request.response
-
- @request.response
+ @response ||= begin
+ @request.response || @session.request(@request)
+ end
end
def respond_to_missing?(meth, *args)
response.respond_to?(meth, *args) || super
end
@@ -103,12 +92,10 @@
requests = args.first.is_a?(Request) ? args : build_requests(*args, options)
raise Error, "only 1 response at a time is supported for streaming requests" unless requests.size == 1
request = requests.first
- connections = _send_requests(requests)
-
- StreamResponse.new(request, self, connections)
+ StreamResponse.new(request, self)
end
end
module RequestMethods
attr_accessor :stream