addDependency function
Adds a dependency to 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 add
the specified packageName
.
If the request is successful, it prints a success message. If an error occurs, it prints the error message.
Implementation
Future<void> addDependency(String projectName, String packageName) 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'];
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/add_dependency');
final Map<String, dynamic> requestBody = {
'host': host,
'port': port,
'oydaBase': oydaBase,
'user': user,
'password': password,
'package_name': packageName,
};
try {
final response = await http.post(
url,
headers: {'Content-Type': 'application/json'},
body: json.encode(requestBody),
);
if (response.statusCode == 200 || response.statusCode == 201) {
final responseBody = json.decode(response.body);
print('${responseBody['message']}');
} else {
final responseBody = json.decode(response.body);
print('Error: ${responseBody['error']}');
}
} catch (e) {
print('Error connecting to the Oydabase: $e');
}
}