getApiKeyFromApiKeyHelper function

Future<String?> getApiKeyFromApiKeyHelper(
  1. bool isNonInteractiveSession
)

Async fetch of API key from the configured helper command.

Implementation

Future<String?> getApiKeyFromApiKeyHelper(bool isNonInteractiveSession) async {
  if (getConfiguredApiKeyHelper() == null) return null;

  final ttl = calculateApiKeyHelperTtl();
  if (_apiKeyHelperCache != null) {
    final now = DateTime.now().millisecondsSinceEpoch;
    if (now - _apiKeyHelperCache!.timestamp < ttl) {
      return _apiKeyHelperCache!.value;
    }
    // Stale: return stale value, refresh in background.
    _apiKeyHelperInflight ??= _ApiKeyHelperInflight(
      promise: _runAndCacheApiKeyHelper(
        isNonInteractiveSession,
        false,
        _apiKeyHelperEpoch,
      ),
      startedAt: null,
    );
    return _apiKeyHelperCache!.value;
  }

  // Cold cache: deduplicate concurrent calls.
  if (_apiKeyHelperInflight != null) return _apiKeyHelperInflight!.promise;
  _apiKeyHelperInflight = _ApiKeyHelperInflight(
    promise: _runAndCacheApiKeyHelper(
      isNonInteractiveSession,
      true,
      _apiKeyHelperEpoch,
    ),
    startedAt: DateTime.now().millisecondsSinceEpoch,
  );
  return _apiKeyHelperInflight!.promise;
}