|
| 1 | +import 'dart:async'; |
| 2 | +import 'package:dio/dio.dart'; |
| 3 | +import 'package:flutter_secure_storage/flutter_secure_storage.dart'; |
| 4 | +import 'package:connectivity_plus/connectivity_plus.dart'; |
| 5 | +import 'package:progres/core/network/cache_manager.dart'; |
| 6 | + |
| 7 | +class DischargeApiClient { |
| 8 | + static const String baseUrl = 'https://quittance.mesrs.dz/api'; |
| 9 | + |
| 10 | + late final Dio _dio; |
| 11 | + final FlutterSecureStorage _secureStorage; |
| 12 | + late final CacheManager _cacheManager; |
| 13 | + final Duration _shortTimeout = const Duration(seconds: 5); |
| 14 | + final Connectivity _connectivity = Connectivity(); |
| 15 | + |
| 16 | + DischargeApiClient({FlutterSecureStorage? secureStorage}) |
| 17 | + : _secureStorage = secureStorage ?? const FlutterSecureStorage() { |
| 18 | + _dio = Dio( |
| 19 | + BaseOptions( |
| 20 | + baseUrl: baseUrl, |
| 21 | + connectTimeout: const Duration(seconds: 30), |
| 22 | + receiveTimeout: const Duration(seconds: 30), |
| 23 | + headers: {'Content-Type': 'application/json'}, |
| 24 | + ), |
| 25 | + ); |
| 26 | + _dio.interceptors.add( |
| 27 | + InterceptorsWrapper( |
| 28 | + onRequest: (options, handler) async { |
| 29 | + final token = await _secureStorage.read(key: 'auth_token'); |
| 30 | + if (token != null) { |
| 31 | + options.headers['authorization'] = token; |
| 32 | + } |
| 33 | + return handler.next(options); |
| 34 | + }, |
| 35 | + onError: (error, handler) { |
| 36 | + // Handle errors |
| 37 | + return handler.next(error); |
| 38 | + }, |
| 39 | + ), |
| 40 | + ); |
| 41 | + CacheManager.getInstance().then((value) => _cacheManager = value); |
| 42 | + } |
| 43 | + |
| 44 | + Future<bool> get isConnected async { |
| 45 | + final result = await _connectivity.checkConnectivity(); |
| 46 | + return result != ConnectivityResult.none; |
| 47 | + } |
| 48 | + |
| 49 | + Future<void> saveToken(String token) async { |
| 50 | + await _secureStorage.write(key: 'auth_token', value: token); |
| 51 | + } |
| 52 | + |
| 53 | + Future<void> saveUuid(String uuid) async { |
| 54 | + await _secureStorage.write(key: 'uuid', value: uuid); |
| 55 | + } |
| 56 | + |
| 57 | + Future<void> saveEtablissementId(String etablissementId) async { |
| 58 | + await _secureStorage.write(key: 'etablissement_id', value: etablissementId); |
| 59 | + } |
| 60 | + |
| 61 | + Future<String?> getUuid() async { |
| 62 | + return await _secureStorage.read(key: 'uuid'); |
| 63 | + } |
| 64 | + |
| 65 | + Future<String?> getEtablissementId() async { |
| 66 | + return await _secureStorage.read(key: 'etablissement_id'); |
| 67 | + } |
| 68 | + |
| 69 | + Future<bool> isLoggedIn() async { |
| 70 | + final token = await _secureStorage.read(key: 'auth_token'); |
| 71 | + return token != null; |
| 72 | + } |
| 73 | + |
| 74 | + // Generate a cache key string based on path and query parameters |
| 75 | + String _cacheKey(String path, Map<String, dynamic>? queryParameters) { |
| 76 | + final queryStr = |
| 77 | + queryParameters != null |
| 78 | + ? Uri(queryParameters: queryParameters).query |
| 79 | + : ''; |
| 80 | + return '$path?$queryStr'; |
| 81 | + } |
| 82 | + |
| 83 | + Future<Response> get( |
| 84 | + String path, { |
| 85 | + Map<String, dynamic>? queryParameters, |
| 86 | + }) async { |
| 87 | + final key = _cacheKey(path, queryParameters); |
| 88 | + |
| 89 | + if (!await isConnected) { |
| 90 | + // offline - use cached data if available |
| 91 | + final cachedData = _cacheManager.getCache(key); |
| 92 | + if (cachedData != null) { |
| 93 | + return Response( |
| 94 | + requestOptions: RequestOptions(path: path), |
| 95 | + data: cachedData, |
| 96 | + statusCode: 200, |
| 97 | + ); |
| 98 | + } else { |
| 99 | + // No cache, throw offline error |
| 100 | + throw DioException( |
| 101 | + requestOptions: RequestOptions(path: path), |
| 102 | + error: 'No internet connection and no cached data', |
| 103 | + ); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + try { |
| 108 | + // Try to get fresh data with a short timeout for fast fallback on slow responses |
| 109 | + final response = await _dio.get( |
| 110 | + path, |
| 111 | + queryParameters: queryParameters, |
| 112 | + options: Options( |
| 113 | + sendTimeout: _shortTimeout, |
| 114 | + receiveTimeout: _shortTimeout, |
| 115 | + ), |
| 116 | + ); |
| 117 | + await _cacheManager.saveCache(key, response.data); |
| 118 | + return response; |
| 119 | + } catch (e) { |
| 120 | + // On failure, return cached data if available |
| 121 | + final cachedData = _cacheManager.getCache(key); |
| 122 | + if (cachedData != null) { |
| 123 | + return Response( |
| 124 | + requestOptions: RequestOptions(path: path), |
| 125 | + data: cachedData, |
| 126 | + statusCode: 200, |
| 127 | + ); |
| 128 | + } |
| 129 | + rethrow; |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + Future<Response> post(String path, {dynamic data}) async { |
| 134 | + try { |
| 135 | + final response = await _dio.post(path, data: data); |
| 136 | + return response; |
| 137 | + } catch (e) { |
| 138 | + rethrow; |
| 139 | + } |
| 140 | + } |
| 141 | +} |
0 commit comments