1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-21 22:29:38 +02:00

Add source headers to Synth calls (#1062)

This commit is contained in:
Zach Gollwitzer 2024-08-05 12:21:12 -04:00 committed by GitHub
parent 9ad04a82cb
commit 6e74414cb2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 28 additions and 7 deletions

View file

@ -33,8 +33,7 @@ class Provider::Synth
def fetch_exchange_rate(from:, to:, date:) def fetch_exchange_rate(from:, to:, date:)
retrying Provider::Base.known_transient_errors do |on_last_attempt| retrying Provider::Base.known_transient_errors do |on_last_attempt|
response = Faraday.get("#{base_url}/rates/historical") do |req| response = client.get("#{base_url}/rates/historical") do |req|
req.headers["Authorization"] = "Bearer #{api_key}"
req.params["date"] = date.to_s req.params["date"] = date.to_s
req.params["from"] = from req.params["from"] = from
req.params["to"] = to req.params["to"] = to
@ -69,6 +68,22 @@ class Provider::Synth
"https://api.synthfinance.com" "https://api.synthfinance.com"
end end
def app_name
"maybe_app"
end
def app_type
Rails.application.config.app_mode
end
def client
@client ||= Faraday.new(url: base_url) do |faraday|
faraday.headers["Authorization"] = "Bearer #{api_key}"
faraday.headers["X-Source"] = app_name
faraday.headers["X-Source-Type"] = app_type
end
end
def build_error(response) def build_error(response)
Provider::Base::ProviderError.new(<<~ERROR) Provider::Base::ProviderError.new(<<~ERROR)
Failed to fetch data from #{self.class} Failed to fetch data from #{self.class}
@ -78,7 +93,7 @@ class Provider::Synth
end end
def fetch_page(url, page, params = {}) def fetch_page(url, page, params = {})
Faraday.get(url) do |req| client.get(url) do |req|
req.headers["Authorization"] = "Bearer #{api_key}" req.headers["Authorization"] = "Bearer #{api_key}"
params.each { |k, v| req.params[k.to_s] = v.to_s } params.each { |k, v| req.params[k.to_s] = v.to_s }
req.params["page"] = page req.params["page"] = page

View file

@ -17,18 +17,24 @@ class Provider::SynthTest < ActiveSupport::TestCase
end end
test "retries then provides failed response" do test "retries then provides failed response" do
Faraday.expects(:get).returns(OpenStruct.new(success?: false)).times(3) @client = mock
Faraday.stubs(:new).returns(@client)
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.current @client.expects(:get).returns(OpenStruct.new(success?: false)).times(3)
response = @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
assert_match "Failed to fetch data from Provider::Synth", response.error.message assert_match "Failed to fetch data from Provider::Synth", response.error.message
end end
test "retrying, then raising on network error" do test "retrying, then raising on network error" do
Faraday.expects(:get).raises(Faraday::TimeoutError).times(3) @client = mock
Faraday.stubs(:new).returns(@client)
@client.expects(:get).raises(Faraday::TimeoutError).times(3)
assert_raises Faraday::TimeoutError do assert_raises Faraday::TimeoutError do
@synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.current @synth.fetch_exchange_rate from: "USD", to: "MXN", date: Date.iso8601("2024-08-01")
end end
end end
end end