-
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.
Refactor synth provider and show UI error if not configured
- Loading branch information
1 parent
128aaad
commit 7ad98f0
Showing
9 changed files
with
83 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,62 @@ | ||
class Provider::Synth | ||
include Retryable | ||
class << self | ||
include Retryable | ||
|
||
def initialize(api_key = ENV["SYNTH_API_KEY"]) | ||
@api_key = api_key || ENV["SYNTH_API_KEY"] | ||
end | ||
attr_accessor :configuration | ||
|
||
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 | ||
def initialized? | ||
configuration.api_key.present? | ||
end | ||
|
||
def configure | ||
self.configuration ||= Configuration.new | ||
yield(configuration) | ||
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 | ||
|
||
if response.success? | ||
ExchangeRateResponse.new \ | ||
rate: JSON.parse(response.body).dig("data", "rates", to), | ||
success?: true, | ||
raw_response: response | ||
else | ||
if on_last_attempt | ||
if response.success? | ||
ExchangeRateResponse.new \ | ||
success?: false, | ||
error: build_error(response), | ||
rate: JSON.parse(response.body).dig("data", "rates", to), | ||
success?: true, | ||
raw_response: response | ||
else | ||
raise build_error(response) | ||
if on_last_attempt | ||
ExchangeRateResponse.new \ | ||
success?: false, | ||
error: build_error(response), | ||
raw_response: response | ||
else | ||
raise build_error(response) | ||
end | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
attr_reader :api_key | ||
private | ||
class Configuration | ||
attr_accessor :api_key | ||
end | ||
|
||
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.class} | ||
Status: #{response.status} | ||
Body: #{response.body.inspect} | ||
ERROR | ||
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 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<%# locals: (type: "error", content: "") -%> | ||
<% color = type == "error" ? "red" : "blue" %> | ||
<%= content_tag :div, | ||
class: "flex justify-between bg-#{color}-50 rounded-xl p-3", | ||
data: {controller: "element-removal" }, | ||
role: type == "error" ? "alert" : "status" do %> | ||
<div class="flex gap-3 text-<%= color %>-500 items-center"> | ||
<%= lucide_icon("info", class: "w-5 h-5 shrink-0") %> | ||
<p class="text-sm"><%= content %></p> | ||
</div> | ||
<%= content_tag :a, lucide_icon("x", class: "w-5 h-5 shrink-0 text-#{color}-500"), data: { action: "click->element-removal#remove" }, class:"flex gap-1 font-medium items-center text-gray-900 px-3 py-1.5 rounded-lg cursor-pointer" %> | ||
<% 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
Rails.application.config.to_prepare do | ||
Provider::Synth.configure do |config| | ||
config.api_key = ENV["SYNTH_API_KEY"] | ||
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 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