This is an unofficial chat room, if you want to reach out to the Faraday core team, join us on GitHub Discussions (https://github.com/lostisland/faraday/discussions) instead
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:
@handlers
to see them)This is definitely a "dirtier" solution, but unfortunately necessary if you don't control the original connection
Faraday::Connection
exposes a few attr_reader
s you can use to check these things. Check out https://github.com/lostisland/faraday/blob/main/lib/faraday/connection.rb#L21
params
are exposed as-is and middleware is stored in the builder
. As for options, there's a getter method here: https://github.com/lostisland/faraday/blob/main/lib/faraday/connection.rb#L222
Faraday::RackBuilder::Handler
objects. The reason they show as class names in the Ruby console is because we override the inspect
method (see https://github.com/lostisland/faraday/blob/main/lib/faraday/rack_builder.rb#L42). If you try calling conn.builder.handlers.first.class
you'll see the actual class and if you call conn.builder.handlers.first.build
you'll get an actual configured instance of the middleware.
headers: true
and was able to see other headers except "Authorization". Which is good actually just want to confirm. The API calls are still working so I guess you are not showing it intentionally for security reasons ?
builder.request :basic_auth, user, password
headers: true
. I suspect what is happening here is that you've put the :logger
middleware BEFORE the basic_auth
one :). The logger logs the request the moment it receives it, so if the :basic_auth
middleware is added later in the stack, it won't have added the Authorization
header to the request yet. If this is what you want, then it's a pretty good solution on how to log the request without exposing the Authorization
header :D
builder.response(:logger, nil, headers: true, bodies: true,
log_level: Rails.application.config.log_level) do |logger|
logger.filter(/(Authorization)([^&]+)/, '\1[REMOVED]')
end
filter_auth
flag to logger which does the filtering thing. I will be happy to open a PR.
filter_auth
sounds a bit too specific. Maybe a more generic method to filter headers? I’d suggest opening an issue before jumping on an implementation so we could discuss a proposal (how would that be used, how it will differ from the current filter
method, …)
net-http-persistent
team and ask them about this. In the end, Faraday is just a wrapper on the adapters, so whatever they suggest as the best approach will work in Faraday as well
@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, ...
I am using faraday with rspec for single request I am facing below issue
NoMethodError:
undefined method `register_middleware' for Faraday::Adapter:Class
Faraday::Adapter.register_middleware(net_http: Faraday::Adapter::NetHttp)
I have tried to install faraday_middleware gem but it is not found
Not sure where I am worng
Hello all!
I am new to faraday and net-http (we use the default adapter). I have a rails application and it calls many other services to fetch data using faraday gem. However, we are experiencing a lot of timeout errors and we were thinking of reducing the timeout value etc...
I wanted to try one thing, I wanted to collect few data here.
Given that I want to perform a GET API request
I want to log or print the value of the time it takes to open the connection with the server
And I also want to log or print the time it takes till the client starts or ends receiving the response back from the server
And I also want to log or print the time it takes to close the connection with the server.
(Iterating again, I am using Rails application :) ) How can I achieve this? Thanks!