From ce81ee62c214468571fd264fc499adde82b21983 Mon Sep 17 00:00:00 2001 From: Sikander Saleem Date: Thu, 30 Jan 2025 11:14:20 +0300 Subject: [PATCH] Api 2.0 changes cont. --- lib/api/api_client.dart | 46 ++++++++++++--------------- lib/api/login_api_client.dart | 2 +- lib/ui/login/verify_login_screen.dart | 34 ++++++++++---------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/lib/api/api_client.dart b/lib/api/api_client.dart index 08b3124..6792abc 100644 --- a/lib/api/api_client.dart +++ b/lib/api/api_client.dart @@ -16,15 +16,11 @@ class APIError { int? errorCode; int? errorType; String? errorMessage; - int? errorStatusCode; + int? errorStatusCode; + APIError(this.errorCode, this.errorMessage, this.errorType, this.errorStatusCode); - Map toJson() => { - 'errorCode': errorCode, - 'errorMessage': errorMessage, - 'errorType': errorType, - 'ErrorStatusCode': errorStatusCode - }; + Map toJson() => {'errorCode': errorCode, 'errorMessage': errorMessage, 'errorType': errorType, 'ErrorStatusCode': errorStatusCode}; @override String toString() { @@ -46,7 +42,7 @@ APIException _throwAPIException(Response response) { APIError? apiError; if (response.body != null && response.body.isNotEmpty) { var jsonError = jsonDecode(response.body); - apiError = APIError(jsonError['ErrorCode'], jsonError['ErrorMessage'], jsonError['ErrorType'],jsonError['ErrorStatusCode']); + apiError = APIError(jsonError['ErrorCode'], jsonError['ErrorMessage'], jsonError['ErrorType'], jsonError['ErrorStatusCode']); } return APIException(APIException.BAD_REQUEST, error: apiError); case 401: @@ -73,7 +69,7 @@ class ApiClient { factory ApiClient() => _instance; Future postJsonForObject(FactoryConstructor factoryConstructor, String url, T jsonObject, - {String? token, Map? queryParameters, Map? headers, int retryTimes = 0, bool isFormData = false}) async { + {String? token, Map? queryParameters, Map? headers, int retryTimes = 0, bool isFormData = false, bool returnJsonData = true}) async { var _headers = {'Accept': 'application/json'}; if (headers != null && headers.isNotEmpty) { _headers.addAll(headers); @@ -84,22 +80,22 @@ class ApiClient { print("body:$bodyJson"); } var response = await postJsonForResponse(url, jsonObject, token: token, queryParameters: queryParameters, headers: _headers, retryTimes: retryTimes, isFormData: isFormData); - // try { - if (!kReleaseMode) { - logger.i("res: " + response.body); - } + // try { + if (!kReleaseMode) { + logger.i("res: " + response.body); + } - var jsonData = jsonDecode(response.body); - if (jsonData["IsAuthenticated"] != null) { - AppState().setIsAuthenticated = jsonData["IsAuthenticated"]; - } - if (jsonData["ErrorMessage"] == null) { - return factoryConstructor(jsonData["Data"]); - } else { - APIError? apiError; - apiError = APIError(jsonData['ErrorCode'], jsonData['ErrorEndUserMessage'], jsonData['ErrorType'] ?? 0, jsonData['ErrorStatusCode']); - throw APIException(APIException.BAD_REQUEST, error: apiError); - } + var jsonData = jsonDecode(response.body); + if (jsonData["IsAuthenticated"] != null) { + AppState().setIsAuthenticated = jsonData["IsAuthenticated"]; + } + if (jsonData["ErrorMessage"] == null) { + return factoryConstructor(returnJsonData ? jsonData["Data"] : jsonData); + } else { + APIError? apiError; + apiError = APIError(jsonData['ErrorCode'], jsonData['ErrorEndUserMessage'], jsonData['ErrorType'] ?? 0, jsonData['ErrorStatusCode']); + throw APIException(APIException.BAD_REQUEST, error: apiError); + } // } catch (ex) { // if (ex is APIException) { // rethrow; @@ -147,7 +143,7 @@ class ApiClient { } var response = await _post(Uri.parse(url), body: requestBody, headers: _headers).timeout(Duration(seconds: 120)); - if (response. statusCode >= 200 && response.statusCode < 300) { + if (response.statusCode >= 200 && response.statusCode < 300) { return response; } else { throw _throwAPIException(response); diff --git a/lib/api/login_api_client.dart b/lib/api/login_api_client.dart index 406a240..dc44bb3 100644 --- a/lib/api/login_api_client.dart +++ b/lib/api/login_api_client.dart @@ -55,7 +55,7 @@ class LoginApiClient { String url = "${ApiConsts.authRest}CheckMobileAppVersion"; Map postParams = {}; postParams.addAll(AppState().postParamsJson); - return await ApiClient().postJsonForObject((json) => CheckMobileAppVersionModel.fromJson(json), url, postParams); + return await ApiClient().postJsonForObject((json) => CheckMobileAppVersionModel.fromJson(json), url, postParams,returnJsonData: false); } Future memberLogin(String username, String password) async { diff --git a/lib/ui/login/verify_login_screen.dart b/lib/ui/login/verify_login_screen.dart index ac3c454..78266e3 100644 --- a/lib/ui/login/verify_login_screen.dart +++ b/lib/ui/login/verify_login_screen.dart @@ -619,7 +619,7 @@ class _VerifyLoginScreenState extends State { int.tryParse(AppState().memberLoginList?.pMOBILENUMBER ?? ""), (value, TextEditingController _pinPutController) async { Utils.showLoading(context); - try { + //try { GenericResponseModel? genericResponseModel = await LoginApiClient().checkActivationCode(true, AppState().memberLoginList?.pMOBILENUMBER, value, AppState().getUserName); GenericResponseModel? genericResponseModel1 = await LoginApiClient().insertMobileLoginInfoNEW( AppState().memberLoginList?.pEMAILADDRESS ?? "", @@ -648,22 +648,22 @@ class _VerifyLoginScreenState extends State { Utils.hideLoading(context); Navigator.pop(context); Navigator.pushNamedAndRemoveUntil(context, AppRoutes.dashboard, (Route route) => false); - } catch (ex) { - print(ex); - _pinPutController.clear(); - otpFieldClear.value = ""; - Utils.hideLoading(context); - Utils.handleException(ex, context, null); - dynamic errorCode = ex; - if(errorCode.error.errorStatusCode ==699){ - Future.delayed(const Duration(seconds: 2), () - { - Navigator.pop(context); - Navigator.pushNamedAndRemoveUntil( - context, AppRoutes.login, (Route route) => false); - }); - } - } + // } catch (ex) { + // print(ex); + // _pinPutController.clear(); + // otpFieldClear.value = ""; + // Utils.hideLoading(context); + // Utils.handleException(ex, context, null); + // dynamic errorCode = ex; + // if(errorCode.error.errorStatusCode ==699){ + // Future.delayed(const Duration(seconds: 2), () + // { + // Navigator.pop(context); + // Navigator.pushNamedAndRemoveUntil( + // context, AppRoutes.login, (Route route) => false); + // }); + // } + // } }, () => { Navigator.pop(context),