Initial push to my private server

This commit is contained in:
Hamza-Ayed
2025-09-21 15:02:12 +03:00
parent 7e904ae460
commit f08ee61a7e
32 changed files with 1622 additions and 373 deletions

View File

@@ -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,