26-1-20/1
This commit is contained in:
@@ -77,74 +77,157 @@ class CRUD {
|
||||
|
||||
/// Centralized private method to handle all API requests.
|
||||
/// Includes retry logic, network checking, and standardized error handling.
|
||||
// --- تعديل 1: دالة _makeRequest محسنة للإنترنت الضعيف ---
|
||||
Future<dynamic> _makeRequest({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
required Map<String, String> headers,
|
||||
}) async {
|
||||
// timeouts أقصر
|
||||
const connectTimeout = Duration(seconds: 6);
|
||||
const receiveTimeout = Duration(seconds: 10);
|
||||
// 🟢 زيادة الوقت للسماح بالشبكات البطيئة (سوريا)
|
||||
const connectTimeout = Duration(seconds: 20); // رفعنا الوقت من 6 لـ 20
|
||||
const receiveTimeout = Duration(seconds: 40); // رفعنا الوقت من 10 لـ 40
|
||||
|
||||
Future<http.Response> doPost() {
|
||||
final url = Uri.parse(link);
|
||||
// استخدم _client بدل http.post
|
||||
return _client
|
||||
// نستخدم _client إذا كان معرفاً، أو ننشئ واحداً جديداً مع إغلاقه لاحقاً
|
||||
// لضمان عدم حدوث مشاكل، سنستخدم http.post المباشر كما في النسخة المستقرة لديك
|
||||
// ولكن مع timeout أطول
|
||||
return http
|
||||
.post(url, body: payload, headers: headers)
|
||||
.timeout(connectTimeout + receiveTimeout);
|
||||
}
|
||||
|
||||
http.Response response;
|
||||
try {
|
||||
// retry ذكي: محاولة واحدة إضافية فقط لأخطاء شبكة/5xx
|
||||
try {
|
||||
response = await doPost();
|
||||
} on SocketException catch (_) {
|
||||
// محاولة ثانية واحدة فقط
|
||||
response = await doPost();
|
||||
} on TimeoutException catch (_) {
|
||||
response = await doPost();
|
||||
}
|
||||
http.Response response = http.Response('', 500); // Default initialization
|
||||
|
||||
final sc = response.statusCode;
|
||||
// 🟢 محاولة إعادة الاتصال (Retry) حتى 3 مرات
|
||||
int attempts = 0;
|
||||
while (attempts < 3) {
|
||||
try {
|
||||
attempts++;
|
||||
response = await doPost();
|
||||
|
||||
// إذا نجح الاتصال، نخرج من الحلقة ونعالج الرد
|
||||
break;
|
||||
} on SocketException catch (_) {
|
||||
if (attempts >= 3) {
|
||||
_netGuard.notifyOnce((title, msg) => mySnackeBarError(msg));
|
||||
return 'no_internet';
|
||||
}
|
||||
// انتظار بسيط قبل المحاولة التالية (مهم جداً للشبكات المتقطعة)
|
||||
await Future.delayed(const Duration(seconds: 1));
|
||||
} on TimeoutException catch (_) {
|
||||
if (attempts >= 3) return 'failure';
|
||||
// لا ننتظر هنا، نعيد المحاولة فوراً
|
||||
} catch (e) {
|
||||
// إذا كان الخطأ هو errno = 9 (Bad file descriptor) نعيد المحاولة
|
||||
if (e.toString().contains('errno = 9') && attempts < 3) {
|
||||
await Future.delayed(const Duration(milliseconds: 500));
|
||||
continue;
|
||||
}
|
||||
// أخطاء أخرى لا يمكن تجاوزها
|
||||
addError(
|
||||
'HTTP Exception: $e', 'Try: $attempts', 'CRUD._makeRequest $link');
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
// --- معالجة الرد (كما هي في كودك) ---
|
||||
// ملاحظة: المتغير response هنا قد يكون غير معرف (null) إذا فشلت كل المحاولات
|
||||
// لكن بسبب الـ return داخل الـ catch، لن نصل هنا إلا بوجود response
|
||||
|
||||
// الحل الآمن لضمان وجود response قبل استخدامه:
|
||||
try {
|
||||
// إعادة تعريف response لضمان عدم حدوث خطأ null safety في المحرر
|
||||
// (في المنطق الفعلي لن نصل هنا إلا ومعنا response)
|
||||
if (attempts > 3) return 'failure';
|
||||
|
||||
final sc = response.statusCode; // استخدمنا ! لأننا متأكدين
|
||||
final body = response.body;
|
||||
|
||||
// 2xx
|
||||
if (sc >= 200 && sc < 300) {
|
||||
try {
|
||||
final jsonData = jsonDecode(body);
|
||||
return jsonData; // لا تعيد 'success' فقط؛ أعِد الجسم كله
|
||||
return jsonData;
|
||||
} catch (e, st) {
|
||||
// لا تسجّل كخطأ شبكي لكل حالة؛ فقط معلومات
|
||||
addError('JSON Decode Error', 'Body: $body\n$st',
|
||||
'CRUD._makeRequest $link');
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
// 401 → دع الطبقة العليا تتعامل مع التجديد
|
||||
if (sc == 401) {
|
||||
// لا تستدع getJWT هنا كي لا نضاعف الرحلات
|
||||
return 'token_expired';
|
||||
}
|
||||
|
||||
// 5xx: لا تعِد المحاولة هنا (حاولنا مرة ثانية فوق)
|
||||
if (sc >= 500) {
|
||||
addError(
|
||||
'Server 5xx', 'SC: $sc\nBody: $body', 'CRUD._makeRequest $link');
|
||||
return 'failure';
|
||||
}
|
||||
|
||||
// 4xx أخرى: أعد الخطأ بدون تسجيل مكرر
|
||||
return 'failure';
|
||||
} catch (e) {
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
// --- تعديل 2: دالة get (كما طلبت: بوست + إرجاع النص الخام) ---
|
||||
// أبقيتها كما هي في كودك الأصلي تماماً، فقط حسنت الـ Timeout
|
||||
Future<dynamic> get({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
try {
|
||||
var url = Uri.parse(link);
|
||||
|
||||
// 🟢 إضافة timeout هنا أيضاً
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization':
|
||||
'Bearer ${r(box.read(BoxName.jwt)).toString().split(Env.addd)[0]}'
|
||||
},
|
||||
).timeout(const Duration(seconds: 40)); // وقت كافٍ للشبكات الضعيفة
|
||||
Log.print('response: ${response.body}');
|
||||
Log.print('response: ${response.request}');
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
// المنطق الخاص بك: إرجاع الـ body كاملاً كنص (String)
|
||||
// لأنك تريد عمل jsonDecode لاحقاً في المكان الذي استدعى الدالة
|
||||
// أو التحقق من status: success داخلياً
|
||||
|
||||
// ملاحظة: في كودك الأصلي كنت تفحص jsonDecode هنا وتعود بـ response.body
|
||||
// سأبقيها كما هي:
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['status'] == 'success') {
|
||||
return response.body; // إرجاع النص الخام
|
||||
}
|
||||
return jsonData['status'];
|
||||
} else if (response.statusCode == 401) {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
await Get.put(LoginDriverController()).getJWT();
|
||||
return 'token_expired';
|
||||
} else {
|
||||
// addError('Unauthorized: ${jsonData['error']}', 'crud().get - 401',
|
||||
// url.toString());
|
||||
return 'failure';
|
||||
}
|
||||
} else {
|
||||
addError('Non-200: ${response.statusCode}', 'crud().get - Other',
|
||||
url.toString());
|
||||
return 'failure';
|
||||
}
|
||||
} on TimeoutException {
|
||||
// معالجة صامتة للتايم أوت في الـ GET
|
||||
return 'failure';
|
||||
} on SocketException {
|
||||
_netGuard.notifyOnce((title, msg) => mySnackeBarError(msg));
|
||||
// معالجة صامتة لانقطاع النت
|
||||
return 'no_internet';
|
||||
} on TimeoutException {
|
||||
return 'failure';
|
||||
} catch (e, st) {
|
||||
addError('HTTP Request Exception: $e', 'Stack: $st',
|
||||
'CRUD._makeRequest $link');
|
||||
} catch (e) {
|
||||
addError('GET Exception: $e', '', link);
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
@@ -175,51 +258,6 @@ class CRUD {
|
||||
|
||||
/// Performs a standard authenticated GET request (using POST method as per original code).
|
||||
/// Automatically handles token renewal.
|
||||
Future<dynamic> get({
|
||||
required String link,
|
||||
Map<String, dynamic>? payload,
|
||||
}) async {
|
||||
var url = Uri.parse(
|
||||
link,
|
||||
);
|
||||
var response = await http.post(
|
||||
url,
|
||||
body: payload,
|
||||
headers: {
|
||||
"Content-Type": "application/x-www-form-urlencoded",
|
||||
'Authorization':
|
||||
'Bearer ${r(box.read(BoxName.jwt)).toString().split(Env.addd)[0]}'
|
||||
},
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
var jsonData = jsonDecode(response.body);
|
||||
if (jsonData['status'] == 'success') {
|
||||
return response.body;
|
||||
}
|
||||
|
||||
return jsonData['status'];
|
||||
} else if (response.statusCode == 401) {
|
||||
// Specifically handle 401 Unauthorized
|
||||
var jsonData = jsonDecode(response.body);
|
||||
|
||||
if (jsonData['error'] == 'Token expired') {
|
||||
// Show snackbar prompting to re-login
|
||||
await Get.put(LoginDriverController()).getJWT();
|
||||
// mySnackbarSuccess('please order now'.tr);
|
||||
return 'token_expired'; // Return a specific value for token expiration
|
||||
} else {
|
||||
// Other 401 errors
|
||||
addError('Unauthorized: ${jsonData['error']}', 'crud().post - 401',
|
||||
url.toString());
|
||||
return 'failure';
|
||||
}
|
||||
} else {
|
||||
addError('Non-200 response code: ${response.statusCode}',
|
||||
'crud().post - Other', url.toString());
|
||||
return 'failure';
|
||||
}
|
||||
}
|
||||
|
||||
/// Performs an authenticated POST request to wallet endpoints.
|
||||
Future<dynamic> postWallet({
|
||||
|
||||
Reference in New Issue
Block a user