execute method
dynamic
execute(
- String _method,
- SellonMethod sellonMethod,
- Parameter _parameter,
- OnSuccessListener _onSuccessListener, {
- bool? isShowMessage,
- bool? isIndonesiaOnly,
- bool? encrytion,
Implementation
execute(String _method, SellonMethod sellonMethod, Parameter _parameter,
OnSuccessListener _onSuccessListener,
{
// OnSuccessListListener? onSuccessListListener,
// OnSuccessStringListener? onSuccessStringListener,
bool? isShowMessage,
bool? isIndonesiaOnly,
bool? encrytion}) async {
int _errCode = 0;
String _errMessage = '';
if (encrytion == null) {
encrytion = false;
}
Methods.listEncryption.forEach((element) {
if (element == _method) {
encrytion = true;
}
});
// if (_method == "community_like/baruv3/"){
// encrytion = true;
// }
this._onSuccessListener = _onSuccessListener;
// if (onSuccessListListener != null) {
// this._onSuccessListListener = onSuccessListListener;
// }
// if (onSuccessStringListener != null) {
// this._onSuccessStringListener = onSuccessStringListener;
// }
if (this._onErrorListener == null) {
this._onErrorListener = (error) {
if (isShowMessage == null) {
if (error.errCode != 1040 || error.errCode != 799) {
if (error.errMessage != "") {
Ui(context).errorAlert(title: 'Oops', message: error.errMessage);
}
}
if (error.errCode == 799) {
Helper().toMainten(context,
isErrorApi: true, key: UniqueKey(), method: _method);
}
}
};
}
if (this._onExceptionListener == null) {
this._onExceptionListener = (ex) {
String msg = ex.toString().replaceAll("Exception: ", "");
Ui(context).errorAlert(title: 'Oops', message: "No Internet");
};
}
if (_errCode == 0) {
if (_method.isEmpty) {
_errCode++;
_errMessage = 'Method empty';
}
}
if (_errCode == 0) {
if (_parameter == null) {
_errCode++;
_errMessage = 'Parameter empty';
}
}
if (_errCode == 0) {
if (this._paginator != null) {
this
._paginator!
.setOnSuccessPaginator(this._onSuccessListener)
.setup(_method, sellonMethod, _parameter);
}
}
// var index = 0;
// _parameter.data.forEach((key, val) {
// index++;
// });
try {
if (_errCode == 0) {
final result = await InternetAddress.lookup('google.com');
// print("curret timezone asss ${result}");
if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
Config.AppVersion == VersionApps.indonesia
? _parameter.set('nation_code', "62")
: _parameter.set('nation_code', "65");
// if(_method == "pelanggan/check_version_mobile_app/"){
// _parameter.set('nation_code', "65");
// }
Preferences.getApiKey().then((value) {
Preferences.getApises().then((apisess) async {
if (value != null) {
_parameter.set('apikey', value);
}
if (_method != Methods.pelangganLogin || _method != "bank/list/"
// || _method != "pelanggan/check_version_mobile_app/"
) {
if (apisess != null) {
_parameter.set('apisess', apisess);
}
}
if (_method != 'language/change/' ||
_method != "language/user/") {
if (Config.bahasanya == Bahasa.indonesia) {
_parameter.set("language_id", "2");
} else {
_parameter.set("language_id", "1");
}
}
final String currentTimeZone =
await FlutterTimezone.getLocalTimezone();
if (_method != "bank/list/") {
_parameter.set("timezone", currentTimeZone);
}
_parameter.set('device', Platform.isIOS ? 'ios' : 'android');
// print("sellon ${Config.postMethod}");
// sellonMethod = _method
executeFuture(
_method,
// Config.postMethod != null && Config.postMethod! ? SellonMethod.post :
sellonMethod,
_parameter,
_onSuccessListener,
encryption: encrytion,
indonesiaOnly: isIndonesiaOnly)
.then((body) async {
if (Config.openApi) {
print("bodyyyy ${_method} ${body}");
}
// print("bod $body");
if (body == null) {
// print("bbb ${body}");
// _handleResponseError('799', "Sorry We will back soon", _method, _parameter, _onSuccessListener);
this._onErrorListener!(
new Error(this, 799, 'Sorry We will back soon'));
} else {
if (body == "Please wait for a while") {
this._onErrorListener!(new Error(this, 99999, ''));
_handleResponseError(
null, null, _method, _parameter, _onSuccessListener);
} else {
if (Config.openApi) {
print("error ${body}");
}
var decodeSucceeded = false;
var item;
try {
item = jsonDecode(body);
decodeSucceeded = true;
} on FormatException catch (e) {
print('The provided string is not valid JSON');
}
// var item = jsonDecode(body);
if (decodeSucceeded) {
if (item is Map) {
int code = 200;
if (item['status'] is String) {
code = int.parse(item['status']);
} else if (item['status'] is int) {
code = item['status'];
}
if (code == 200) {
// if (_method == Methods.pelangganLogin) {
// if (Config.AppVersion == VersionApps.indonesia) {
// getLanguageUser();
// }
// }
if (item['data'] is List) {
Success success = new Success(this, item);
this._onSuccessListener(success);
} else if (item['data'] is String) {
Success success = new Success(this, item);
this._onSuccessListener(success);
} else {
Success success = new Success(this, item);
this._onSuccessListener(success);
}
} else {
if (code == 400) {
await ApiSellon.createDefaultParams(
(parameter) async {
parameter.set('username', 'mobile');
parameter.set('password',
'a4786851f5261822f0f57182ee2dc656b57687c8a105c1b697c280ef48aee7e8');
Config.AppVersion == VersionApps.indonesia
? parameter.set('nation_code', '62')
: parameter.set('nation_code', '65');
await ApiSellon().execute(
'apikey/auth/', SellonMethod.post, parameter,
(response) async {
String apikey =
response.data()['data']['apikey_plain'];
var bytes =
utf8.encode(apikey); // data being hashed
var sha256c = sha256.convert(bytes);
Preferences.setApiKey(sha256c.toString());
await ApiSellon.createDefaultParams(
(parameter) async {
Preferences.getApiKey().then((apikeynew) {
Preferences.getApises()
.then((apisessnew) async {
if (apikeynew != null) {
parameter = _parameter;
_parameter.set(
'apikey', sha256c.toString());
Config.AppVersion ==
VersionApps.indonesia
? _parameter.set(
'nation_code', "62")
: _parameter.set('nation_code', 65);
// if(_method == "pelanggan/check_version_mobile_app/"){
// _parameter.set('nation_code', "65");
// }
if (apisessnew != null) {
_parameter.set('apisess', apisessnew);
}
_parameter.set("timezone",
Helper().dateNowChange());
ApiSellon api2 = ApiSellon();
api2.setErrorListener((error) {
this._onErrorListener!(new Error(
this,
error.errCode!,
error.errMessage!));
});
await api2.execute(
_method, sellonMethod, _parameter,
(response) {
int status =
response.data()!["status"];
if (status == 200) {
Success success = response;
this._onSuccessListener(success);
}
},
isShowMessage: _method.contains(
"pelanggan/login_sosmedv2")
? false
: true);
// await ApiSellon(_context).execute(
// _method, sellonMethod, _parameter,
// (response) {
// int status = response.data()["status"];
// if (status == 200) {
// Success success = response;
// this._onSuccessListener(success);
// }
// });
}
});
});
});
});
});
} else if (code == 401 &&
item['message']
.toString()
.toLowerCase()
.contains(
"missing or invalid api session") ||
item['message']
.toString()
.toLowerCase()
.contains("sesi api tidak ada")) {
Preferences.getId().then((value) async {
if (value != null ||
value != '' ||
value != 'null') {
Helper().modalSellOn(context,
alone: true,
barierDismisable: false,
message: 'Please login again', onOke: () {
Preferences.clearChace();
Config.loginWith = LoginWith.nonLogin;
Navigator.pop(
Config.navigatorKey.currentContext!);
SellonRouter.makeFirst(
Config.navigatorKey.currentContext!,
Config.splashData!);
// Navigator.pop(context);
// // SellonRouter.makeFirst(context, HomeScreen());
// Navigator.of(context).pushReplacement(
// new MaterialPageRoute(
// builder: (context) =>
// SplashSellonScreen()),
// );
});
}
});
} else {
if (isShowMessage == null) {
_handleResponseError(
'${item['status']}',
item['message'],
_method,
_parameter,
_onSuccessListener);
}
this._onErrorListener!(new Error(
this,
item['status'] is String
? int.parse(item['status'])
: item['status'],
"${item['message']}"));
}
}
if (this._onCompleteListener != null) {
this._onCompleteListener!();
}
}
} else {
if (isShowMessage == null) {
_handleResponseError(null, null, _method, _parameter,
_onSuccessListener);
}
this._onErrorListener!(
new Error(this, 99999, 'Please wait a moment'));
}
}
}
// )
// .
// timeout(timeoutDuration!, onTimeout: () {
// this._onErrorListener!(new Error(this, 199, 'Time Out'));
// _handleResponseError(
// null, null, _method, _parameter, _onSuccessListener);
});
});
});
}
// else {
// _errCode++;
// _errMessage = 'No Akses';
// }
// })
// .onError((error, stackTrace) {
// // print("object${error.toString()}");
// // Future.delayed(Duration(seconds: 1)).then((value) {
// if(Config.openMaintence != null && Config.openMaintence == false){
// if(context != null){
// SellonRouter.makeFirst(context, MaintanceScreen(isErrorApi: true,domainbefore: Config.domainMain == "sellon.net" ? "sellon.store" : "sellon.net" ,key: UniqueKey(),));
// }
// }else{
//
// if(context != null){
// Config.openMaintence = true;
// SellonRouter.makeFirst(context, MaintanceScreen(isErrorApi: true,domainbefore: Config.domainMain == "sellon.net" ? "sellon.store" : "sellon.net" ,key: UniqueKey(),));
// }
// }
// });
// });
// print("result host=> ${result[0].host} adress ==> ${result[0].address} raw ==> ${result[0].rawAddress}");
}
} on SocketException catch (a) {
// print("aapa ini ${a.message}");
Future.delayed(Duration(seconds: 1)).then((value) {
Helper().toMainten(context, isErrorApi: true, method: _method);
// SellonRouter.makeFirst(
// context,
// MaintanceScreen(
// isErrorApi: true,
// domainbefore: Config.domainMain == "sellon.net"
// ? "sellon.store"
// : "sellon.net",
// ));
});
_errCode++;
_errMessage = 'No Internet';
} catch (e) {
// print("object ${e.toString()}");
_errCode++;
_errMessage = 'No Internet';
Future.delayed(Duration(seconds: 1)).then((value) {
Helper().toMainten(context, isErrorApi: true, method: _method);
});
// print("heree ");
}
if (_errCode > 0) {
try {
throw new Exception(_errMessage);
} catch (ex) {
this._onExceptionListener!(ex);
}
}
}