update
feat(api_service): Update try-catch funtion and handle exception update(loading_animation): Update loading animation using Lottie
This commit is contained in:
@@ -1,3 +1,4 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
@@ -12,6 +13,7 @@ class NetworkManager {
|
||||
NetworkManager._init();
|
||||
static NetworkManager? _instance;
|
||||
static NetworkManager? get instance => _instance ??= NetworkManager._init();
|
||||
|
||||
|
||||
Future<Map<String, String>> getHeaders() async {
|
||||
String? token =
|
||||
@@ -35,15 +37,24 @@ class NetworkManager {
|
||||
/// [String] if the request is successful (status code 200), or an empty
|
||||
/// string if the request fails
|
||||
Future<String> getDataFromServer(String path) async {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] GET url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http.get(url, headers: headers);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.body;
|
||||
} else {
|
||||
return "";
|
||||
try {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] GET url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http.get(url, headers: headers).timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
onTimeout: () =>
|
||||
throw TimeoutException('Yêu cầu GET hết thời gian'),
|
||||
);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.body;
|
||||
} else {
|
||||
throw Exception('Lỗi server: ${response.statusCode}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
log('Lỗi khi lấy dữ liệu: $e, StackTrace: $stackTrace');
|
||||
throw Exception('Lỗi khi lấy dữ liệu: $e');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,6 +71,25 @@ class NetworkManager {
|
||||
/// Returns a [Future<String>] containing the server response body.
|
||||
Future<String> getDataFromServerWithParams(
|
||||
String path, Map<String, dynamic> params) async {
|
||||
try {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path, params);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] GET Params url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http.get(url, headers: headers).timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
onTimeout: () =>
|
||||
throw TimeoutException('Yêu cầu GET+PARAM hết thời gian'),
|
||||
);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.body;
|
||||
} else {
|
||||
throw Exception('Lỗi server: ${response.statusCode}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
log('Lỗi khi lấy dữ liệu: $e, StackTrace: $stackTrace');
|
||||
throw Exception('Lỗi khi lấy dữ liệu: $e');
|
||||
}
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path, params);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] GET Params url: $url");
|
||||
final headers = await getHeaders();
|
||||
@@ -77,12 +107,27 @@ class NetworkManager {
|
||||
/// [path] is the endpoint for the request, and [body] contains the data
|
||||
/// to be sent. Returns the HTTP status code of the response.
|
||||
Future<int> createDataInServer(String path, Map<String, dynamic> body) async {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] POST url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response =
|
||||
await http.post(url, headers: headers, body: jsonEncode(body));
|
||||
return response.statusCode;
|
||||
try {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] POST url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http
|
||||
.post(url, headers: headers, body: jsonEncode(body))
|
||||
.timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
onTimeout: () =>
|
||||
throw TimeoutException('Yêu cầu POST hết thời gian'),
|
||||
);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.statusCode;
|
||||
} else {
|
||||
throw Exception('Lỗi server: ${response.statusCode}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
log('Lỗi khi lấy dữ liệu: $e, StackTrace: $stackTrace');
|
||||
throw Exception('Lỗi khi lấy dữ liệu: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Updates existing data on the server using a PUT request.
|
||||
@@ -90,12 +135,26 @@ class NetworkManager {
|
||||
/// [path] is the endpoint for the request, and [body] contains the data
|
||||
/// to be updated. Returns the HTTP status code of the response.
|
||||
Future<int> updateDataInServer(String path, Map<String, dynamic> body) async {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] PUT url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response =
|
||||
await http.put(url, headers: headers, body: jsonEncode(body));
|
||||
return response.statusCode;
|
||||
try {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] PUT url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response =
|
||||
await http.put(url, headers: headers, body: jsonEncode(body)).timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
onTimeout: () =>
|
||||
throw TimeoutException('Yêu cầu PUT hết thời gian'),
|
||||
);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.statusCode;
|
||||
} else {
|
||||
throw Exception('Lỗi server: ${response.statusCode}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
log('Lỗi khi lấy dữ liệu: $e, StackTrace: $stackTrace');
|
||||
throw Exception('Lỗi khi lấy dữ liệu: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// Deletes data from the server using a DELETE request.
|
||||
@@ -105,10 +164,24 @@ class NetworkManager {
|
||||
/// A status code of 200 indicates success, while other codes indicate
|
||||
/// failure or an error.
|
||||
Future<int> deleteDataInServer(String path) async {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] DELETE url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http.delete(url, headers: headers);
|
||||
return response.statusCode;
|
||||
try {
|
||||
final url = Uri.https(ApplicationConstants.DOMAIN, path);
|
||||
log("[${DateTime.now().toLocal().toString().split(' ')[1]}] DELETE url: $url");
|
||||
final headers = await getHeaders();
|
||||
final response = await http.delete(url, headers: headers).timeout(
|
||||
Duration(seconds: ApplicationConstants.CALL_API_TIMEOUT),
|
||||
onTimeout: () =>
|
||||
throw TimeoutException('Yêu cầu DELETE hết thời gian'),
|
||||
);
|
||||
if (response.statusCode == StatusCodeConstants.OK ||
|
||||
response.statusCode == StatusCodeConstants.CREATED) {
|
||||
return response.statusCode;
|
||||
} else {
|
||||
throw Exception('Lỗi server: ${response.statusCode}');
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
log('Lỗi khi lấy dữ liệu: $e, StackTrace: $stackTrace');
|
||||
throw Exception('Lỗi khi lấy dữ liệu: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user