plaid / plaid-ruby

Ruby bindings for Plaid
https://plaid.com/docs
MIT License
228 stars 144 forks source link

Feature request - ability to use custom faraday middleware #424

Closed daisy1754 closed 2 years ago

daisy1754 commented 2 years ago

In our application, we use inhouse faraday middleware called RequestMetricsMiddleware to instrument all outgoing request. It would be great if user of library can use custom middleware like below

Plaid::Client.new(config) do |builder|
  builder.use RequestMetricsMiddleware
end
vorpus commented 2 years ago

Hi - this will be added in the next release. Will post in this thread once it's released.

configuration = Plaid::Configuration.new
api_client = Plaid::ApiClient.new(configuration)
api_client.create_connection do |builder|
  builder.use Faraday::Response::Logger
end
ctlevi commented 2 years ago

Just in case you need another example, I believe my team is also blocked on upgrading from v13 to v15 because of this. Here's is our setup:

client = ::Plaid::Client.new(config) do |builder|
  # Log responses, which will include the request_id needed by Plaid support.
  # This needs to come before the Plaid connection setup below because that will set
  # the adapter, which must be the last middleware in Faraday, otherwise it complains.
  builder.use :instrumentation, name: 'request.plaid'
  ::Plaid::Client.build_default_connection(builder)
  # Set a custom timeout that may be configured differently between web & background requests.
  builder.options[:timeout] = Rails.configuration.x.plaid_request_timeout
end
vorpus commented 2 years ago

Hi sorry for the delayed response It looks like the change I mentioned above is live, could you let me know if this works for your use case? To use custom middleware, reinitialize the faraday connection object.

ctlevi commented 2 years ago

@vorpus Yes the new documentation in the README to use create_connection worked great for me.

daisy1754 commented 2 years ago

works for me as well, thanks for update

jpriollaud commented 2 years ago

@vorpus maybe provide some more examples similar to that of @ctlevi to help people through the upgrade path

    intitialized_client = Plaid::ApiClient.new(configuration)

    intitialized_client.create_connection do |builder|
      builder.response :logger, logger
    end

    @client ||= Plaid::PlaidApi.new(intitialized_client)