getDependencies function

Future<List<String>> getDependencies(
  1. String? projectName
)

Fetches the dependencies from the Oydabase.

The function loads environment variables from the .env file to get the necessary connection parameters. It then sends a POST request to the API endpoint to fetch the dependencies.

If the request is successful, it returns a list of dependencies as strings. If an error occurs, it prints the error message and returns an empty list.

Implementation

Future<List<String>> getDependencies(String? projectName) async {
  var env = DotEnv(includePlatformEnvironment: true);
  env.load(['$projectName/.env']);

  String? host = env['HOST'];
  int? port = int.tryParse(env['PORT']!);
  String? oydaBase = env['OYDABASE'];
  String? user = env['USER'];
  String? password = env['PASSWORD'];

  print('Getting dependencies from $host:$port/$oydaBase');

  if (host == null ||
      port == null ||
      oydaBase == null ||
      user == null ||
      password == null) {
    print('Error: Missing required connection parameters.');
    return [];
  }

  final url =
      Uri.parse('https://oydabackend.azurewebsites.net/api/get_dependencies');
  final Map<String, dynamic> requestBody = {
    'host': host,
    'port': port,
    'oydaBase': oydaBase,
    'user': user,
    'password': password,
  };
  try {
    final response = await http.post(
      url,
      headers: {'Content-Type': 'application/json'},
      body: json.encode(requestBody),
    );

    if (response.statusCode == 200) {
      final responseBody = json.decode(response.body);
      return List<String>.from(responseBody['dependencies']);
    } else {
      final responseBody = json.decode(response.body);
      print('Error: ${responseBody['error']}');
      return [];
    }
  } catch (e) {
    print('Error connecting to the Oydabase: $e');
    return [];
  }
}