diff --git a/.mailmap b/.mailmap index f762996..ca19b6b 100644 --- a/.mailmap +++ b/.mailmap @@ -1 +1,2 @@ -Flavio Fernandes \ No newline at end of file +Flavio Fernandes +Korbin Hoffman \ No newline at end of file diff --git a/async-http-faraday.gemspec b/async-http-faraday.gemspec index 82d49d8..578b876 100644 --- a/async-http-faraday.gemspec +++ b/async-http-faraday.gemspec @@ -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'] diff --git a/lib/async/http/faraday/adapter.rb b/lib/async/http/faraday/adapter.rb index 8fac5d4..0a688af 100644 --- a/lib/async/http/faraday/adapter.rb +++ b/lib/async/http/faraday/adapter.rb @@ -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' @@ -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 diff --git a/lib/async/http/faraday/clients.rb b/lib/async/http/faraday/clients.rb index 6c52c9a..2845cec 100644 --- a/lib/async/http/faraday/clients.rb +++ b/lib/async/http/faraday/clients.rb @@ -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' diff --git a/license.md b/license.md index dbbc208..ce95b24 100644 --- a/license.md +++ b/license.md @@ -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 diff --git a/test/async/http/faraday/adapter.rb b/test/async/http/faraday/adapter.rb index 9150239..689840a 100644 --- a/test/async/http/faraday/adapter.rb +++ b/test/async/http/faraday/adapter.rb @@ -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 @@ -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