Skip to content

Commit

Permalink
Pass through Rack::Response
Browse files Browse the repository at this point in the history
  • Loading branch information
mkon committed Oct 5, 2022
1 parent bcf6286 commit 42c52fb
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 12 deletions.
6 changes: 3 additions & 3 deletions examples/aggregation/config.ru
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require 'api_valve'
require 'byebug'

app = Rack::Builder.new do
use ApiValve::Middleware::ErrorHandling
Expand All @@ -9,11 +10,10 @@ app = Rack::Builder.new do
threads = (1..5).map do |i|
Thread.new { forwarder.call request, 'path' => "posts/#{i}" }
end
threads.each(&:join)
body = threads.map(&:value).map do |rack_response|
JSON.parse(rack_response[2].first)
JSON.parse(rack_response.body.first)
end.to_json
[200, {'Content-Type' => 'application/json'}, [body]]
Rack::Response.new(body, 200, {'Content-Type' => 'application/json'})
end
end

Expand Down
2 changes: 1 addition & 1 deletion examples/basic/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ require 'api_valve'

app = Rack::Builder.new do
map '/api' do
run ApiValve::Proxy.from_hash(endpoint: 'http://api.host/api/')
run ApiValve::Proxy.from_hash(endpoint: 'https://jsonplaceholder.typicode.com')
end
map '/oauth' do
run ApiValve::Proxy.from_hash(endpoint: 'http://auth.host/oauth/')
Expand Down
4 changes: 2 additions & 2 deletions lib/api_valve/error_responder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ def initialize(error)
end

def call
[
Rack::Response[
status,
{'Content-Type' => 'application/vnd.api+json'},
[MultiJson.dump({errors: [json_error]}, mode: :compat)]
MultiJson.dump({errors: [json_error]}, mode: :compat)
]
end

Expand Down
2 changes: 1 addition & 1 deletion lib/api_valve/forwarder/response.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(original_request, original_response, options = {})

# Must return a rack compatible response array of status code, headers and body
def rack_response
[status, headers, [body]]
Rack::Response.new(body, status, headers)
end

protected
Expand Down
6 changes: 5 additions & 1 deletion lib/api_valve/middleware/error_handling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def call(env)
@app.call(env)
rescue Exception => e # rubocop:disable Lint/RescueException
log_error e
self.class.const_get(ApiValve.error_responder).new(e).call
render_error(e).to_a
end

private
Expand All @@ -17,5 +17,9 @@ def log_error(error)
ApiValve.logger.error { "#{error.class}: #{error.message}" }
ApiValve.logger.error { error.backtrace.join("\n") }
end

def render_error(error)
self.class.const_get(ApiValve.error_responder).new(error).call
end
end
end
4 changes: 2 additions & 2 deletions lib/api_valve/proxy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ def initialize(forwarder)
end

def call(env)
to_app.call(env)
to_app.call(env).to_a
rescue ApiValve::Error::Client, ApiValve::Error::Server => e
render_error e
render_error(e).to_a
end

delegate :add_route, to: :route_set
Expand Down
4 changes: 2 additions & 2 deletions spec/api_valve/forwarder/response_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
let(:response_headers) do
{'Location' => '/remote-prefix/see/other/path'}
end
let(:rack_response) { response.rack_response }
let(:headers) { rack_response[1] }
let(:rack_response) { Rack::Response[*response.rack_response] }
let(:headers) { rack_response.headers }

describe 'Location header' do
subject { headers['Location'] }
Expand Down

0 comments on commit 42c52fb

Please sign in to comment.