From 7149a1293d343361d806ebfe36eb5a9d883bb8af Mon Sep 17 00:00:00 2001 From: Jakub Strus Date: Tue, 26 Oct 2021 17:29:37 +0200 Subject: [PATCH 1/3] Lock Faraday gem on version ~> 1.0 --- basecrm.gemspec | 2 +- lib/basecrm/http_client.rb | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/basecrm.gemspec b/basecrm.gemspec index 173b5b3..e893b80 100644 --- a/basecrm.gemspec +++ b/basecrm.gemspec @@ -20,7 +20,7 @@ Gem::Specification.new do |spec| spec.files = Dir["README.md", "LICENSE", "lib/**/*"] spec.test_files = Dir["spec/**/*"] - spec.add_dependency "faraday", "~> 0.9", ">= 0.9.0" + spec.add_dependency "faraday", "~> 1.0" spec.add_dependency "json", "~> 2.0" spec.add_development_dependency "rspec", "~> 3.2" diff --git a/lib/basecrm/http_client.rb b/lib/basecrm/http_client.rb index 2b14265..b3f03a0 100644 --- a/lib/basecrm/http_client.rb +++ b/lib/basecrm/http_client.rb @@ -74,7 +74,7 @@ def request(method, path, data={}, headers={}) body = extract_body(res) @config.logger.debug body if @config.debug? && body && @config.logger [res.status, res.headers, body] - rescue Faraday::Error::ConnectionFailed => e + rescue Faraday::ConnectionFailed => e raise ConnectionError, e.message end From bbc2a47711f5fbd63618df89362dc591d4429994 Mon Sep 17 00:00:00 2001 From: Jakub Strus Date: Tue, 26 Oct 2021 17:31:21 +0200 Subject: [PATCH 2/3] Add optional max_retry and retry_statuses parameters to HttpClient --- README.md | 2 ++ lib/basecrm/configuration.rb | 4 ++++ lib/basecrm/http_client.rb | 8 ++++++++ spec/http_client_spec.rb | 15 +++++++++++++++ spec/support/client_helpers.rb | 4 ++++ 5 files changed, 33 insertions(+) create mode 100644 spec/http_client_spec.rb diff --git a/README.md b/README.md index 1afa3d0..49afd5b 100644 --- a/README.md +++ b/README.md @@ -49,6 +49,8 @@ The following options are available while instantiating a client: * __timeout__: Request timeout * __verbose__: Verbose/debug mode * __logger__: Logger used in verbose mode + * __max_retry__: Number of retries on failed requests. Passed to Faraday + * __retry_statuses__: By default only timeout error will be retries. This allows to retry on specific HTTP statuses. Passed to Faraday ### Architecture diff --git a/lib/basecrm/configuration.rb b/lib/basecrm/configuration.rb index 58764c6..78bd2f1 100644 --- a/lib/basecrm/configuration.rb +++ b/lib/basecrm/configuration.rb @@ -7,6 +7,8 @@ class Configuration attr_reader :user_agent attr_reader :timeout attr_reader :verify_ssl + attr_reader :max_retry + attr_reader :retry_statuses attr_reader :logger, :verbose alias_method :debug?, :verbose @@ -19,6 +21,8 @@ def initialize(options={}) @verbose = !!options[:verbose] @timeout = options[:timeout] || 30 @verify_ssl = options.fetch(:verify_ssl, true) + @max_retry = options[:max_retry] + @retry_statuses = options[:retry_statuses] end def validate! diff --git a/lib/basecrm/http_client.rb b/lib/basecrm/http_client.rb index b3f03a0..96d0575 100644 --- a/lib/basecrm/http_client.rb +++ b/lib/basecrm/http_client.rb @@ -22,6 +22,7 @@ def initialize(config) options[:ssl] = { verify: false } unless config.verify_ssl @client = Faraday.new(config.base_url, options) do |faraday| + faraday.request :retry, retry_options faraday.use BaseCRM::Middlewares::OAuthBearerToken, config.access_token faraday.use BaseCRM::Middlewares::RaiseError faraday.response :logger, config.logger if config.debug? @@ -89,5 +90,12 @@ def extract_body(res) content_type = res.headers['Content-Type'] content_type && content_type.include?('json') ? JSON.parse(res.body, symbolize_names: true) : res.body end + + def retry_options + retry_options = {} + retry_options[:max] = @config.max_retry if @config.max_retry + retry_options[:retry_statuses] = @config.retry_statuses if @config.retry_statuses + retry_options + end end end diff --git a/spec/http_client_spec.rb b/spec/http_client_spec.rb new file mode 100644 index 0000000..bb5ce4d --- /dev/null +++ b/spec/http_client_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +describe BaseCRM::HttpClient do + describe 'Retries' do + let(:statuses_to_retry) { [503] } + let(:max_retry) { 3 } + subject(:client_with_retry) { client_with_basic_retry(max_retry: max_retry, on_statuses: statuses_to_retry) } + + it "should pass retry statues to Faraday and do a valid request" do + expect(client_with_retry.http_client.client.app.options.retry_statuses).to match_array(statuses_to_retry) + expect(client_with_retry.http_client.client.app.options.max).to eq(max_retry) + expect(client_with_retry.accounts.self).to be_instance_of BaseCRM::Account + end + end +end diff --git a/spec/support/client_helpers.rb b/spec/support/client_helpers.rb index beafc82..cbfae29 100644 --- a/spec/support/client_helpers.rb +++ b/spec/support/client_helpers.rb @@ -3,6 +3,10 @@ def client @client ||= BaseCRM::Client.new(access_token: access_token, base_url: base_url) end + def client_with_basic_retry(max_retry: 1, on_statuses: []) + @client_with_basic_retry ||= BaseCRM::Client.new(access_token: access_token, base_url: base_url, max_retry: max_retry, retry_statuses: on_statuses) + end + def access_token @access_token ||= ENV.fetch("BASECRM_ACCESS_TOKEN") { raise '"BASECRM_ACCESS_TOKEN" has not been found.' } end From 23629dfe5ada25a93b64a9e0120cedfdedeccf3d Mon Sep 17 00:00:00 2001 From: Jakub Strus Date: Tue, 26 Oct 2021 17:31:31 +0200 Subject: [PATCH 3/3] Prepare v2.0.0 --- CHANGELOG.md | 4 ++++ VERSION | 2 +- lib/basecrm/version.rb | 2 +- 3 files changed, 6 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4491d03..c63e1be 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,8 @@ ## CHANGELOG +v2.0.0 (2021-10-26) +**Features and Improvements** +* Faraday updated to version > 1.0 +* Optional parameters `max_retry` and `retry_statuses` to specify no. of retries on failed request and statuses to retry. v1.3.10 (2021-06-28) **Features and Improvements** diff --git a/VERSION b/VERSION index 0c00f61..227cea2 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.3.10 +2.0.0 diff --git a/lib/basecrm/version.rb b/lib/basecrm/version.rb index d60d847..859067f 100644 --- a/lib/basecrm/version.rb +++ b/lib/basecrm/version.rb @@ -1,3 +1,3 @@ module BaseCRM - VERSION = "1.3.10" + VERSION = "2.0.0" end