ApiClient constructor

ApiClient({
  1. String? baseUrl,
  2. int? receiveTimeout,
  3. int? connectTimeout,
  4. String? screenName,
  5. String? buttonName,
  6. String? transactionId,
  7. dynamic onFetchRequestTime(
    1. String time
    )?,
  8. dynamic onFetchResponseTime(
    1. String time
    )?,
  9. bool isLogRequired = true,
})

Implementation

factory ApiClient({
  String? baseUrl,
  int? receiveTimeout,
  int? connectTimeout,
  String? screenName,
  String? buttonName,
  String? transactionId,
  Function(String time)? onFetchRequestTime,
  Function(String time)? onFetchResponseTime,
  bool isLogRequired = true,
}) {
  String requestTime = "";
  var logger = Logger(
    filter: MyFilter(),
    printer: PrettyPrinter(
      lineLength: 0,
      methodCount: 0,
      errorMethodCount: 0,
      noBoxingByDefault: true,
    ),
    output: null,
  );
  final storage = GetStorage();
  var jwt = SecureStorageService.readSecureData(
    SecureStorageService.accessToken,
  );
  var uDID = UDID.uDID;
  Dio dio = Dio();
  dio.options = BaseOptions(
    receiveTimeout: Duration(
      seconds: receiveTimeout ?? Constants.defaultTimeout,
    ),
    connectTimeout: Duration(
      seconds: connectTimeout ?? Constants.defaultTimeout,
    ),
    baseUrl: ApiConstant.baseUrl,
    followRedirects: true,
  );
  dio.options.headers["Content-Type"] = "application/json";
  dio.options.headers["Accept"] = "*/*";
  dio.options.headers["Authorization"] = "Bearer $jwt";
  dio.options.headers["x-request-txn-id"] = uDID;
  dio.options.headers["X-Request-ID"] = "tedhsjdh";
  // dio.options.headers["X-UserId"] = storage.read("XUSER_ID");
  dio.interceptors.add(
    LogInterceptor(
      request: true,
      requestBody: true,
      requestHeader: true,
      responseBody: true,
      responseHeader: true,
      error: true,
      logPrint: (object) {
        // logger.d(object);
        if (isLogRequired) {
          ConditionalLogs().customLog("$object");
        }
        // log(object.toString());
        // print(object.toString());
      },
    ),
  );
  dio.interceptors.add(
    InterceptorsWrapper(
      onRequest: (options, handler) {
        if (onFetchRequestTime != null) {
          onFetchRequestTime(DateTime.now().toString());
        }
        requestTime = DateTime.now().toString();
        return handler.next(options);
      },
      onResponse: (response, handler) async {
        if (isLogRequired) {
          ConditionalLogs().customLog("${response.data}");
        }
        if (onFetchResponseTime != null) {
          onFetchResponseTime(DateTime.now().toString());
        }
        return handler.next(response);
      },
      onError: (DioException e, handler) {
        if (onFetchResponseTime != null) {
          onFetchResponseTime(DateTime.now().toString());
        }
        if (isLogRequired) {
          Helper.logEvent(
            "ERROR_EVENT",
            failure: true,
            endPoint: e.requestOptions.path,
            requestDate: requestTime,
            responseDate: DateTime.now().toString(),
            screenName: screenName,
            buttonName: buttonName,
            error: e,
          );
        }
        return handler.next(e);
      },
    ),
  );
  return _ApiClient(dio, baseUrl: baseUrl);
}