fetchDatabaseSwagger function
Implementation
Future<DatabaseSwagger?> fetchDatabaseSwagger(String url, String apiKey,
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
// Use both query param (for compatibility) and headers (new standard)
final response = await _secureRequest('$url/rest/v1/', apiKey);
if (response.statusCode == 200) {
return DatabaseSwagger.fromJson(
jsonDecode(response.body), mapOfEnums, jsonbToDynamic);
}
// Second attempt without API key (for open/public APIs)
print("Trying without the API key...");
final response2 = await _secureRequestNoAuth('$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;
}