Skip to content

Commit

Permalink
Add support for on_data, enabling streaming response body. (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Aug 13, 2024
1 parent c1ff0ae commit 38ec627
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 15 deletions.
3 changes: 2 additions & 1 deletion .mailmap
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Flavio Fernandes <flavio.fernandes6@gmail.com>
Flavio Fernandes <flavio.fernandes6@gmail.com>
Korbin Hoffman <k1@k1.io>
2 changes: 1 addition & 1 deletion async-http-faraday.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
spec.version = Async::HTTP::Faraday::VERSION

spec.summary = "Provides an adaptor between async-http and faraday."
spec.authors = ["Samuel Williams", "Igor Sidorov", "Andreas Garnaes", "Genki Takiuchi", "Olle Jonsson", "Benoit Daloze", "Denis Talakevich", "Flavio Fernandes", "Jacob Frautschi"]
spec.authors = ["Samuel Williams", "Igor Sidorov", "Andreas Garnaes", "Genki Takiuchi", "Olle Jonsson", "Benoit Daloze", "Denis Talakevich", "Flavio Fernandes", "Jacob Frautschi", "Korbin Hoffman"]
spec.license = "MIT"

spec.cert_chain = ['release.cert']
Expand Down
21 changes: 18 additions & 3 deletions lib/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# Copyright, 2023, by Genki Takiuchi.
# Copyright, 2023, by Flavio Fernandes.
# Copyright, 2024, by Jacob Frautschi.
# Copyright, 2024, by Korbin Hoffman.

require 'faraday'
require 'faraday/adapter'
Expand Down Expand Up @@ -119,9 +120,23 @@ def call(env)
request = ::Protocol::HTTP::Request.new(endpoint.scheme, endpoint.authority, method, endpoint.path, nil, headers, body)

with_timeout do
response = client.call(request)

save_response(env, response.status, encoded_body(response), response.headers)
if env.stream_response?
response = env.stream_response do |&on_data|
response = client.call(request)

response.each do |chunk|
on_data.call(chunk)
end

response
end

save_response(env, response.status, nil, response.headers)
else
response = client.call(request)

save_response(env, response.status, encoded_body(response), response.headers)
end
end
end

Expand Down
8 changes: 1 addition & 7 deletions lib/async/http/faraday/clients.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,7 @@
# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2018-2024, by Samuel Williams.
# Copyright, 2018, by Andreas Garnaes.
# Copyright, 2019, by Denis Talakevich.
# Copyright, 2019-2020, by Igor Sidorov.
# Copyright, 2023, by Genki Takiuchi.
# Copyright, 2023, by Flavio Fernandes.
# Copyright, 2024, by Jacob Frautschi.
# Copyright, 2024, by Samuel Williams.

require 'faraday'
require 'faraday/adapter'
Expand Down
1 change: 1 addition & 0 deletions license.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Copyright, 2020, by Benoit Daloze.
Copyright, 2023, by Genki Takiuchi.
Copyright, 2023, by Flavio Fernandes.
Copyright, 2024, by Jacob Frautschi.
Copyright, 2024, by Korbin Hoffman.

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
41 changes: 38 additions & 3 deletions test/async/http/faraday/adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})

expect(response.body).to be == 'text=Hello+World'
end

it "can use a ::Protocol::HTTP::Body::Readable body" do
readable = ::Protocol::HTTP::Body::File.new(File.open(__FILE__, 'r'), 0...128)

response = Faraday.new do |builder|
builder.adapter :async_http
end.post(bound_url, readable)

expect(response.body).to be == File.read(__FILE__, 128)
end
end
Expand All @@ -155,6 +155,41 @@ def get_response(url = bound_url, path = '/index', adapter_options: {})
expect(config_block_invoked).to be == true
end
end

with "a streaming response" do
let(:app) do
Protocol::HTTP::Middleware.for do |request|
body = ::Async::HTTP::Body::Writable.new

Async do
3.times do |i|
body.write("chunk#{i}")
end
ensure
body.close
end

Protocol::HTTP::Response[200, {}, body]
end
end

it "can stream response" do
client = Faraday.new do |builder|
builder.adapter :async_http
end

chunks = []

response = client.get(bound_url) do |request|
request.options.on_data = proc do |chunk|
chunks << chunk
end
end

expect(response.body).to be_nil
expect(chunks).to be == ["chunk0", "chunk1", "chunk2"]
end
end
end

with "a remote http server" do
Expand Down

0 comments on commit 38ec627

Please sign in to comment.