You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
61 lines
2.3 KiB
Dart
61 lines
2.3 KiB
Dart
|
1 month ago
|
import 'package:dio/dio.dart';
|
||
|
|
import 'package:uco_mobile_poc/app/core/config/server_response.dart';
|
||
|
|
|
||
|
|
import '../constants/translation_keys.dart';
|
||
|
|
|
||
|
|
|
||
|
|
|
||
|
|
class ApiUtils {
|
||
|
|
static bool isHeaderRequired = false;
|
||
|
|
|
||
|
|
static ServerResponse parseServerResponse(Response response) {
|
||
|
|
return ServerResponse(response.data.toString().isEmpty ? {} : response.data, false, "", "", response.statusCode!);
|
||
|
|
}
|
||
|
|
// static void parseError(ServerResponse responseModel, Response response) {
|
||
|
|
// Map<String, dynamic> errorResponse = response.data;
|
||
|
|
// String errCode = errorResponse.containsKey("errorCode") ? errorResponse["errorCode"] : "";
|
||
|
|
// dynamic arguments = errorResponse.containsKey("arguments") ? errorResponse["arguments"] : [];
|
||
|
|
// responseModel.setErrorMsg(replaceArguments(arguments, TranslationKeys.makeTranslation(errCode)));
|
||
|
|
// responseModel.setErrorCode(errCode);
|
||
|
|
// responseModel.setResponse(response.data);
|
||
|
|
// }
|
||
|
|
static void parseError(ServerResponse responseModel, Response response) {
|
||
|
|
dynamic data = response.data;
|
||
|
|
Map<String, dynamic> errorResponse = {};
|
||
|
|
if (data is Map<String, dynamic>) {
|
||
|
|
errorResponse = data;
|
||
|
|
}
|
||
|
|
else if (data is String) {
|
||
|
|
responseModel.setErrorMsg(data);
|
||
|
|
responseModel.setErrorCode("");
|
||
|
|
responseModel.setResponse(data);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
else {
|
||
|
|
responseModel.setErrorMsg("Something went wrong");
|
||
|
|
responseModel.setErrorCode("");
|
||
|
|
responseModel.setResponse(data);
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
String errCode = errorResponse.containsKey("errorCode") ? errorResponse["errorCode"] ?? "" : "";
|
||
|
|
dynamic arguments = errorResponse.containsKey("arguments") ? errorResponse["arguments"] ?? [] : [];
|
||
|
|
responseModel.setErrorMsg(replaceArguments(arguments, TranslationKeys.makeTranslation(errCode),),);
|
||
|
|
responseModel.setErrorCode(errCode);
|
||
|
|
responseModel.setResponse(data);
|
||
|
|
}
|
||
|
|
|
||
|
|
|
||
|
|
static String replaceArguments(List<dynamic>? argumentsArray, String errorString) {
|
||
|
|
if (argumentsArray == null){
|
||
|
|
return errorString;
|
||
|
|
}
|
||
|
|
for (int i = 0; i < argumentsArray.length; i++) {
|
||
|
|
int indexToReplace = i + 1;
|
||
|
|
String valueToReplace = "\\{\\{*value$indexToReplace\\}\\}";
|
||
|
|
String replacement = (argumentsArray[i] ?? "").toString();
|
||
|
|
errorString = errorString.replaceAll(RegExp(valueToReplace), replacement);
|
||
|
|
}
|
||
|
|
return errorString;
|
||
|
|
}
|
||
|
|
}
|