testConnection method

Future<bool> testConnection()

Tests the connection to the AI service.

This method makes a simple request to verify that the API key is valid and the service is accessible.

Returns true if the connection is successful, false otherwise.

Example:

final isConnected = await aiService.testConnection();
if (isConnected) {
  print('AI service is accessible');
} else {
  print('AI service is not accessible');
}

Implementation

Future<bool> testConnection() async {
  try {
    if (apiKey == null || apiKey!.isEmpty) {
      return false;
    }

    final response = await http.get(
      Uri.parse('$_baseUrl/models'),
      headers: {
        'Authorization': 'Bearer $apiKey',
      },
    );

    return response.statusCode == 200;
  } on FormatException {
    return false;
  } on Exception {
    return false;
  }
}