lostisland / faraday

Simple, but flexible HTTP client library, with support for multiple backends.
https://lostisland.github.io/faraday
MIT License
5.74k stars 976 forks source link

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

Closed partydrone closed 3 years ago

partydrone commented 3 years ago

Basic Info

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:

jarl-dk commented 3 years ago

Proc is only a 2.x feature (and only documented for 2.x). However help is on it's way in https://github.com/lostisland/faraday/pull/1322

iMacTia commented 3 years ago

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 πŸ‘

iMacTia commented 3 years ago

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