Skip to content

Commit

Permalink
feat: Adding support for streamed responses (#6)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikeRogers0 committed Jul 12, 2021
1 parent f7408a8 commit 92a676a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 2 deletions.
21 changes: 20 additions & 1 deletion lib/faraday/adapter/net_http_persistent.rb
Expand Up @@ -50,7 +50,26 @@ def proxy_uri(env)
end

def perform_request(http, env)
http.request env[:url], create_request(env)
if env[:request].stream_response?
size = 0
yielded = false

http_response = http.request(env[:url], create_request(env)) do |response|
response.read_body do |chunk|
if chunk.bytesize.positive? || size.positive?
yielded = true
size += chunk.bytesize
env[:request].on_data.call(chunk, size)
end
end
end

env[:request].on_data.call(+"", 0) unless yielded
http_response.body = nil
http_response
else
http.request(env[:url], create_request(env))
end
rescue Errno::ETIMEDOUT, Net::OpenTimeout => e
raise Faraday::TimeoutError, e
rescue Net::HTTP::Persistent::Error => e
Expand Down
2 changes: 1 addition & 1 deletion spec/faraday/adapter/net_http_persistent_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true

RSpec.describe Faraday::Adapter::NetHttpPersistent do
features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method
features :request_body_on_query_methods, :reason_phrase_parse, :compression, :trace_method, :streaming

it_behaves_like "an adapter"

Expand Down

0 comments on commit 92a676a

Please sign in to comment.