This is an unofficial chat room, if you want to reach out to the Faraday core team, join us on GitHub Discussions () instead
connection = Faraday.new(url: url, proxy: ENV['PROXY_URL']) do |faraday| faraday.response :logger faraday.adapter :async_http, **adapter_options end
if proxy = env.request.proxy
ProxyOptionsobject by the
Faraday::Connection(see)
env.request.proxy, this should return a
ProxyOptionsobject
http_proxy). This way you won't even need to pass it to the connection, it will be picked automatically for you!
Yeah I think that's clear, thanks for the context @MaryamAdnan3! The most immediate thing that comes to mind would be to have a "connection builder" method that returns a new faraday connection based on some parameters. Something like this:
def build_connection(retry_options: nil) Faraday.new(...) do |f| # other middleware f.request :retry, **retry_options if retry_options.present? # other middleware and adapter end end
You could then use this method to build your original Faraday connection object as well, so you won't need to copy over the handlers from one to another since the configuration will live inside this method. I'm not sure if this is possible in your specific case, but it would definitely be the cleanest way.
If you're not the one creating the original Faraday connection, then the only other option would be similar to what you're already doing, with the exception that you would need to always generate a new connection:
@handlersto see them)
This is definitely a "dirtier" solution, but unfortunately necessary if you don't control the original connection
@conn = Faraday.new(url: Sso::BASE_URL) do |faraday| # faraday.authorization :Bearer, access_token if access_token.present? faraday.request :authorization, :Bearer, access_token if access_token.present? faraday.request :json faraday.response :json faraday.adapter Faraday.default_adapter end end
:authorization, :Bearer...which was previously
:authorization :Bearer, ...