Initial push to my private server
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:jwt_decoder/jwt_decoder.dart';
|
||||
import 'package:sefer_driver/controller/functions/network/net_guard.dart';
|
||||
import 'package:secure_string_operations/secure_string_operations.dart';
|
||||
@@ -16,6 +17,7 @@ import '../../constant/info.dart';
|
||||
import '../../views/widgets/error_snakbar.dart';
|
||||
import '../../print.dart';
|
||||
import 'gemeni.dart';
|
||||
import 'network/connection_check.dart';
|
||||
import 'upload_image.dart';
|
||||
|
||||
class CRUD {
|
||||
@@ -86,24 +88,27 @@ class CRUD {
|
||||
Map<String, dynamic>? payload,
|
||||
required Map<String, String> headers,
|
||||
}) async {
|
||||
// ✅ 1. Check for internet connection before making any request.
|
||||
if (!await _netGuard.hasInternet(mustReach: Uri.parse(link))) {
|
||||
// ✅ 2. If no internet, show a notification to the user (only once every 15s).
|
||||
_netGuard.notifyOnce((title, msg) {
|
||||
mySnackeBarError(
|
||||
msg); // Using your existing snackbar for notifications.
|
||||
});
|
||||
// ✅ 3. Return a specific status to indicate no internet.
|
||||
return 'no_internet';
|
||||
}
|
||||
|
||||
var url = Uri.parse(link);
|
||||
try {
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: headers,
|
||||
// 1. Wrap the http.post call directly with HttpRetry.sendWithRetry.
|
||||
// It will attempt the request immediately and retry on transient errors.
|
||||
var response = await HttpRetry.sendWithRetry(
|
||||
() {
|
||||
var url = Uri.parse(link);
|
||||
return http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: headers,
|
||||
);
|
||||
},
|
||||
// Optional: you can customize retry behavior for each call
|
||||
maxRetries: 3,
|
||||
timeout: const Duration(seconds: 15),
|
||||
);
|
||||
// Log.print('response: ${response.body}');
|
||||
// Log.print('request: ${response.request}');
|
||||
// Log.print('payload: ${payload}');
|
||||
// ✅ All your existing logic for handling server responses remains the same.
|
||||
// This part is only reached if the network request itself was successful.
|
||||
|
||||
// Handle successful response (200 OK)
|
||||
if (response.statusCode == 200) {
|
||||
@@ -112,16 +117,18 @@ class CRUD {
|
||||
if (jsonData['status'] == 'success') {
|
||||
return jsonData; // Return the full JSON object on success
|
||||
} else {
|
||||
// The API reported a logical failure (e.g., validation error)
|
||||
addError(
|
||||
'API Logic Error: ${jsonData['status']}',
|
||||
'Response: ${response.body}',
|
||||
'CRUD._makeRequest - $link',
|
||||
);
|
||||
if (jsonData['status'] == 'failure') {
|
||||
// return 'failure';
|
||||
} else {
|
||||
addError(
|
||||
'API Logic Error: ${jsonData['status']}',
|
||||
'Response: ${response.body}',
|
||||
'CRUD._makeRequest - $link',
|
||||
);
|
||||
}
|
||||
return jsonData['status']; // Return the specific status string
|
||||
}
|
||||
} catch (e, stackTrace) {
|
||||
// Error decoding the JSON response from the server
|
||||
addError(
|
||||
'JSON Decode Error: $e',
|
||||
'Response Body: ${response.body}\nStack Trace: $stackTrace',
|
||||
@@ -130,20 +137,13 @@ class CRUD {
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
// Handle Unauthorized (401) - typically means token expired
|
||||
// Handle Unauthorized (401)
|
||||
else if (response.statusCode == 401) {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
// The token refresh logic is handled before the call,
|
||||
// but we log this case if it still happens.
|
||||
// addError(
|
||||
// 'Token Expired',
|
||||
// 'A new token should have been fetched before this call.',
|
||||
// 'CRUD._makeRequest - $link',
|
||||
// );
|
||||
await Get.put(LoginDriverController()).getJWT();
|
||||
return 'token_expired';
|
||||
} else {
|
||||
// Other 401 errors (e.g., invalid token)
|
||||
addError(
|
||||
'Unauthorized Error: ${jsonData['error']}',
|
||||
'Status Code: 401',
|
||||
@@ -161,8 +161,14 @@ class CRUD {
|
||||
);
|
||||
return 'failure';
|
||||
}
|
||||
} on SocketException {
|
||||
// 2. This block now catches the "no internet" case after all retries have failed.
|
||||
_netGuard.notifyOnce((title, msg) {
|
||||
mySnackeBarError(msg);
|
||||
});
|
||||
return 'no_internet'; // Return the specific status you were using before.
|
||||
} catch (e, stackTrace) {
|
||||
// Handle network exceptions (e.g., no internet, DNS error)
|
||||
// 3. This is a general catch-all for any other unexpected errors.
|
||||
addError(
|
||||
'HTTP Request Exception: $e',
|
||||
'Stack Trace: $stackTrace',
|
||||
@@ -177,15 +183,15 @@ class CRUD {
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
// 1. Check if the token is expired
|
||||
bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
.toString()
|
||||
.split(AppInformation.addd)[0]);
|
||||
// bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
// .r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
// .toString()
|
||||
// .split(AppInformation.addd)[0]);
|
||||
|
||||
// 2. If expired, get a new one
|
||||
if (isTokenExpired) {
|
||||
await LoginDriverController().getJWT();
|
||||
}
|
||||
// // 2. If expired, get a new one
|
||||
// if (isTokenExpired) {
|
||||
// await LoginDriverController().getJWT();
|
||||
// }
|
||||
|
||||
// 3. Prepare the headers with the valid token
|
||||
final headers = {
|
||||
@@ -303,15 +309,15 @@ class CRUD {
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
.r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
.toString()
|
||||
.split(AppInformation.addd)[0]);
|
||||
// Log.print('isTokenExpired: ${isTokenExpired}');
|
||||
// bool isTokenExpired = JwtDecoder.isExpired(X
|
||||
// .r(X.r(X.r(box.read(BoxName.jwt), cn), cC), cs)
|
||||
// .toString()
|
||||
// .split(AppInformation.addd)[0]);
|
||||
// // Log.print('isTokenExpired: ${isTokenExpired}');
|
||||
|
||||
if (isTokenExpired) {
|
||||
await LoginDriverController().getJWT();
|
||||
}
|
||||
// if (isTokenExpired) {
|
||||
// await LoginDriverController().getJWT();
|
||||
// }
|
||||
// await Get.put(LoginDriverController()).getJWT();
|
||||
var url = Uri.parse(
|
||||
link,
|
||||
|
||||
@@ -109,8 +109,7 @@ class LocationController extends GetxController {
|
||||
|
||||
// ✅ تحديث للسيرفر
|
||||
await CRUD().post(
|
||||
link:
|
||||
box.read(BoxName.serverChosen) + '/ride/location/update.php',
|
||||
link: '${AppLink.server}/ride/location/update.php',
|
||||
payload: payload,
|
||||
);
|
||||
|
||||
@@ -141,7 +140,7 @@ class LocationController extends GetxController {
|
||||
if (_insertCounter == 12) {
|
||||
_insertCounter = 0;
|
||||
await CRUD().post(
|
||||
link: box.read(BoxName.serverChosen) + '/ride/location/add.php',
|
||||
link: '${AppLink.server}/ride/location/add.php',
|
||||
payload: payload,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ Future<void> showDriverGiftClaim(BuildContext context) async {
|
||||
if (box.read(BoxName.is_claimed).toString() == '0' ||
|
||||
box.read(BoxName.is_claimed) == null) {
|
||||
MyDialog().getDialog(
|
||||
'You have gift 300 L.E'.tr, 'This for new registration'.tr, () async {
|
||||
'You have gift 30000 SYP'.tr, 'This for new registration'.tr, () async {
|
||||
var res = await CRUD().post(link: AppLink.updateDriverClaim, payload: {
|
||||
'driverId': box.read(BoxName.driverID),
|
||||
});
|
||||
if (res != 'failure') {
|
||||
Get.find<CaptainWalletController>()
|
||||
.addDriverWallet('new driver', '300', '300');
|
||||
.addDriverWallet('new driver', '30000', '30000');
|
||||
Confetti.launch(
|
||||
context,
|
||||
options:
|
||||
|
||||
@@ -77,7 +77,7 @@ class SmsEgyptController extends GetxController {
|
||||
var res = await http.post(
|
||||
Uri.parse(AppLink.checkCredit),
|
||||
body: {
|
||||
"username": AppInformation.appName,
|
||||
"username": 'Sefer',
|
||||
"password": AK.smsPasswordEgypt,
|
||||
"message": "This is an example SMS message.",
|
||||
"language": box.read(BoxName.lang) == 'en' ? "e" : 'r',
|
||||
|
||||
Reference in New Issue
Block a user