Class: HighLevel::TokenRefresher

Inherits:
Object
  • Object
show all
Defined in:
lib/high_level/token_refresher.rb

Overview

Encapsulates the 401-recovery policy: refresh the access token via the session’s refresh_token, or fall back to deriving a fresh location token from the company’s agency token. Behaviorally matches vendor/highlevel-api-sdk/lib/HighLevel.ts (‘fetchToken` + `handleLocationTokenFallback`).

Constant Summary collapse

EXPIRY_BUFFER_MS =

A token within this many milliseconds of expiry is treated as already expired and refreshed proactively.

60_000

Instance Method Summary collapse

Constructor Details

#initialize(config:, oauth:, storage:) ⇒ TokenRefresher

Returns a new instance of TokenRefresher.



14
15
16
17
18
# File 'lib/high_level/token_refresher.rb', line 14

def initialize(config:, oauth:, storage:)
  @config = config
  @oauth = oauth
  @storage = storage
end

Instance Method Details

#refresh_for(resource_id:) ⇒ Object

Returns the new access_token string, or nil when no recovery is possible (no session, no refresh_token, refresh fails and no fallback applies).



23
24
25
26
27
28
29
30
# File 'lib/high_level/token_refresher.rb', line 23

def refresh_for(resource_id:)
  return nil if @storage.nil? || resource_id.nil? || resource_id.to_s.empty?

  session = @storage.get_session(resource_id)
  return nil if session.nil?

  direct_refresh(resource_id, session) || fallback(resource_id, session)
end