initialize method

Future<void> initialize({
  1. String? apiKey,
  2. String? environment,
  3. bool autoInitWithAdmin = true,
})

Initialize the API key service

Implementation

Future<void> initialize({
  String? apiKey,
  String? environment,
  bool autoInitWithAdmin = true,
}) async {
  try {
    OnairosDebugHelper.log('🔑 Initializing API Key Service');

    // If no API key provided and auto-init is enabled, use admin key
    if (apiKey == null && autoInitWithAdmin) {
      apiKey = ADMIN_API_KEY;
      OnairosDebugHelper.log('⚠️ Using admin API key for development - apps should provide their own key');
    }

    // Validate API key format
    if (apiKey != null && !_validateApiKeyFormat(apiKey)) {
      throw Exception('Invalid API key format: API key must be at least 20 characters long');
    }

    // Store configuration
    _currentApiKey = apiKey;
    _environment = environment ?? 'production';

    if (apiKey != null) {
      await _storage.write(key: 'onairos_api_key', value: apiKey);
    }
    await _storage.write(key: 'onairos_environment', value: _environment!);

    _isInitialized = true;
    OnairosDebugHelper.log('✅ API Key Service initialized successfully');

  } catch (e) {
    OnairosDebugHelper.log('❌ Error initializing API Key Service: $e');
    rethrow;
  }
}