1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-08-05 05:25:24 +02:00

Use faraday retry, move retry logic to concrete provider level (#2042)

This commit is contained in:
Zach Gollwitzer 2025-04-01 08:41:49 -04:00 committed by GitHub
parent 0a17b84566
commit 939244bd3e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 50 additions and 90 deletions

View file

@ -1,6 +1,4 @@
class Provider
include Retryable
Response = Data.define(:success?, :data, :error)
class Error < StandardError
@ -23,17 +21,8 @@ class Provider
PaginatedData = Data.define(:paginated, :first_page, :total_pages)
UsageData = Data.define(:used, :limit, :utilization, :plan)
# Subclasses can specify errors that can be retried
def retryable_errors
[]
end
def with_provider_response(retries: default_retries, error_transformer: nil, &block)
data = if retries > 0
retrying(retryable_errors, max_retries: retries) { yield }
else
yield
end
def with_provider_response(error_transformer: nil, &block)
data = yield
Response.new(
success?: true,
@ -67,9 +56,4 @@ class Provider
self.class::Error.new(error.message)
end
end
# Override to set class-level number of retries for methods using `with_provider_response`
def default_retries
0
end
end

View file

@ -39,7 +39,7 @@ class Provider::Synth < Provider
# ================================
def fetch_exchange_rate(from:, to:, date:)
with_provider_response retries: 2 do
with_provider_response do
response = client.get("#{base_url}/rates/historical") do |req|
req.params["date"] = date.to_s
req.params["from"] = from
@ -53,7 +53,7 @@ class Provider::Synth < Provider
end
def fetch_exchange_rates(from:, to:, start_date:, end_date:)
with_provider_response retries: 1 do
with_provider_response do
data = paginate(
"#{base_url}/rates/historical-range",
from: from,
@ -128,7 +128,7 @@ class Provider::Synth < Provider
end
def fetch_security_prices(security, start_date:, end_date:)
with_provider_response retries: 1 do
with_provider_response do
params = {
start_date: start_date,
end_date: end_date
@ -191,14 +191,6 @@ class Provider::Synth < Provider
TransactionEnrichmentData = Data.define(:name, :icon_url, :category)
def retryable_errors
[
Faraday::TimeoutError,
Faraday::ConnectionFailed,
Faraday::SSLError
]
end
def base_url
ENV["SYNTH_URL"] || "https://api.synthfinance.com"
end
@ -213,6 +205,13 @@ class Provider::Synth < Provider
def client
@client ||= Faraday.new(url: base_url) do |faraday|
faraday.request(:retry, {
max: 2,
interval: 0.05,
interval_randomness: 0.5,
backoff_factor: 2
})
faraday.response :raise_error
faraday.headers["Authorization"] = "Bearer #{api_key}"
faraday.headers["X-Source"] = app_name