buildProxyManager method

Future<ProxyManager> buildProxyManager()

Builds a ProxyManager instance

This is the core component needed for proxy management

Implementation

Future<ProxyManager> buildProxyManager() async {
  // Initialize HTTP client if not provided
  final httpClient = _httpClient ?? http.Client();

  // Initialize SharedPreferences if not provided
  final sharedPreferences =
      _sharedPreferences ?? await SharedPreferences.getInstance();

  // Initialize data sources
  final localDataSource = ProxyLocalDataSourceImpl(
    sharedPreferences: sharedPreferences,
  );
  final remoteDataSource = ProxyRemoteDataSourceImpl(
    client: httpClient,
    sourceConfig: _sourceConfig,
  );

  // Initialize analytics service if enabled
  ProxyAnalyticsService? analyticsService;
  if (_enableAnalytics) {
    analyticsService = ProxyAnalyticsServiceImpl(
      sharedPreferences: sharedPreferences,
    );
  }

  // Initialize repository
  final repository = ProxyRepositoryImpl(
    remoteDataSource: remoteDataSource,
    localDataSource: localDataSource,
    client: httpClient,
    maxConcurrentValidations: _maxConcurrentValidations,
    sourceConfig: _sourceConfig,
    analyticsService: analyticsService,
  );

  // Initialize use cases
  final getProxies = GetProxies(repository);
  final validateProxy = ValidateProxy(repository);
  final getValidatedProxies = GetValidatedProxies(repository);

  // Initialize cache manager if needed
  ProxyCacheManager? cacheManager;
  if (_usePreloadedProxies) {
    cacheManager = ProxyCacheManager(sharedPreferences: sharedPreferences);
    await cacheManager.initialize();
  }

  // Initialize preloader service if needed
  ProxyPreloaderService? preloaderService;
  if (_usePreloadedProxies && cacheManager != null) {
    preloaderService = ProxyPreloaderService(
      repository: repository,
      cacheManager: cacheManager,
    );
  }

  if (_useAdvancedProxyManager) {
    // Create the advanced proxy manager
    final advancedProxyManager = AdvancedProxyManager(
      repository: repository,
      analyticsService: analyticsService,
      preloaderService: preloaderService,
      cacheManager: cacheManager,
      strategyType: _rotationStrategyType,
    );

    // Enable preloaded proxies if requested
    if (_usePreloadedProxies) {
      advancedProxyManager.setUsePreloadedProxies(true);
    }

    // Convert to regular proxy manager interface
    return AdvancedProxyManagerAdapter(advancedProxyManager);
  } else {
    // Initialize standard proxy manager
    final proxyManager = ProxyManager(
      getProxies: getProxies,
      validateProxy: validateProxy,
      getValidatedProxies: getValidatedProxies,
      analyticsService: analyticsService,
    );

    // Set the rotation strategy
    proxyManager.setRotationStrategy(_rotationStrategyType);

    return proxyManager;
  }
}