apiClient function

Future<Dio> apiClient()

Implementation

Future<Dio> apiClient() async {
  final dio = Dio();
  dio.interceptors.clear();
  dio.interceptors.add(
      InterceptorsWrapper(onRequest: (RequestOptions options, handler) async {
        DeviceInfo deviceInfo = await getDeviceInfo();
        var headerData = deviceInfo.appType == "android"
            ? '{"browser":null,"browser_version":null,"deviceType":"mobile","os":"${deviceInfo.appType}","os_version":"${deviceInfo.deviceVersion}"}'
            : '{"browser":null,"browser_version":null,"deviceType":"mobile","os":"${deviceInfo.appType}","os_version":"${deviceInfo.deviceVersion}"}';

        var decodeJson = jsonDecode(headerData);
        String encodedData = jsonEncode(decodeJson);
        var token = accessToken;
        print('accessToken= ///$token///');
        //print('encodedData= $encodedData');
        // Do something before request is sent
        //var accessToken = await getAccessToken();

        options.headers['content-Type'] = 'application/json';
        options.headers["document-access-token"] = token;
        options.headers["user-device"] = encodedData;
        return handler.next(options);
      }, onResponse: (Response response, handler) {
        //print('data=${response.requestOptions.data}');
        //print('headers=${response.requestOptions.headers}');

        // Do something with response data
        return handler.next(response); // continue
        return handler.next(response); // continue
      }, onError: (DioError error, handler) async {

        // Do something with response error
        // showtoast(error.response!.data["msg"]);

         print('msg=${error.response}');
        // print('msg=${error.response?.data}');
        // print('url=${error.requestOptions.path}');
         print('error=${error.response?.statusCode}');
        if (error.response != null &&
            (error.response!.requestOptions.path == 'imageRecognition/idCard'||
                error.response!.requestOptions.path == 'imageRecognition/faceVerify')) {
           print("triggered");
        } else {
            showErrorToast(DioException.fromDioError(error).toString(),);
            if(error.response!.statusCode==400){
              if(error.response!.data != null){
                if(error.response!.data["token"] != null){
                  // print('res=${error.response!.data}///');
                  accessToken = error.response!.data["token"];
                  //print('//// token updated///');
                }

              }
            }

            return handler.next(error);
          // if(error.response!.requestOptions.path == 'imageRecognition/idCard'||
          //     error.response!.requestOptions.path == 'imageRecognition/faceVerify'){
          //   print("Ml error");
          // }else{
          //   showErrorToast(DioException.fromDioError(error).toString(),);
          //   return handler.next(error);
          // }
          // throw Exception(DioException.fromDioError(error).toString(),);

        }
        //showErrorToast(DioException.fromDioError(error).toString(),);
        return handler.next(error);
      }));
  dio.options.baseUrl = baseUrl1;
  dio.options.connectTimeout = const Duration(seconds: 30);
  dio.options.receiveTimeout = const Duration(seconds: 30);
  return dio;
}