configureHttpClientAdapter static method

void configureHttpClientAdapter(
  1. Dio dio,
  2. LLMConfig config
)

Configure HTTP client adapter with proxy and SSL settings IO platform implementation using IOHttpClientAdapter

Implementation

static void configureHttpClientAdapter(Dio dio, LLMConfig config) {
  final proxyUrl = config.getExtension<String>('httpProxy');
  final bypassSSL =
      config.getExtension<bool>('bypassSSLVerification') ?? false;
  final certificatePath = config.getExtension<String>('sslCertificate');

  // Only configure adapter if any HTTP client settings are specified
  if ((proxyUrl != null && proxyUrl.isNotEmpty) ||
      bypassSSL ||
      (certificatePath != null && certificatePath.isNotEmpty)) {
    if (proxyUrl != null) {
      _logger.info('Configuring HTTP proxy: $proxyUrl');
    }
    if (bypassSSL) {
      _logger.warning('⚠️ SSL certificate verification is disabled');
    }
    if (certificatePath != null) {
      _logger.info('Loading SSL certificate from: $certificatePath');
    }

    // Set a new IOHttpClientAdapter with combined configuration
    dio.httpClientAdapter = IOHttpClientAdapter(
      createHttpClient: () {
        final client = HttpClient();

        // Configure proxy if specified
        if (proxyUrl != null && proxyUrl.isNotEmpty) {
          client.findProxy = (uri) {
            return "PROXY $proxyUrl";
          };
        }

        // Configure SSL settings if specified
        if (bypassSSL) {
          client.badCertificateCallback = (cert, host, port) => true;
        }

        if (certificatePath != null && certificatePath.isNotEmpty) {
          try {
            final certFile = File(certificatePath);
            if (certFile.existsSync()) {
              // Note: This is a simplified example. In practice, you might need
              // more sophisticated certificate handling depending on the format.
              // For now, we just log that the certificate file exists
              _logger.info('SSL certificate loaded successfully');
            } else {
              _logger.warning(
                  'SSL certificate file not found: $certificatePath');
            }
          } catch (e) {
            _logger.severe('Failed to load SSL certificate: $e');
          }
        }

        return client;
      },
    );
  }
}