callApi method
Implementation
Future<ResponseApi> callApi(
{required EndPointData endpointData, Map<String, dynamic>? query, Map<String, dynamic>? body, Map<String, String>? replacementParams, FileModel? fileModel, String? savePath}) async {
Response response;
header?["content-length"] = endpointData.method == Methods.delete ? "-1" : "0";
final dio = Dio();
dio.interceptors.add(CurlLoggerDioInterceptor(
printOnSuccess: environment != 'RELEASE', convertFormData: false));
dio.options.baseUrl = urlBase;
dio.options.headers = header;
dio.options.followRedirects = true;
dio.options.connectTimeout = const Duration(milliseconds: 25000);
dio.options.receiveTimeout = const Duration(milliseconds: 15000);
dio.options.method = endpointData.method.name;
dio.options.queryParameters = query ?? {};
if (!skipFingerPrintValidation && (endpointData.allowedSHAFingerprints?.length ?? 0) > 0) {
dio.interceptors.add(SSLPinningInterceptor(endpointData.allowedSHAFingerprints!));
} else {
SafePrint("Skip SHA Finger Print Validation");
}
SafePrint("#### REQUEST ####");
SafePrint(urlBase, prefix: "Url Base: ");
SafePrint(header, prefix: "Header: ");
SafePrint(endpointData.path, prefix: "Path: ");
SafePrint(endpointData.method, prefix: "Method: ");
SafePrint(endpointData.endpoint, prefix: "Endpoint: ");
SafePrint(query, prefix: "Query: ");
SafePrint(body, prefix: "Body: ");
SafePrint(fileModel, prefix: "FileModel: ");
SafePrint("#### REQUEST ####");
try {
String formattedEndpoint = endpointData.endpoint;
if (replacementParams != null) {
replacementParams.forEach((key, value) {
formattedEndpoint = formattedEndpoint.replaceAll("{{$key}}", value);
});
}
if (endpointData.method == Methods.get && savePath != null) {
if(DeviceHelper.isWeb){
downloadFile(urlBase+endpointData.endpoint, header, query, savePath);
loading?.hideOverlay();
return ResponseApi(1, "Download", null, null, null);
} else {
response = await dio.download(
formattedEndpoint,
savePath,
);
}
} else {
FormData formData = FormData.fromMap({
'file': MultipartFile.fromBytes(fileModel?.fileBytes ?? <int>[],
filename: fileModel?.fileName ?? ''),
});
response = await dio.request(
formattedEndpoint,
data: fileModel != null ? formData : body,
);
}
} on DioException catch (e) {
if(e.response == null) {
SafePrint("Exception Call Api ${e.message}");
loading?.hideOverlay();
return ResponseApi(
e.response?.statusCode ?? 0,
e.response?.statusMessage ?? "",
e.response?.data,
e.toString(),
e.toString()
);
} else {
response = e.response!;
}
}
SafePrint(response.data.toString());
SafePrint(response.statusCode, prefix: "Status Code ");
SafePrint(response.statusMessage, suffix: " Status Name");
var responseApi = ResponseApi(
response.statusCode ?? 0,
response.statusMessage ?? "",
response.data is Map<String, dynamic>? ? response.data : <String, dynamic>{},
null,
response.data.toString()
);
loading?.hideOverlay();
return responseApi;
}