Skip to content

Commit

Permalink
try streamed approach
Browse files Browse the repository at this point in the history
  • Loading branch information
ndushay committed Sep 11, 2019
1 parent 4104511 commit c3a3b66
Showing 1 changed file with 21 additions and 6 deletions.
27 changes: 21 additions & 6 deletions app/models/symphony_reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ class ResponseError < StandardError; end
attr_reader :catkey

def self.client
Faraday.new(headers: Settings.catalog.symphony.headers) do |conn|
conn.request :multipart
conn.request :url_encoded
conn.use FaradayMiddleware::Chunked
conn.adapter :net_http
end
Faraday.new(headers: Settings.catalog.symphony.headers) # do |conn|
# conn.request :url_encoded
# conn.use FaradayMiddleware::Chunked
# conn.adapter :net_http
# end
end

def initialize(catkey:)
Expand Down Expand Up @@ -50,6 +49,22 @@ def client
def symphony_response
resp = client.get(format(Settings.catalog.symphony.json_url, catkey: catkey))

if resp.headers['transfer-encoding'] == 'chunked'
streamed = []

conn.get(format(Settings.catalog.symphony.json_url, catkey: catkey)) do |req|
# Set a callback which will receive tuples of chunk Strings
# and the sum of characters received so far
req.options.on_data = Proc.new do |chunk, overall_received_bytes|
puts "Received #{overall_received_bytes} characters"
streamed << chunk
end
end

resp.body = streamed.join
resp.status = 200
end

if resp.status == 200
validate_response(resp)
return resp
Expand Down

0 comments on commit c3a3b66

Please sign in to comment.