fetchDatabaseSwagger function

Future<DatabaseSwagger?> fetchDatabaseSwagger(
  1. String url,
  2. String anonKey,
  3. Map<String, List<String>> mapOfEnums,
  4. bool jsonbToDynamic,
)

Implementation

Future<DatabaseSwagger?> fetchDatabaseSwagger(String url, String anonKey,
    Map<String, List<String>> mapOfEnums, bool jsonbToDynamic) async {
  if (!url.startsWith("http://") && !url.startsWith("https://")) {
    url = "https://$url"; // Default to HTTPS if no scheme is provided
  }

  try {
    // First attempt with API key
    final response = await _secureRequest('$url/rest/v1/?apikey=$anonKey');
    if (response.statusCode == 200) {
      return DatabaseSwagger.fromJson(
          jsonDecode(response.body), mapOfEnums, jsonbToDynamic);
    }

    // Second attempt without API key
    print("Trying without the API key...");
    final response2 = await _secureRequest('$url/rest/v1/');
    if (response2.statusCode == 200) {
      return DatabaseSwagger.fromJson(
          jsonDecode(response2.body), mapOfEnums, jsonbToDynamic);
    }

    print(
        "Failed to fetch Supabase Swagger. Status code: ${response2.statusCode}");
    print("Response body: ${response2.body}");
  } catch (e) {
    print("Error fetching Supabase Swagger: $e");
  }

  return null;
}