getProxyWithProtocol method

Future<Proxy?> getProxyWithProtocol(
  1. ProxyProtocol protocol
)

Gets a proxy with the specified protocol

Implementation

Future<Proxy?> getProxyWithProtocol(ProxyProtocol protocol) async {
  if (_validatedProxies.isEmpty) {
    try {
      await getValidatedProxies();
    } catch (e) {
      // If we can't get validated proxies, return null
      return null;
    }
  }

  // Find a proxy with the specified protocol
  final matchingProxies =
      _validatedProxies.where((p) => p.protocol == protocol).toList();
  if (matchingProxies.isEmpty) {
    return null;
  }

  // Use the rotation strategy to select from the matching proxies
  final tempStrategy = RotationStrategyFactory.createStrategy(
    type: _strategyType,
    proxies: matchingProxies,
  );

  final proxy = tempStrategy.getNextProxy();

  // Track proxy usage
  if (proxy != null && _analyticsService != null) {
    await _analyticsService.recordRequest(proxy, true, null, 'usage');
  }

  return proxy;
}