diff options
author | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-08 15:13:49 +0900 |
---|---|---|
committer | Kazuki Yamaguchi <k@rhe.jp> | 2016-05-08 15:24:17 +0900 |
commit | db7f141a7ea0d486599e02c839f12ddc72cdd152 (patch) | |
tree | 9c4e827a534a769c3b2311c82a67bc267b2d5afd | |
parent | cd269bf5693bcb9a7b5b301bbd3a0eb65222b9fc (diff) | |
download | plum-db7f141a7ea0d486599e02c839f12ddc72cdd152.tar.gz |
client: add Response#join method
This method waits until the response completes.
-rw-r--r-- | examples/client/synchronous.rb | 24 | ||||
-rw-r--r-- | lib/plum/client.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/client_session.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/legacy_client_session.rb | 2 | ||||
-rw-r--r-- | lib/plum/client/response.rb | 12 | ||||
-rw-r--r-- | test/plum/client/test_response.rb | 22 |
6 files changed, 49 insertions, 15 deletions
diff --git a/examples/client/synchronous.rb b/examples/client/synchronous.rb new file mode 100644 index 0000000..6701e1e --- /dev/null +++ b/examples/client/synchronous.rb @@ -0,0 +1,24 @@ +# frozen-string-literal: true +# client/synchronous.rb: download 3 files in sequence +$LOAD_PATH.unshift File.expand_path("../../../lib", __FILE__) +require "plum" +require "zlib" + +client = Plum::Client.start("http2.golang.org", 443) + +reqinfo = client.get("/reqinfo").join +puts "/reqinfo: #{reqinfo.status}" + +test = "test" +crc32 = client.put("/crc32", test).join +puts "/crc32{#{test}}: #{crc32.body}" +puts "Zlib.crc32: #{Zlib.crc32(test).to_s(16)}" + +client.get("/clockstream") + .on_headers { |res| + puts "status: #{res.status}, headers: #{res.headers}" + }.on_chunk { |chunk| + puts chunk + }.on_finish { + puts "finish!" + }.join diff --git a/lib/plum/client.rb b/lib/plum/client.rb index bdbfd72..cb968b4 100644 --- a/lib/plum/client.rb +++ b/lib/plum/client.rb @@ -33,7 +33,7 @@ module Plum @host = host @port = port || (config[:https] ? 443 : 80) end - @config = DEFAULT_CONFIG.merge(hostname: host).merge(config) + @config = DEFAULT_CONFIG.merge(hostname: host).merge!(config) @started = false end diff --git a/lib/plum/client/client_session.rb b/lib/plum/client/client_session.rb index 0c7e366..f21d2a6 100644 --- a/lib/plum/client/client_session.rb +++ b/lib/plum/client/client_session.rb @@ -42,7 +42,7 @@ module Plum ":scheme" => @config[:https] ? "https" : "http", }.merge(headers) - response = Response.new(**options, &headers_cb) + response = Response.new(self, **options, &headers_cb) @responses << response stream = @plum.open_stream stream.send_headers(headers, end_stream: !body) diff --git a/lib/plum/client/legacy_client_session.rb b/lib/plum/client/legacy_client_session.rb index 07ae6a9..3766ad3 100644 --- a/lib/plum/client/legacy_client_session.rb +++ b/lib/plum/client/legacy_client_session.rb @@ -39,7 +39,7 @@ module Plum end end - response = Response.new(**options, &headers_cb) + response = Response.new(self, **options, &headers_cb) @requests << [response, headers, body, chunked] consume_queue response diff --git a/lib/plum/client/response.rb b/lib/plum/client/response.rb index a091c7a..32a3d30 100644 --- a/lib/plum/client/response.rb +++ b/lib/plum/client/response.rb @@ -6,9 +6,9 @@ module Plum attr_reader :headers # @api private - def initialize(auto_decode: true, **options, &on_headers) + def initialize(session, auto_decode: true, **options, &on_headers) + @session = session @headers = nil - @body = Queue.new @finished = false @failed = false @body = [] @@ -48,6 +48,7 @@ module Plum raise ArgumentError, "block must be given" unless block_given? @on_headers = block yield self if @headers + self end # Set callback that will be called when received a chunk of response body. @@ -60,6 +61,7 @@ module Plum @body.each(&block) @body.clear end + self end # Set callback that will be called when the response finished. @@ -70,6 +72,7 @@ module Plum else @on_finish = block end + self end # Returns the complete response body. Use #each_body instead if the body can be very large. @@ -80,6 +83,11 @@ module Plum @body.join end + def join + @session.succ until (@finished || @failed) + self + end + private # internal: set headers and setup decoder def set_headers(headers) diff --git a/test/plum/client/test_response.rb b/test/plum/client/test_response.rb index a1af31c..a4c7fb8 100644 --- a/test/plum/client/test_response.rb +++ b/test/plum/client/test_response.rb @@ -3,7 +3,7 @@ require "test_helper" using Plum::BinaryString class ResponseTest < Minitest::Test def test_finished - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, {}) assert_equal(false, resp.finished?) resp.send(:finish) @@ -11,13 +11,13 @@ class ResponseTest < Minitest::Test end def test_fail - resp = Response.new + resp = Response.new(nil) resp.send(:fail, true) assert(resp.failed?, "response must be failed") end def test_status - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, ":status" => "200" ) @@ -25,7 +25,7 @@ class ResponseTest < Minitest::Test end def test_headers - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, ":status" => "200", "header" => "abc" @@ -34,7 +34,7 @@ class ResponseTest < Minitest::Test end def test_body - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, {}) resp.send(:add_chunk, "a") resp.send(:add_chunk, "b") @@ -43,7 +43,7 @@ class ResponseTest < Minitest::Test end def test_body_not_finished - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, {}) resp.send(:add_chunk, "a") resp.send(:add_chunk, "b") @@ -54,7 +54,7 @@ class ResponseTest < Minitest::Test def test_on_headers_initialize called = false - resp = Response.new { |r| called = true } + resp = Response.new(nil) { |r| called = true } assert(!called) resp.send(:set_headers, { ":status" => 201 }) assert(called) @@ -62,7 +62,7 @@ class ResponseTest < Minitest::Test def test_on_headers_explicit called = false - resp = Response.new + resp = Response.new(nil) resp.on_headers { |r| called = true } assert(!called) resp.send(:set_headers, { ":status" => 201 }) @@ -70,7 +70,7 @@ class ResponseTest < Minitest::Test end def test_on_chunk - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, {}) res = [] resp.send(:add_chunk, "a") @@ -83,7 +83,7 @@ class ResponseTest < Minitest::Test end def test_on_finish - resp = Response.new + resp = Response.new(nil) resp.send(:set_headers, {}) ran = false resp.on_finish { ran = true } @@ -93,4 +93,6 @@ class ResponseTest < Minitest::Test resp.on_finish { ran = true } assert(ran) end + + # FIXME: test Response#join end |