Class: Spaceship::TokenRefreshMiddleware

Inherits:
Faraday::Middleware
  • Object
show all
Defined in:
spaceship/lib/spaceship/connect_api/token_refresh_middleware.rb

Instance Method Summary collapse

Constructor Details

#initialize(app, token) ⇒ TokenRefreshMiddleware

Returns a new instance of TokenRefreshMiddleware.



8
9
10
11
# File 'spaceship/lib/spaceship/connect_api/token_refresh_middleware.rb', line 8

def initialize(app, token)
  @token = token
  super(app)
end

Instance Method Details

#call(env) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'spaceship/lib/spaceship/connect_api/token_refresh_middleware.rb', line 13

def call(env)
  if @token.expired?
    puts("App Store Connect API token expired at #{@token.expiration}... refreshing") if Spaceship::Globals.verbose?
    @token.refresh!
  end

  env.request_headers["Authorization"] = "Bearer #{@token.text}"

  @app.call(env)
end