executeFuture method

Future<String?> executeFuture(
  1. String _method,
  2. SellonMethod sellonMethod,
  3. Parameter _parameter,
  4. OnSuccessListener _onSuccessListener, {
  5. bool? encryption,
  6. bool? indonesiaOnly,
})

Implementation

Future<String?> executeFuture(String _method, SellonMethod sellonMethod,
    Parameter _parameter, OnSuccessListener _onSuccessListener,
    {bool? encryption, bool? indonesiaOnly}) async {

  // print("paaa ${_parameter.getData()}");

  if (sellonMethod == SellonMethod.get) {
    var requestParams = "";
    var index = 0;

    _parameter.data?.forEach((key, val) {
      index++;
      requestParams +=
          "$key=$val${index == _parameter.data?.length ? '' : '&'}";
    });
    if (Config.openApi) {
      print("request ${Config().baseUrl}${_method}?$requestParams");
    }

    // print("pa ${_parameter.getData()}");

    final http.Response response = await http
        .get(Uri.parse(
            '${_getUrl(_method, indonesiaOnly: indonesiaOnly)}?$requestParams'))
        .catchError((onError) {
      print("errornya apaan ${onError}");
    });
    if (Config.openApi) {
      print("bnor ${_method} ${response.body.toString()} get ");
    }

    // print("para ${_getUrl(_method)} apaan ${response.statusCode}");

    if (response != null) {
      if (response.statusCode == 200) {
        if (response.body != null || response.body.toString() != '') {
          return response.body;
        } else {
          this._onErrorListener!(
              new Error(this, response.statusCode, "${response.body}"));
          _handleResponseError(response.statusCode.toString(), response.body,
              _method, _parameter, _onSuccessListener);
        }
      } else {
        this._onErrorListener!(
            new Error(this, response.statusCode, "${response.body}"));
        _handleResponseError(response.statusCode.toString(), response.body,
            _method, _parameter, _onSuccessListener);
      }
    } else {
      if (response.statusCode != null) {
        this._onErrorListener!(
            new Error(this, response.statusCode, "${response.body}"));
        _handleResponseError(response.statusCode.toString(), response.body,
            _method, _parameter, _onSuccessListener);
      } else {
        // untuk pindah domain
        // if (Config.mainCoreDomain){
        //   Config.mainCoreDomain = false;
        //   // print("print main core ${Config.mainCoreDomain}");
        //   // Future.delayed(Duration(seconds: 3)).then((value) {
        //     SellonRouter.makeFirst(context, SplashSellonScreen());
        //   // });
        // }
        // print("mainCoreDong ${Config.mainCoreDomain}");
        if (!Config.isMaintanceShutDown) {
          Helper().toMainten(context,
              isErrorApi: true, key: UniqueKey(), method: _method);
        }
        // this._onErrorListener(new Error(this, 9999, "Maintaince Page"));
        // _handleResponseError(9999.toString(), "Maintaince Page",
        //     _method, _parameter, _onSuccessListener);
      }
    }
  } else {
    var requestParams = "";
    var index = 0;

    _parameter.data?.forEach((key, val) {
      index++;
      if (val is File) {
      } else {
        if (val is String) {
          if (!val.toString().contains("#")) {
            if (!key.contains("watched_video")) {
              if (_method == "community/baru/" ||
                  _method == "community/edit/" ||
                  _method == "chat/send/" ||
                  _method == "community/add_discussion/" ||
                  _method == "produk/add_discussion/" ||
                  _method == "produk/baru/" ||
                  _method == "produk/edit/" ||
                  _method == "community/" ||
                  _method == "pelanggan/daftar/" ||
                  _method == "pelanggan/daftarv2/" ||
                  _method == "pelanggan/daftarv3" ||
                  _method == "pelanggan/login_sosmedv3") {
                // if (key.toString().contains("username")) {
                //   requestParams +=
                //   "$key=$val${index == _parameter.data.length ? '' : '&'}";
                // }
                // if (key.toString().contains("password")) {
                //   requestParams +=
                //   "$key=$val${index == _parameter.data.length ? '' : '&'}";
                // }
                // if (key.toString().contains("telp")) {
                //   requestParams +=
                //   "$key=$val${index == _parameter.data.length ? '' : '&'}";
                // }
                if (!encryption!) {
                  if (key.toString().contains("apikey")) {
                    // print("confi meth ${Config.postMethod}");
                    // if (!Config.postMethod!){
                    requestParams +=
                        "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                    // }
                  }
                  if (key.toString().contains("apisess")) {
                    if (val != null || val != "null") {
                      requestParams +=
                          "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                    }
                  }
                }

                if (key.toString().contains("timezone")) {
                  requestParams +=
                      "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                }
                if (key.toString().contains("nation_code")) {
                  requestParams +=
                      "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                }
              } else {
                if (key == "bla1" ||
                    key == "bla2" ||
                    key == "bla3" ||
                    key == "samsung" ||
                    key == "fullhd" ||
                    key == "nvidia") {
                } else {
                  if (encryption!) {
                    if (key.toString().contains("nation_code")) {
                      requestParams +=
                          "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                    }
                  } else {
                    if (val != "Buy&Sell") {
                      if (val != 'null') {
                        requestParams +=
                            "$key=$val${index == _parameter.data?.length ? '' : '&'}";
                      }
                    }
                  }
                }
              }
            }
          }
        } else {
          requestParams +=
              "$key=$val${index == _parameter.data?.length ? '' : '&'}";
        }
      }
    });
    Parameter paramnew = Parameter();
    if (encryption!) {
      await Helper().checksodium().then((sodium) async {
        ParameterRegisterNew par = await Helper()
            .encryparamater(sodium, _parameter.getJson(), "clubkey");

        paramnew.set("nvidia", par.postData);
        paramnew.set("samsung", par.bla1);
        paramnew.set("fullhd", par.bla2);

        // print("value key --> ${par.postData}");
        // print("value keyserver ${Config().keyapikey}");
        // print("public key mobile => ${par.bla1} nonce ==>${par.bla2}");
      });
    }

    // print("parame $requestParams");

    // if(_method == "community/"){
    //   print("resq ${requestParams}");
    // }
    // print("call Api ${_method}");

    var request = http.MultipartRequest(
        "POST",
        Uri.parse(_getUrl(_method, indonesiaOnly: indonesiaOnly) +
            '?$requestParams'));

    // print("request param method${_method} ${r} ");

    if (Config.openApi) {
      print("request param method${_method} ${_parameter.getData()} ");
      // print("request ${_getUrl(_method)}?${requestParams}");
      // print("request2 ${request.url.toString()}");
    }
    if (Config.openApi) {
      print(
          "request ${_getUrl(_method, indonesiaOnly: indonesiaOnly)}?$requestParams");
    }

    if (encryption) {
      paramnew.data?.forEach((key, value) {
        request.fields[key] = '$value';
      });
    } else {
      _parameter.data?.forEach((k, v) async {
        if (v is File) {
          File file = v;
          if (file.existsSync()) {
            // String basename = file.path
            //     .split('/')
            //     .last
            //     .toString()
            //     .split(".")
            //     .first
            //     .toString();

            String basename = path.basename(file.path);

            // String basename = path.basename(
            //     pathh.substring(0, pathh.lastIndexOf(".") + 1) +
            //         getMimeType(pathh));
            // var stream = new http.ByteStream(Dele)
            // var stream = new http.ByteStream(DelegatingStream.typed(file.openRead()));
            var filestream = file.readAsBytes().asStream();
            var length = file.lengthSync();
            http.MultipartFile multipartFile = new http.MultipartFile(
                "$k", filestream, length,
                filename: basename);

            // var byet = file.readAsBytesSync();
            // final httpImage = http.MultipartFile.fromBytes('files.myimage', byet,
            //     contentType: MediaType.parse(mimeType), filename: 'myImage.png');
            request.files.add(multipartFile);
          }
        } else {
          request.fields[k] = '$v';
        }
      });
    }

    // final dio = Dio();
    //
    // final response2 = await dio.post(
    //     _getUrl(_method,indonesiaOnly: indonesiaOnly) + '?$requestParams',
    //     data: encryption ? paramnew.getData() : _parameter.getData(),
    //     options: Options(
    //       validateStatus: (status) => true,
    //     )
    // );
    //
    // print("responsenya ${response2.statusCode}");
    // print("responsenya ${response2.statusMessage}");
    // print("responsenya ${response2.data}");
    //
    // //
    // if (response2.statusCode == 200){
    //   return jsonEncode(response2.data);
    // }else{
    //   this._onErrorListener!(
    //       new Error(this, response2.statusCode ?? 9999999, response2.statusMessage));
    //   _handleResponseError((response2.statusCode ?? '99999') as String?, response2.statusMessage, _method,
    //       _parameter, _onSuccessListener);
    // }

    try {
      var response = await request.send();
      // .timeout(timeoutDuration!, onTimeout: () async {
      //   _handleResponseError(
      //       null, null, _method, _parameter, _onSuccessListener);
      //   return null;
      //   // return "true";
      // });

      String body = await response.stream.bytesToString();
      // print("asss ${_parameter.getData()}");

      if (Config.openApi) {
        print("body ${body}");
      }

      // print("ses method${_method}  ${body}");

      if (response != null) {
        if (response.statusCode == 200) {
          return body;
        } else {
          this._onErrorListener!(
              new Error(this, response.statusCode, "${body}"));
          _handleResponseError(response.statusCode.toString(), body, _method,
              _parameter, _onSuccessListener);
        }
      } else {
        if (response.statusCode != null) {
          this._onErrorListener!(
              new Error(this, response.statusCode, "${body}"));
          _handleResponseError(response.statusCode.toString(), body, _method,
              _parameter, _onSuccessListener);
        } else {
          if (!Config.isMaintanceShutDown) {
            Helper().toMainten(context,
                isErrorApi: true, key: UniqueKey(), method: _method);
          }
        }
      }
    } catch (exp) {
      if (_onExceptionListener != null && exp is Exception) {
        _onExceptionListener!(exp);
      }
    }
  }
  //   return "true";
}