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;

        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) {
        return handler.next(response); // continue
      }, onError: (DioError error, handler) async {
        // print('error=${error.response}');
        if (error.response != null) {
          if ((error.response!.requestOptions.path == 'imageRecognition/idCard' ||
              error.response!.requestOptions.path ==
                  'imageRecognition/faceVerify')) {
          } else {
            showErrorToast(
              DioException.fromDioError(error).toString(),
            );
            if (error.response!.statusCode == 400) {
              if (error.response!.data != null) {
                if (error.response!.data["token"] != null) {
                  accessToken = error.response!.data["token"];
                }
              }
            }
            return handler.next(error);
          }
        } else {
          showErrorToast(
            DioException.fromDioError(error).toString(),
          );
        }
        return handler.next(error);
      }));
  dio.options.baseUrl = UrlResources.baseUrl;
  dio.options.connectTimeout = const Duration(milliseconds: 120000);
  dio.options.receiveTimeout = const Duration(milliseconds: 120000);
  return dio;
}