def streaming_request(url, headers, &block)
headers = build_headers(:GET, url, headers, nil, true)
retriable_rest_request(:GET, url, nil, headers) do |rest_request|
tempfile = nil
response = rest_request.call do |r|
if block_given? && r.kind_of?(Net::HTTPSuccess)
begin
tempfile = stream_to_tempfile(url, r, &block)
yield tempfile
ensure
tempfile.close!
end
else
tempfile = stream_to_tempfile(url, r)
end
end
if response.kind_of?(Net::HTTPSuccess)
tempfile
elsif redirect_location = redirected_to(response)
tempfile && tempfile.close!
follow_redirect {streaming_request(create_url(redirect_location), {}, &block)}
else
tempfile && tempfile.close!
response.error!
end
end
end