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

95 lines
2.7 KiB
Dart
Raw Normal View History

2024-05-24 19:32:53 +05:30
// common_api.dart
import 'package:dio/dio.dart';
2024-07-11 10:51:29 +05:30
import 'package:flutter/foundation.dart';
import '../../Api_Helper/base_manager.dart';
2024-05-24 19:32:53 +05:30
class NetworkApiService {
final Dio _dio = Dio();
// Common function for GET requests
2024-07-11 10:51:29 +05:30
Future<ResponseData> get(String url,
2024-05-24 19:32:53 +05:30
{Map<String, dynamic>? queryParameters}) async {
2024-07-11 10:51:29 +05:30
if (kDebugMode) {
print("api url is >>> $url");
}
Response response;
2024-05-24 19:32:53 +05:30
try {
2024-07-11 10:51:29 +05:30
response = await _dio.get(url);
if (response.statusCode == 201 || response.statusCode == 200) {
return ResponseData<dynamic>("success", ResponseStatus.SUCCESS,
data: response.data);
} else {
try {
return ResponseData<dynamic>(
response.data['message'].toString(), ResponseStatus.FAILED);
} catch (_) {
return ResponseData<dynamic>(
data: response.data,
response.statusMessage!,
ResponseStatus.FAILED);
}}
2024-05-24 19:32:53 +05:30
} catch (e) {
2024-07-11 10:51:29 +05:30
return ResponseData<dynamic>(
"Something went wrong", ResponseStatus.FAILED);
2024-05-24 19:32:53 +05:30
}
}
// Common function for POST requests
2024-07-11 19:36:40 +05:30
Future<ResponseData> post(String url, dynamic data) async {
if (kDebugMode) {
print("data >>> $data");
print("api url is >>> $url");
}
2024-05-24 19:32:53 +05:30
try {
2024-07-11 19:36:40 +05:30
var response= await _dio.post(url, data: data);
if (response.statusCode == 201 || response.statusCode == 200) {
return ResponseData<dynamic>("success", ResponseStatus.SUCCESS,
data: response.data);
}else {
try {
return ResponseData<dynamic>(
response.data['message'].toString(), ResponseStatus.FAILED);
} catch (_) {
return ResponseData<dynamic>(
data: response.data,
response.statusMessage!,
ResponseStatus.FAILED);
}}
2024-05-24 19:32:53 +05:30
} catch (e) {
2024-07-11 19:36:40 +05:30
return ResponseData<dynamic>(
"Oops something went wrong",
ResponseStatus.FAILED);
2024-05-24 19:32:53 +05:30
}
}
// 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
}
}
}