Files
Tanami_App/lib/shared/api/network_api_services.dart
2024-07-10 17:34:49 +05:30

55 lines
1.3 KiB
Dart

// common_api.dart
import 'package:dio/dio.dart';
class NetworkApiService {
final Dio _dio = Dio();
// Common function for GET requests
Future<Response> get(String url,
{Map<String, dynamic>? queryParameters}) async {
try {
return await _dio.get(url);
} catch (e) {
throw _handleError(e);
}
}
// Common function for POST requests
Future<Response> post(String url, dynamic data) async {
try {
return await _dio.post(url, data: data);
} catch (e) {
throw _handleError(e);
}
}
// Common function for PUT requests
Future<Response> put(String url, dynamic data) async {
try {
return await _dio.put(url, data: data);
} catch (e) {
throw _handleError(e);
}
}
// Common function for DELETE requests
Future<Response> delete(String url) async {
try {
return await _dio.delete(url);
} 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
}
}
}