Files
Tanami_App/lib/shared/api/network_api_services.dart
2024-05-24 19:32:53 +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 endpoint,
{Map<String, dynamic>? queryParameters}) async {
try {
return await _dio.get(endpoint, queryParameters: queryParameters);
} catch (e) {
throw _handleError(e);
}
}
// Common function for POST requests
Future<Response> post(String endpoint, dynamic data) async {
try {
return await _dio.post(endpoint, data: data);
} catch (e) {
throw _handleError(e);
}
}
// Common function for PUT requests
Future<Response> put(String endpoint, dynamic data) async {
try {
return await _dio.put(endpoint, data: data);
} catch (e) {
throw _handleError(e);
}
}
// Common function for DELETE requests
Future<Response> delete(String endpoint) async {
try {
return await _dio.delete(endpoint);
} 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
}
}
}