142 lines
4.6 KiB
Dart
142 lines
4.6 KiB
Dart
import 'dart:convert';
|
|
import 'package:flutter_cache_manager/flutter_cache_manager.dart';
|
|
import 'package:http/http.dart';
|
|
import '../../../Helper/Logger.dart';
|
|
import '../../../Model/Response/Customers/GetCustomerById/GetCustomerByTenantId.dart';
|
|
import '../../../Model/Response/Customers/GetCustomerById/Getdropcustomerbytenantid.dart';
|
|
|
|
|
|
class GetCustomersByTenantIdProvider {
|
|
|
|
static const cacheKey = 'customerCache';
|
|
final cacheManager = DefaultCacheManager();
|
|
|
|
Future<GetCustomerByTenantId?> getCustomerByTenantId(String urlData) async {
|
|
GetCustomerByTenantId? getCustomerByTenantId;
|
|
|
|
logger.i('GetCustomerURL : ${urlData}');
|
|
|
|
try {
|
|
/// 1. Attempt to load from cache first
|
|
final fileInfo = await cacheManager.getFileFromCache(cacheKey);
|
|
|
|
String? cachedData;
|
|
|
|
if (fileInfo != null && fileInfo.file.existsSync()) {
|
|
cachedData = await fileInfo.file.readAsString();
|
|
final parsedJson = json.decode(cachedData);
|
|
getCustomerByTenantId = GetCustomerByTenantId.fromJson(parsedJson);
|
|
logger.i("......Customer data Loaded from cache......");
|
|
}
|
|
|
|
/// 2.Always make a request to check for updates
|
|
final response = await get(Uri.parse(urlData), headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
});
|
|
|
|
if (response.statusCode == 200) {
|
|
final freshData = response.body;
|
|
|
|
/// Compare with cached data (optional optimization: hash/ETag/updatedAt)
|
|
if (cachedData == null || freshData != cachedData) {
|
|
logger.i("......Updating cache with fresh API data.......");
|
|
|
|
/// Update the cache with fresh data
|
|
await cacheManager.putFile(
|
|
cacheKey,
|
|
response.bodyBytes,
|
|
fileExtension: 'json',
|
|
key: cacheKey,
|
|
);
|
|
|
|
final parsedJson = json.decode(freshData);
|
|
getCustomerByTenantId = GetCustomerByTenantId.fromJson(parsedJson);
|
|
} else {
|
|
logger.i(".........Customer API data is same as cache, no update required..........");
|
|
}
|
|
} else {
|
|
logger.w("Customer API responded with status code: ${response.statusCode}");
|
|
}
|
|
} catch (e) {
|
|
logger.e("Error fetching customer deals: $e");
|
|
}
|
|
|
|
return getCustomerByTenantId;
|
|
}
|
|
|
|
|
|
Future<GetCustomerByTenantId?> getTenantsByTenantId(String urldata,) async {
|
|
GetCustomerByTenantId? getCustomerByTenantId;
|
|
try {
|
|
|
|
final url = Uri.parse('$urldata');
|
|
|
|
final response = await get(url, headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
// 'Authorization': '$token',
|
|
});
|
|
print('getCustomerByTenantId!!!!!!!!${response.body}');
|
|
print('getCustomerByTenantIdproviderurldata!!!!!!!!${urldata.toString()}');
|
|
Map<String, dynamic> parsedJson = json.decode(response.body.toString());
|
|
|
|
getCustomerByTenantId = GetCustomerByTenantId.fromJson(parsedJson);
|
|
|
|
} catch (e) {
|
|
print(e.toString());
|
|
print("error");
|
|
}
|
|
return getCustomerByTenantId;
|
|
}
|
|
|
|
|
|
Future<GetDropCustomerByTenantId?> getDropCustomerByTenantId(String urldata,) async {
|
|
GetDropCustomerByTenantId? getDropCustomerByTenantId;
|
|
try {
|
|
|
|
final url = Uri.parse('$urldata');
|
|
|
|
final response = await get(url, headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
// 'Authorization': '$token',
|
|
});
|
|
print('getDropCustomerByTenantIdresponse!!!!!!!!${response.body}');
|
|
print('getDropCustomerByTenantIdproviderurldata!!!!!!!!${urldata.toString()}');
|
|
Map<String, dynamic> parsedJson = json.decode(response.body.toString());
|
|
|
|
getDropCustomerByTenantId = GetDropCustomerByTenantId.fromJson(parsedJson);
|
|
|
|
} catch (e) {
|
|
print(e.toString());
|
|
print("error");
|
|
}
|
|
return getDropCustomerByTenantId;
|
|
}
|
|
|
|
Future<GetDropCustomerByTenantId?> getDropTenantByTenantId(String urldata,) async {
|
|
GetDropCustomerByTenantId? getDropCustomerByTenantId;
|
|
try {
|
|
|
|
final url = Uri.parse('$urldata');
|
|
|
|
final response = await get(url, headers: {
|
|
'Content-Type': 'application/json',
|
|
'Accept': 'application/json',
|
|
// 'Authorization': '$token',
|
|
});
|
|
print('getDropCustomerByTenantIdresponse!!!!!!!!${response.body}');
|
|
print('getDropCustomerByTenantIdproviderurldata!!!!!!!!${urldata.toString()}');
|
|
Map<String, dynamic> parsedJson = json.decode(response.body.toString());
|
|
|
|
getDropCustomerByTenantId = GetDropCustomerByTenantId.fromJson(parsedJson);
|
|
|
|
} catch (e) {
|
|
print(e.toString());
|
|
print("error");
|
|
}
|
|
return getDropCustomerByTenantId;
|
|
}
|
|
|
|
} |