Files
Tanami_App/lib/shared/api/network_api_services.dart

55 lines
1.3 KiB
Dart
Raw Normal View History

2024-05-24 19:32:53 +05:30
// common_api.dart
import 'package:dio/dio.dart';
class NetworkApiService {
final Dio _dio = Dio();
// Common function for GET requests
2024-07-10 17:34:49 +05:30
Future<Response> get(String url,
2024-05-24 19:32:53 +05:30
{Map<String, dynamic>? queryParameters}) async {
try {
2024-07-10 17:34:49 +05:30
return await _dio.get(url);
2024-05-24 19:32:53 +05:30
} catch (e) {
throw _handleError(e);
}
}
// Common function for POST requests
2024-07-10 17:34:49 +05:30
Future<Response> post(String url, dynamic data) async {
2024-05-24 19:32:53 +05:30
try {
2024-07-10 17:34:49 +05:30
return await _dio.post(url, data: data);
2024-05-24 19:32:53 +05:30
} catch (e) {
throw _handleError(e);
}
}
// Common function for PUT requests
2024-07-10 17:34:49 +05:30
Future<Response> put(String url, dynamic data) async {
2024-05-24 19:32:53 +05:30
try {
2024-07-10 17:34:49 +05:30
return await _dio.put(url, data: data);
2024-05-24 19:32:53 +05:30
} catch (e) {
throw _handleError(e);
}
}
// Common function for DELETE requests
2024-07-10 17:34:49 +05:30
Future<Response> delete(String url) async {
2024-05-24 19:32:53 +05:30
try {
2024-07-10 17:34:49 +05:30
return await _dio.delete(url);
2024-05-24 19:32:53 +05:30
} catch (e) {
throw _handleError(e);
}
}
// Handle Dio errors
dynamic _handleError(dynamic e) {
if (e is DioException) {
// Handle Dio specific errors (e.g., DioErrorType.connectTimeout, DioErrorType.response)
return e.message; // Or return a custom error message
} else {
return 'An error occurred'; // Generic error message for other types of errors
}
}
}