Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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**
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.3.10
2.0.0
2 changes: 1 addition & 1 deletion basecrm.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 4 additions & 0 deletions lib/basecrm/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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!
Expand Down
10 changes: 9 additions & 1 deletion lib/basecrm/http_client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down Expand Up @@ -74,7 +75,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

Expand All @@ -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
2 changes: 1 addition & 1 deletion lib/basecrm/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module BaseCRM
VERSION = "1.3.10"
VERSION = "2.0.0"
end
15 changes: 15 additions & 0 deletions spec/http_client_spec.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 4 additions & 0 deletions spec/support/client_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down