icyleaf / halite

💎HTTP Requests Client with a chainable REST API, built-in sessions and middlewares.
MIT License
170 stars 13 forks source link

Streaming support #53

Closed icyleaf closed 5 years ago

icyleaf commented 5 years ago

Similar to HTTP::Client (search keyword "Streaming") but Halite returns a Halite::Response object:

Halite.get "http://httpbin.org/stream/3" do |response|
  puts response.status_code
  while line = response.body_io.gets
    puts line
    puts "*" * 10
  end
end

Use streaming requests we can store binary data chunk by chunk:

Halite.get("https://github.com/icyleaf/halite/archive/master.zip") do |response|
  filename = response.filename || "halite-master.zip"
  File.open(filename, "w") do |file|
    IO.copy(response.body_io, file)
  end
end

Relates #49