From a103660f8cba46f530df0b09ccc90595c573a6d7 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Thu, 18 Mar 2021 01:27:05 +0900 Subject: [PATCH 1/2] Add follow_redirect option --- README.md | 1 + lib/presto/client/faraday_client.rb | 4 +++- lib/presto/client/query.rb | 1 + presto-client.gemspec | 1 + spec/statement_client_spec.rb | 14 ++++++++++++++ 5 files changed, 20 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 28142613..75446210 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ client = Presto::Client.new( }, http_proxy: "proxy.example.com:8080", http_debug: true, + follow_redirect: true ) # run a query and get results as an array of arrays: diff --git a/lib/presto/client/faraday_client.rb b/lib/presto/client/faraday_client.rb index a1aa5b59..980c60f8 100644 --- a/lib/presto/client/faraday_client.rb +++ b/lib/presto/client/faraday_client.rb @@ -60,7 +60,9 @@ def self.faraday_client(options) if options[:user] && options[:password] faraday.basic_auth(options[:user], options[:password]) end - + if options[:follow_redirect] == true + faraday.use FaradayMiddleware::FollowRedirects + end faraday.response :logger if options[:http_debug] faraday.adapter Faraday.default_adapter end diff --git a/lib/presto/client/query.rb b/lib/presto/client/query.rb index 9cfc1032..a01d2e54 100644 --- a/lib/presto/client/query.rb +++ b/lib/presto/client/query.rb @@ -16,6 +16,7 @@ module Presto::Client require 'faraday' + require 'faraday_middleware' require 'presto/client/models' require 'presto/client/errors' require 'presto/client/faraday_client' diff --git a/presto-client.gemspec b/presto-client.gemspec index 71f462a7..53fe7c8e 100644 --- a/presto-client.gemspec +++ b/presto-client.gemspec @@ -20,6 +20,7 @@ Gem::Specification.new do |gem| gem.required_ruby_version = ">= 1.9.1" gem.add_dependency "faraday", ["~> 0.12"] + gem.add_dependency "faraday_middleware", ["~> 0.12.2"] gem.add_dependency "msgpack", [">= 0.7.0"] gem.add_development_dependency "rake", [">= 0.9.2", "< 11.0"] diff --git a/spec/statement_client_spec.rb b/spec/statement_client_spec.rb index 94fd1e80..9e63742a 100644 --- a/spec/statement_client_spec.rb +++ b/spec/statement_client_spec.rb @@ -10,6 +10,7 @@ time_zone: "US/Pacific", language: "ja_JP", debug: true, + follow_redirect: true } end @@ -229,6 +230,19 @@ statement_client.query_info end.should raise_error(PrestoHttpError, /Presto API returned unexpected data format./) end + + it "is redirected if server returned 301" do + stub_request(:get, "http://localhost/v1/query/#{response_json2[:id]}"). + with(headers: headers). + to_return(status: 301, headers: {"Location" => "http://localhost/v1/query/redirected"}) + + stub_request(:get, "http://localhost/v1/query/redirected"). + with(headers: headers). + to_return(body: {"queryId" => "queryid"}.to_json) + + query_info = statement_client.query_info + query_info.query_id.should == "queryid" + end end describe "Killing a query" do From 64c0b819c4742720f2f0050fffd2f2fd4e9d93f6 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Thu, 18 Mar 2021 09:36:52 +0900 Subject: [PATCH 2/2] Update lib/presto/client/faraday_client.rb Co-authored-by: NARUSE, Yui --- lib/presto/client/faraday_client.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/presto/client/faraday_client.rb b/lib/presto/client/faraday_client.rb index 980c60f8..8cd23d34 100644 --- a/lib/presto/client/faraday_client.rb +++ b/lib/presto/client/faraday_client.rb @@ -60,7 +60,7 @@ def self.faraday_client(options) if options[:user] && options[:password] faraday.basic_auth(options[:user], options[:password]) end - if options[:follow_redirect] == true + if options[:follow_redirect] faraday.use FaradayMiddleware::FollowRedirects end faraday.response :logger if options[:http_debug]