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,5 +1,9 @@
## [Released]

## [0.12.0] - 2025-06-26

- Add timeout error

## [0.11.0] - 2025-06-25

- Add create report request
Expand Down
3 changes: 2 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
boapi (0.10.0)
boapi (0.12.0)
faraday (> 0.7.6, < 1.0)
faraday_middleware (> 0.1, < 1.0)

Expand Down Expand Up @@ -74,6 +74,7 @@ GEM

PLATFORMS
-darwin-21
-darwin-24
x86_64-darwin-21
x86_64-darwin-23
x86_64-linux
Expand Down
40 changes: 40 additions & 0 deletions lib/boapi/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,22 @@ def create_report(params)
send_request(:post, '/api/v2/reports', params)
end

# rubocop:disable Metrics/AbcSize
# rubocop:disable Metrics/MethodLength
def send_request(method, path, params = nil)
response =
begin
connection.public_send(method, path, params) do |request|
request.headers['Content-Type'] = 'application/json' if %i[post put patch].include?(method)
end
rescue Faraday::ConnectionFailed => e
Boapi.configuration.logger.error("BOAPI::CLIENT::FARADAY::ERROR::CONNECTION_FAILED :: #{e}")
Comment thread
zhabinka marked this conversation as resolved.

build_connection_error_body(e)
rescue Faraday::TimeoutError => e
Boapi.configuration.logger.error("BOAPI::CLIENT::FARADAY::ERROR::READ_TIMEOUT :: #{e}")

build_read_timeout_error_body(e)
rescue Faraday::Error => e
Boapi.configuration.logger.error("BOAPI::CLIENT::FARADAY::ERROR :: #{e}")

Expand All @@ -100,6 +110,8 @@ def send_request(method, path, params = nil)
Boapi::Response.new(response)
end

# rubocop:enable Metrics/AbcSize
# rubocop:enable Metrics/MethodLength
def connection
@connection ||= build_connection
end
Expand Down Expand Up @@ -142,6 +154,34 @@ def build_error_body(error_message)
)
end

def build_read_timeout_error_body(error_message)
Comment thread
zhabinka marked this conversation as resolved.
Struct.new(:status, :success?, :body, keyword_init: true).new(
status: 504,
body: {
'error' => {
'code' => 'gateway_timeout',
'friendly_message' => 'ReadTimeout error occurred',
'message' => error_message.to_s
}
},
success?: false
)
end

def build_connection_error_body(error_message)
Struct.new(:status, :success?, :body, keyword_init: true).new(
status: 502,
body: {
'error' => {
'code' => 'bad_gateway',
'friendly_message' => 'Connection error',
'message' => error_message.to_s
}
},
success?: false
)
end

# rubocop:enable Metrics/MethodLength
end
# rubocop:enable Metrics/ClassLength
Expand Down
2 changes: 1 addition & 1 deletion lib/boapi/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module Boapi
VERSION = '0.10.0'
VERSION = '0.12.0'
end
36 changes: 36 additions & 0 deletions spec/boapi/client_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,42 @@
end
end

context 'when connection error' do
let(:response) { Boapi::Client.new(account_id: account_id, account_secret: account_secret).health }
let(:url) { "#{Boapi.configuration.api_host}/api/health" }

before do
stub_request(:get, url)
.to_raise(Faraday::ConnectionFailed.new('Failed to connect'))
end

it 'returns connection_failed response' do
expect(response.status).to be 502

expect(response.success?).to be false
expect(response.error?).to be true
expect(response.error).to eq SupportFixtures.bad_gateway_response
end
end

context 'when timeout error' do
let(:response) { Boapi::Client.new(account_id: account_id, account_secret: account_secret).health }
let(:url) { "#{Boapi.configuration.api_host}/api/health" }

before do
stub_request(:get, url)
.to_raise(Faraday::TimeoutError.new('Timeout'))
end

it 'returns connection_failed response' do
expect(response.status).to be 504

expect(response.success?).to be false
expect(response.error?).to be true
expect(response.error).to eq SupportFixtures.gateway_timeout_response
end
end

describe '.health' do
let(:response) { Boapi::Client.new(account_id: account_id, account_secret: account_secret).health }
let(:http_status) { 200 }
Expand Down
16 changes: 16 additions & 0 deletions spec/fixtures/support_fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,20 @@ def failed_without_authentification_response_message
def failed_without_authentification_response
%({"error":#{failed_without_authentification_response_message.to_json}})
end

def bad_gateway_response
{
'code' => 'bad_gateway',
'friendly_message' => 'Connection error',
'message' => 'Failed to connect'
}
end

def gateway_timeout_response
{
'code' => 'gateway_timeout',
'friendly_message' => 'ReadTimeout error occurred',
'message' => 'Timeout'
}
end
end