lostisland/faraday

Authorization middleware can't build a Bearer header from a lambda argument

partydrone opened this issue ยท 3 comments

Basic Info

  • Faraday Version: 1.7.2
  • Ruby Version: 3.0.2

Issue description

Following the Authorization Middleware documentation, I am trying to pass a lambda as an argument for a bearer token, but I get an error instead:

/usr/local/bundle/gems/faraday-1.7.2/lib/faraday/request/authorization.rb:21:in `header': Can't build an Authorization Bearerheader from #<Proc:0x00005588d84bb188 /opt/gem/lib/keap/rest/connection.rb:75 (lambda)> (ArgumentError)

Steps to reproduce

Create a class with a connection method that returns a Faraday::Client object configured with the Authorization middleware:

class Client
  def connection
    @connection ||= Faraday.new("https://api.infusionsoft.com/crm/rest/v1") do |http|
    http.headers[:accept] = "application/json, */*"

    http.request :authorization, :Bearer, -> { "my_secret_token" }
    http.request :json

    http.response :dates
    http.response :json, content_type: "application/json"

    http.adapter Faraday.default_adapter
  end
end

Create a new instance of Client and make a request:

client = Client.new

client.connection.get("account", {}, {})

To see the code in full context, check out my repo and look at:

Proc is only a 2.x feature (and only documented for 2.x). However help is on it's way in #1322

Thanks @jarl-dk for jumping on this!
I can't believe this feature was never asked before ๐Ÿ˜‚, I added it in the 2.x branch (main) while refactoring but I never expected it to be so popular!

Let's keep this open and close it once we merge #1322 ๐Ÿ‘

This is now in the 1.x branch thanks to @jarl-dk and has just been released as v1.8.0 ๐ŸŽ‰