librato / librato-metrics

Ruby wrapper to make it easy to interact with Librato's API.
https://librato.com
Other
108 stars 51 forks source link

[1.x] Avoid deprecation warning about #basic_auth on Faraday::Connection #149

Open olleolleolle opened 2 years ago

olleolleolle commented 2 years ago

This PR fixes a deprecation warning about Faraday.

Background

The #basic_auth helper method will be removed in 2.0, and this changes the code to use the middleware itself instead.

This implements the same thing as https://github.com/librato/librato-metrics/pull/147, but for librato-metrics 1.x.

olleolleolle commented 2 years ago

Workaround 🔧 in my Rails initializer: Mention the class, to have it autoload, then open it, and redefine the method.

# Avoid Deprecation warning from Faraday about the #basic_auth method
# being removed in Faraday 2.0.
#
# PR towards 1.x: https://github.com/librato/librato-metrics/pull/149
Librato::Metrics::Connection
module Librato
  module Metrics
    class Connection
      def transport
        raise(NoClientProvided, "No client provided.") unless @client

        @transport ||= Faraday::Connection.new(
          url: api_endpoint + "/v1/",
          request: { open_timeout: 20, timeout: 30 }) do |f|

          f.use Librato::Metrics::Middleware::RequestBody
          f.use Librato::Metrics::Middleware::Retry
          f.use Librato::Metrics::Middleware::CountRequests
          f.use Librato::Metrics::Middleware::ExpectsStatus

          f.adapter @adapter || Metrics.faraday_adapter
          f.proxy @proxy if @proxy
        end.tap do |transport|
          transport.headers[:user_agent] = user_agent
          transport.headers[:content_type] = "application/json"
          # transport.basic_auth @client.email, @client.api_key # This was the issue
          transport.request :basic_auth, @client.email, @client.api_key # This is the change
        end
      end
    end
  end
end