From bfe9b2eeb98fc6f9b7c3ad9f145f398e86e72a29 Mon Sep 17 00:00:00 2001 From: Oleg Onufriuk Date: Tue, 28 Nov 2023 07:22:58 +0100 Subject: [PATCH] add read_timeout option --- lib/be_gateway/connection.rb | 5 +++-- spec/be_gateway/connection_spec.rb | 1 + 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/be_gateway/connection.rb b/lib/be_gateway/connection.rb index 2d23d15..ebd602f 100644 --- a/lib/be_gateway/connection.rb +++ b/lib/be_gateway/connection.rb @@ -14,6 +14,7 @@ def initialize(params) @login = params.fetch(:shop_id) @password = params.fetch(:secret_key) @url = params.fetch(:url) + @read_timeout = params[:options]&.delete(:read_timeout) @opts = params[:options] || {} @rack_app = params[:rack_app] @passed_headers = params[:headers] @@ -24,7 +25,7 @@ def initialize(params) private - attr_reader :login, :password, :url, :rack_app, :version + attr_reader :login, :password, :url, :rack_app, :version, :read_timeout DEFAULT_OPEN_TIMEOUT = 5 DEFAULT_TIMEOUT = 25 @@ -80,7 +81,7 @@ def failed_response def connection @connection ||= Faraday::Connection.new(url, opts || {}) do |conn| conn.options[:open_timeout] ||= DEFAULT_OPEN_TIMEOUT - conn.options[:timeout] ||= DEFAULT_TIMEOUT + conn.options[:timeout] ||= read_timeout || DEFAULT_TIMEOUT conn.proxy ||= proxy if proxy # we use ||= to keep proxy passed within options conn.headers = headers conn.request :json diff --git a/spec/be_gateway/connection_spec.rb b/spec/be_gateway/connection_spec.rb index c6df5f7..30dbb40 100644 --- a/spec/be_gateway/connection_spec.rb +++ b/spec/be_gateway/connection_spec.rb @@ -28,6 +28,7 @@ let(:proxy) { 'http://example.com' } before { test_class.proxy = proxy } + before { params.merge!(options: { read_timeout: 60 }) } it 'uses proxy' do expect(subject.proxy.uri.to_s).to eq(proxy)