Cache LinagoraEcosystem to avoid multiple network calls

This commit is contained in:
Théo Poizat
2026-02-26 10:43:18 +01:00
committed by Dat H. Pham
parent 71901b2640
commit 6d8d10cf26
4 changed files with 164 additions and 22 deletions
@@ -5,6 +5,7 @@ import 'package:core/data/network/dio_client.dart';
import 'package:core/utils/app_logger.dart';
import 'package:tmail_ui_user/features/login/data/extensions/service_path_extension.dart';
import 'package:tmail_ui_user/features/login/data/network/endpoint.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/data/cache/linagora_ecosystem_cache.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/exceptions/linagora_ecosystem_exceptions.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/linagora_ecosystem/api_url_linagora_ecosystem.dart';
import 'package:tmail_ui_user/features/mailbox_dashboard/domain/linagora_ecosystem/linagora_ecosystem.dart';
@@ -17,45 +18,51 @@ class LinagoraEcosystemApi {
LinagoraEcosystemApi(this._dioClient);
Future<LinagoraEcosystem> getLinagoraEcosystem(String baseUrl) async {
final cachedEcosystem = LinagoraEcosystemCache().getCachedEcosystem(baseUrl);
if (cachedEcosystem != null) {
log('LinagoraEcosystemApi::getLinagoraEcosystem: Using cached data');
return cachedEcosystem;
}
final result = await _dioClient.get(
Endpoint.linagoraEcosystem.usingBaseUrl(baseUrl).generateEndpointPath(),
);
log('LinagoraEcosystemApi::getLinagoraEcosystem: $result');
LinagoraEcosystem ecosystem;
if (result is Map<String, dynamic>) {
return LinagoraEcosystem.deserialize(result);
ecosystem = LinagoraEcosystem.deserialize(result);
} else if (result is String) {
return LinagoraEcosystem.deserialize(jsonDecode(result));
ecosystem = LinagoraEcosystem.deserialize(jsonDecode(result));
} else {
throw NotFoundLinagoraEcosystem();
}
LinagoraEcosystemCache().cacheEcosystem(ecosystem, baseUrl);
return ecosystem;
}
Future<PaywallUrlPattern> getPaywallUrl(String baseUrl) async {
final result = await _dioClient.get(
Endpoint.linagoraEcosystem.usingBaseUrl(baseUrl).generateEndpointPath(),
);
log('LinagoraEcosystemApi::getPaywallUrl: $result');
LinagoraEcosystem? linagoraEcosystem;
if (result is Map<String, dynamic>) {
linagoraEcosystem = LinagoraEcosystem.deserialize(result);
} else if (result is String) {
linagoraEcosystem = LinagoraEcosystem.deserialize(jsonDecode(result));
try {
linagoraEcosystem = await getLinagoraEcosystem(baseUrl);
} catch (exception) {
if (exception is NotFoundLinagoraEcosystem) {
throw NotFoundPaywallUrl();
}
rethrow;
}
if (linagoraEcosystem == null) {
throw NotFoundLinagoraEcosystem();
} else {
final paywallProperty =
linagoraEcosystem.properties?[LinagoraEcosystemIdentifier.paywallURL] as ApiUrlLinagoraEcosystem?;
log('LinagoraEcosystemApi::getPaywallUrl: paywallProperty = $paywallProperty');
final paywallProperty =
linagoraEcosystem.properties?[LinagoraEcosystemIdentifier.paywallURL] as ApiUrlLinagoraEcosystem?;
log('LinagoraEcosystemApi::getPaywallUrl: paywallProperty = $paywallProperty');
if (paywallProperty == null) {
throw NotFoundPaywallUrl();
} else {
return PaywallUrlPattern(paywallProperty.value);
}
if (paywallProperty == null) {
throw NotFoundPaywallUrl();
} else {
return PaywallUrlPattern(paywallProperty.value);
}
}