Update
Logging data Try-catch function
This commit is contained in:
@@ -5,7 +5,8 @@ import 'dart:convert';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:sfm_app/product/shared/model/province_model.dart';
|
||||
import '../shared/model/province_model.dart';
|
||||
import '../utils/app_logger_utils.dart';
|
||||
import '../../feature/device_log/device_logs_model.dart';
|
||||
import '../../feature/devices/device_model.dart';
|
||||
import '../../feature/home/device_alias_model.dart';
|
||||
@@ -26,6 +27,8 @@ import '../constant/enums/local_keys_enums.dart';
|
||||
import '../network/network_manager.dart';
|
||||
|
||||
class APIServices {
|
||||
|
||||
|
||||
Map<String, String> headers = {
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json',
|
||||
@@ -52,12 +55,10 @@ class APIServices {
|
||||
return headers;
|
||||
}
|
||||
|
||||
Future<T> executeApiCall<T>(
|
||||
Future<dynamic> Function() apiCall, {
|
||||
T Function(dynamic)? parser,
|
||||
String errorMessage = 'Lỗi khi gọi API',
|
||||
T Function(int)? statusCodeHandler, // Thêm handler cho statusCode
|
||||
}) async {
|
||||
Future<T> executeApiCall<T>(Future<dynamic> Function() apiCall,
|
||||
{T Function(dynamic)? parser,
|
||||
String errorMessage = 'Lỗi khi gọi API',
|
||||
T Function(int)? statusCodeHandler}) async {
|
||||
try {
|
||||
final response = await apiCall().timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
@@ -81,11 +82,38 @@ class APIServices {
|
||||
throw Exception('Dữ liệu trả về rỗng');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
// log('Lỗi API: $e, StackTrace: $stackTrace');
|
||||
AppLoggerUtils.error("Lỗi gọi API", e, stackTrace);
|
||||
throw Exception('$errorMessage: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Most Used Function
|
||||
// Future<T> execute<T>(Future<T> Function() apiCall) async {
|
||||
// try {
|
||||
// return await apiCall();
|
||||
// } catch (e) {
|
||||
|
||||
// AppLoggerUtils.error(e.toString());
|
||||
// return Future.error(e);
|
||||
// }
|
||||
// }
|
||||
|
||||
Future<T> execute<T>(
|
||||
BuildContext context,
|
||||
Future<T> Function() apiCall, {
|
||||
bool checkMounted = true,
|
||||
}) async {
|
||||
try {
|
||||
return await apiCall();
|
||||
} catch (e) {
|
||||
if (checkMounted && !context.mounted) {
|
||||
return Future.error('Widget not mounted');
|
||||
}
|
||||
showErrorTopSnackBarCustom(context, "Lỗi hệ thống");
|
||||
return Future.error(e);
|
||||
}
|
||||
}
|
||||
|
||||
Future<String> login(String path, Map<String, dynamic> loginRequest) async {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
final headers = await getHeaders();
|
||||
@@ -229,7 +257,8 @@ class APIServices {
|
||||
);
|
||||
}
|
||||
|
||||
Future<int> updateDeviceNotificationSettings(String thingID, Map<String, int> data) async {
|
||||
Future<int> updateDeviceNotificationSettings(
|
||||
String thingID, Map<String, int> data) async {
|
||||
Map<String, dynamic> body = {"thing_id": thingID, "notifi_settings": data};
|
||||
return executeApiCall(
|
||||
() => NetworkManager.instance!.updateDataInServer(
|
||||
@@ -279,7 +308,7 @@ class APIServices {
|
||||
.getDataFromServer(APIPathConstants.PROVINCES_PATH),
|
||||
parser: (json) => Province.fromJsonDynamicList(json['items']),
|
||||
errorMessage: 'Lỗi khi GET /${APIPathConstants.PROVINCES_PATH}');
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
Future<List<Province>> getProvincesByName(String name) async {
|
||||
@@ -290,7 +319,7 @@ class APIServices {
|
||||
parser: (json) => Province.fromJsonDynamicList(json['items']),
|
||||
errorMessage: 'Lỗi khi GET /${APIPathConstants.PROVINCES_PATH}/$name',
|
||||
);
|
||||
;
|
||||
|
||||
}
|
||||
|
||||
Future<Province> getProvinceByID(String provinceID) async {
|
||||
|
||||
Reference in New Issue
Block a user