-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generate error message on missing exchange rates while converting bal…
…ances
- Loading branch information
1 parent
943d436
commit b328230
Showing
15 changed files
with
95 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,62 +1,55 @@ | ||
class Provider::Synth | ||
class << self | ||
include Retryable | ||
include Retryable | ||
|
||
attr_accessor :configuration | ||
|
||
def initialized? | ||
configuration.api_key.present? | ||
end | ||
def initialize(api_key = ENV["SYNTH_API_KEY"]) | ||
@api_key = api_key || ENV["SYNTH_API_KEY"] | ||
end | ||
|
||
def configure | ||
self.configuration ||= Configuration.new | ||
yield(configuration) | ||
end | ||
def configured? | ||
@api_key.present? | ||
end | ||
|
||
def fetch_exchange_rate(from:, to:, date:) | ||
retrying Provider::Base.known_transient_errors do |on_last_attempt| | ||
response = Faraday.get("#{base_url}/rates/historical") do |req| | ||
req.headers["Authorization"] = "Bearer #{configuration.api_key}" | ||
req.params["date"] = date.to_s | ||
req.params["from"] = from | ||
req.params["to"] = to | ||
end | ||
def fetch_exchange_rate(from:, to:, date:) | ||
retrying Provider::Base.known_transient_errors do |on_last_attempt| | ||
response = Faraday.get("#{base_url}/rates/historical") do |req| | ||
req.headers["Authorization"] = "Bearer #{api_key}" | ||
req.params["date"] = date.to_s | ||
req.params["from"] = from | ||
req.params["to"] = to | ||
end | ||
|
||
if response.success? | ||
if response.success? | ||
ExchangeRateResponse.new \ | ||
rate: JSON.parse(response.body).dig("data", "rates", to), | ||
success?: true, | ||
raw_response: response | ||
else | ||
if on_last_attempt | ||
ExchangeRateResponse.new \ | ||
rate: JSON.parse(response.body).dig("data", "rates", to), | ||
success?: true, | ||
success?: false, | ||
error: build_error(response), | ||
raw_response: response | ||
else | ||
if on_last_attempt | ||
ExchangeRateResponse.new \ | ||
success?: false, | ||
error: build_error(response), | ||
raw_response: response | ||
else | ||
raise build_error(response) | ||
end | ||
raise build_error(response) | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
class Configuration | ||
attr_accessor :api_key | ||
end | ||
private | ||
attr_reader :api_key | ||
|
||
ExchangeRateResponse = Struct.new :rate, :success?, :error, :raw_response, keyword_init: true | ||
ExchangeRateResponse = Struct.new :rate, :success?, :error, :raw_response, keyword_init: true | ||
|
||
def base_url | ||
"https://api.synthfinance.com" | ||
end | ||
def base_url | ||
"https://api.synthfinance.com" | ||
end | ||
|
||
def build_error(response) | ||
Provider::Base::ProviderError.new(<<~ERROR) | ||
Failed to fetch exchange rate from #{self} | ||
Status: #{response.status} | ||
Body: #{response.body.inspect} | ||
ERROR | ||
end | ||
end | ||
def build_error(response) | ||
Provider::Base::ProviderError.new(<<~ERROR) | ||
Failed to fetch exchange rate from #{self.class} | ||
Status: #{response.status} | ||
Body: #{response.body.inspect} | ||
ERROR | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
db/migrate/20240524203959_change_account_error_columns_default.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class ChangeAccountErrorColumnsDefault < ActiveRecord::Migration[7.2] | ||
def up | ||
change_column_default :accounts, :sync_warnings, from: "[]", to: [] | ||
change_column_default :accounts, :sync_errors, from: "[]", to: [] | ||
Account.update_all(sync_warnings: []) | ||
Account.update_all(sync_errors: []) | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters