From cd83375b9873a1a10d9cc3bab730a3566d0cd2bc Mon Sep 17 00:00:00 2001 From: Ashutosh Date: Tue, 13 Aug 2024 12:32:40 +0530 Subject: [PATCH] http client class safeApiCall error state handle --- android/app/src/main/AndroidManifest.xml | 2 +- ios/Runner/Info.plist | 2 +- .../auth_module/splash_screen_controller.dart | 12 ++--- .../custom_navigation_drawer.dart | 8 ++-- lib/web_services/auth_services.dart | 32 ++++++++++--- lib/web_services/http_request_client.dart | 48 +++++++++++-------- lib/web_services/rota_services.dart | 2 +- lib/web_services/web_url.dart | 4 +- 8 files changed, 68 insertions(+), 42 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 740033b..7c9bfa1 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -8,7 +8,7 @@ + android:label="FTC Patient"> CFBundleDevelopmentRegion $(DEVELOPMENT_LANGUAGE) CFBundleDisplayName - Ftc Mobile App + FTC Patient CFBundleExecutable $(EXECUTABLE_NAME) CFBundleIdentifier diff --git a/lib/controllers/auth_module/splash_screen_controller.dart b/lib/controllers/auth_module/splash_screen_controller.dart index 1a6e6af..99a11c5 100644 --- a/lib/controllers/auth_module/splash_screen_controller.dart +++ b/lib/controllers/auth_module/splash_screen_controller.dart @@ -10,13 +10,13 @@ class SplashScreenController extends GetxController { @override void onInit() { 5.seconds.delay(() { - // _gotoLoginScreen(); + _gotoLoginScreen(); - if (_loginTokenNotFound) { - _gotoLoginScreen(); - } else { - _checkOngoingShift(); - } + // if (_loginTokenNotFound) { + // _gotoLoginScreen(); + // } else { + // _checkOngoingShift(); + // } }); super.onInit(); diff --git a/lib/view/custom_widgets/custom_navigation_drawer.dart b/lib/view/custom_widgets/custom_navigation_drawer.dart index 98f6afa..4413338 100644 --- a/lib/view/custom_widgets/custom_navigation_drawer.dart +++ b/lib/view/custom_widgets/custom_navigation_drawer.dart @@ -36,10 +36,10 @@ class _CustomDrawerState extends State { Center( child: GestureDetector( onTap: () { - drawerController.selectedIndex.value = -1; - Navigator.pop(context); - Navigator.pushNamed( - context, CustomRouteNames.kViewProfileScreenRoute); + // drawerController.selectedIndex.value = -1; + // Navigator.pop(context); + // Navigator.pushNamed( + // context, CustomRouteNames.kViewProfileScreenRoute); }, child: Obx(() { return MyCircleImage( diff --git a/lib/web_services/auth_services.dart b/lib/web_services/auth_services.dart index 1807ab7..259e4d0 100644 --- a/lib/web_services/auth_services.dart +++ b/lib/web_services/auth_services.dart @@ -1,3 +1,7 @@ +import 'dart:io'; + +import 'package:ftc_mobile_app/utilities/enums/api_method.dart'; + import '../ftc_mobile_app.dart'; class AuthService { @@ -20,27 +24,43 @@ class AuthService { Map requestBody = { "email": email, "password": password, + "role": "serviceuser", //role key h - user , serviceuser, staffmember, superadmin "fcm_tokens": { "token": "fY6_DGfGQ06OW4BUVEsEwG:APA91bGUFza2uhaaR0miN2jtY0ut7RuA5ObleZvqv2X8KLOBgksmDmgc9sHDdea-DBvHmz1aUwX1uhkysk92x50WQCLHPDD1VwGX5ybKhUwVdq4aBfI24vhXqMB-FksWXEZwzDAi9BA_", // "token": "${await FirebaseMessaging.instance.getToken()}", - "deviceType": "android" + "deviceType": Platform.isAndroid ? "android" : "ios", } }; - ResponseModel responseModel = await _httpClient.customRequest( - "POST", + // ResponseModel responseModel = await _httpClient.customRequest( + // "POST", + // url: WebUrls.signInUrl, + // requestBody: requestBody, + // requestHeader: {'Content-Type': 'application/json'}, + // ); + + ResponseModel responseModel = await _httpClient.safeApiCall( + method: ApiMethod.post, url: WebUrls.signInUrl, - requestBody: requestBody, - requestHeader: {'Content-Type': 'application/json'}, + body: requestBody, ); - if (responseModel.statusCode >= 200 && responseModel.statusCode <= 230) { + + if ((responseModel.statusCode ~/ 100) == 2) { return true; } else { return { "message": responseModel.statusDescription, }; } + + // if (responseModel.statusCode >= 200 && responseModel.statusCode <= 230) { + // return true; + // } else { + // return { + // "message": responseModel.statusDescription, + // }; + // } } //verify Otp and if success storing token and user data diff --git a/lib/web_services/http_request_client.dart b/lib/web_services/http_request_client.dart index 6daa6de..a5450d0 100644 --- a/lib/web_services/http_request_client.dart +++ b/lib/web_services/http_request_client.dart @@ -2,7 +2,6 @@ import 'dart:async'; import 'dart:convert'; import 'dart:developer'; import 'dart:io'; -import 'dart:typed_data'; import 'package:dio/dio.dart'; import 'package:flutter/foundation.dart'; import 'package:ftc_mobile_app/utilities/enums/api_method.dart'; @@ -86,6 +85,21 @@ class HttpRequestClient { } //---------------------------------------------------------------- + Future> getRequestHeader({ + bool isBearer = true, + bool isContentType = true, + }) async { + String token = LocalStorageManager.getLoginToken(); + // //todo remove this in end + // String token = + // 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhIjp0cnVlLCJuIjoiSmFtc2hhaWQgU2FiaXIiLCJlIjoiamFtc2hhaWRzYWJpcjQxMTk4MEBnbWFpbC5jb20iLCJkIjoiNjQ2NTEyN2E3MmJjNzEzY2E4NDYwY2IzIiwicCI6Ii91cGxvYWRzL2RwL2RlZmF1bHQucG5nIiwiciI6Il9hIiwiaWF0IjoxNjkzMjIzNTIzfQ.EtpS_o0kEhdlNzCyFdFNNQzHd0IipTw8BEuUBVgfYok'; + Map header = { + 'Authorization': isBearer ? 'Bearer $token' : token, + if (isContentType) 'Content-Type': 'application/json' + }; + return header; + } + Future getRequestWithOutHeader({required String url}) async { try { http.Response response = await http @@ -582,23 +596,6 @@ class HttpRequestClient { } } - Future> getRequestHeader({ - bool isBearer = true, - bool isContentType = true, - }) async { - // String token = SessionManagement().getSessionToken( - // tokenKey: SessionKeys.kUserTokenKey, - // ); - //todo remove this in end - String token = - 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJhIjp0cnVlLCJuIjoiSmFtc2hhaWQgU2FiaXIiLCJlIjoiamFtc2hhaWRzYWJpcjQxMTk4MEBnbWFpbC5jb20iLCJkIjoiNjQ2NTEyN2E3MmJjNzEzY2E4NDYwY2IzIiwicCI6Ii91cGxvYWRzL2RwL2RlZmF1bHQucG5nIiwiciI6Il9hIiwiaWF0IjoxNjkzMjIzNTIzfQ.EtpS_o0kEhdlNzCyFdFNNQzHd0IipTw8BEuUBVgfYok'; - Map header = { - 'Authorization': isBearer ? 'Bearer $token' : token, - if (isContentType) 'Content-Type': 'application/json' - }; - return header; - } - Future safeApiCall({ required ApiMethod method, required String url, @@ -615,7 +612,7 @@ class HttpRequestClient { // } Map customHeader = await getRequestHeader(); - if(headers != null) customHeader.addAll(headers!); + if (headers != null) customHeader.addAll(headers); final dio = Dio() ..interceptors.add(Logging()) @@ -660,9 +657,18 @@ class HttpRequestClient { break; } - return ResponseModel.fromJson(jsonDecode(response.toString())) - ..statusCode = response.statusCode ?? 0; + final Map map = jsonDecode(response.toString()); + final statusFail = (map['status'] is String && + (map['status'] as String).toLowerCase() == "fail"); + final status400 = (map['status'] is int && map['status'] == 400); + if (map.containsKey('status') && (statusFail || status400)) { + return ResponseModel.named( + statusCode: 0, + statusDescription: map['message'], + ); + } + return ResponseModel.fromJson(map)..statusCode = response.statusCode ?? 0; } on DioException catch (e) { return ResponseModel.named( statusCode: e.response?.statusCode ?? 0, diff --git a/lib/web_services/rota_services.dart b/lib/web_services/rota_services.dart index 1ab2a7d..7286c7c 100644 --- a/lib/web_services/rota_services.dart +++ b/lib/web_services/rota_services.dart @@ -27,7 +27,7 @@ class RotaService { Map requestBody = { "startDate": startDateMills, "endDate": endDateMills, - "staffUserId": LocalStorageManager.userId, + "serviceUserId": LocalStorageManager.userId, // "startDate": 1720983600000, // "endDate": 1721502000000, // "staffUserId": "659653a31faf0d9fa4e15d5d", diff --git a/lib/web_services/web_url.dart b/lib/web_services/web_url.dart index 3b1725d..25eda28 100644 --- a/lib/web_services/web_url.dart +++ b/lib/web_services/web_url.dart @@ -6,7 +6,7 @@ class WebUrls extends _BaseUrl { static String socketUrl = 'http://16.171.242.62:3000'; - static String signInUrl = "$baseUrl/users/signin"; + static String signInUrl = "$baseUrl/users/signInApp"; static String passwordLessSignInUrl = "$baseUrl/users/passwordLessLogin"; static String forgetPasswordUrl = "$baseUrl/users/forgot-password"; static String verifyCodeUrl = "$baseUrl/users/verify-code"; @@ -77,7 +77,7 @@ class WebUrls extends _BaseUrl { "$baseUrl/staffWorkLoads/getStaffWorkLoadsWithFullDetails"; static String requestHoliday = "$baseUrl/staffHolidayRequests/createStaffHolidayRequest"; - static String myShifts = "$baseUrl/liveRoster/checkExistShiftByStaffId"; + static String myShifts = "$baseUrl/liveRoster/checkExistShiftByServiceUserid"; static String unassignedShifts = "$baseUrl/liveRoster/unassignedShifts"; static String assignStaffToShift = "$baseUrl/liveRoster/assignStaffToShift";