HttpRequestClass constructor

HttpRequestClass({
  1. String appUrl = "",
  2. bool commonEnc = false,
  3. String? storagePrefix,
  4. dynamic options,
})

Implementation

HttpRequestClass({
  String appUrl = "",
  bool commonEnc = false,
  String? storagePrefix,
  dynamic options,
}) {
  this.appUrl = appUrl;
  this.payloadEnc = Payload(appUrl);
  this.commonEncMode = commonEnc;
  this.opts = options;

  setUserAgent();

  if (storagePrefix != null) {
    prefixAccessToken = storagePrefix + "accessToken";
    prefixRefreshToken = storagePrefix + "refreshToken";
    prefixExpiryToken = storagePrefix + "expiry";
  }

  dio.interceptors.add(InterceptorsWrapper(onRequest: (options, handler) {
    //check expiry
    var auth = options.headers.containsKey("Authorization");
    if (auth) {
      try {
        // print(options.headers.keys);
        var req = checkExpiry(options, handler);
      } on DioError catch (e) {
        // print(e);
      }
    } else {
      return handler.next(options);
    }

    // Do something before request is sent
    // If you want to resolve the request with some custom data,
    // you can resolve a `Response` object eg: `handler.resolve(response)`.
    // If you want to reject the request with a error message,
    // you can reject a `DioError` object eg: `handler.reject(dioError)`
  }, onResponse: (response, handler) {
    // Do something with response data
    // print(response);
    return handler.next(response); // continue
    // If you want to reject the request with a error message,
    // you can reject a `DioError` object eg: `handler.reject(dioError)`
  }, onError: (DioError e, handler) {
    // Do something with response error
    var code = e.response?.statusCode;
    // print(code);
    if (code == 401) {
      var title = e.response!.data["errors"]![0];
      if (title["code"] == "IVACC") {
        // print(e.response!.data);
        handleError(e, handler);
      } else {
        return handler.next(e); //continue
      }
    } else {
      return handler.next(e); //continue
      // If you want to resolve the request with some custom data,
      // you can resolve a `Response` object eg: `handler.resolve(response)`.
    }
  }));
}