Skip to content

Commit

Permalink
[#270] rewrite Adapters::HttpAdapter to fix missed events in streams
Browse files Browse the repository at this point in the history
As per:

openownership/register#270 (comment)

Also simplify the code significantly.
  • Loading branch information
tiredpixel committed Jun 27, 2024
1 parent c08dd81 commit 25afde5
Showing 1 changed file with 28 additions and 39 deletions.
67 changes: 28 additions & 39 deletions lib/register_common/adapters/http_adapter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,51 +8,40 @@ module RegisterCommon
module Adapters
class HttpAdapter
HttpError = Class.new(StandardError)

HttpResponse = Struct.new(:status, :headers, :body, :success)

# rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
def get(url, params: {}, headers: {}, raise_on_failure: false)
streamed = nil
response =
if block_given?
streamed = []
current_chunk = ''

Faraday.new.get(
URI(url), params, headers
) do |req|
req.options.on_data = proc do |chunk, _overall_received_bytes|
current_chunk += chunk
lines = current_chunk.split("\n")
if current_chunk[-1] == "\n"
lines[0...-1].each do |line|
next if line.empty?

yield line
end
current_chunk = ''
elsif lines.length > 1
lines[0...-1].each do |line|
next if line.empty?

yield line
end
current_chunk = lines[-1]
end
streamed << chunk
def get(url, params: {}, headers: {}, raise_on_failure: false, &block)
url = URI(url)
res = if block_given?
get_streamed(url, params, headers, &block)
else
get_all(url, params, headers)
end
end
else
Faraday.new.get(
URI(url), params, headers
)
end
raise HttpError if !res.success? && raise_on_failure

HttpResponse.new(res.status, res.headers, res.body, res.success?)
end

raise HttpError if !response.success? && raise_on_failure
private

HttpResponse.new(response.status, response.headers, streamed ? streamed.join : response.body, response.success?)
def get_all(url, params, headers)
Faraday.new.get(url, params, headers)
end

def get_streamed(url, params, headers)
bfr = ''
Faraday.new.get(url, params, headers) do |req|
req.options.on_data = proc do |chunk|
bfr += chunk
lines = bfr.split("\n", -1)
lines[0...-1].each do |line|
yield line unless line.empty?
end
bfr = lines[-1]
end
end
end
# rubocop:enable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
end
end
end

0 comments on commit 25afde5

Please sign in to comment.