Load profile information on startup
This commit is contained in:
@ -1,6 +1,7 @@
|
||||
import 'package:dart_jsonwebtoken/dart_jsonwebtoken.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:moneymgr_mobile/services/api/api_token.dart';
|
||||
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
||||
import 'package:moneymgr_mobile/services/storage/secure_storage.dart';
|
||||
@ -24,6 +25,7 @@ class ApiClient {
|
||||
|
||||
/// Get Dio instance
|
||||
Future<Response<T>> execute<T>(String uri, {String method = "GET"}) async {
|
||||
Logger.root.fine("Request on ${token.apiUrl} - URI $uri");
|
||||
return client.request(
|
||||
uri,
|
||||
options: Options(
|
||||
|
@ -1,10 +1,12 @@
|
||||
// ignore_for_file: non_constant_identifier_names
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:moneymgr_mobile/services/api/api_client.dart';
|
||||
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
||||
|
||||
part 'auth_api.g.dart';
|
||||
part 'auth_api.freezed.dart';
|
||||
part 'auth_api.g.dart';
|
||||
|
||||
@freezed
|
||||
abstract class AuthInfo with _$AuthInfo {
|
||||
@ -27,4 +29,24 @@ extension AuthApi on ApiClient {
|
||||
final response = await execute("/auth/info", method: "GET");
|
||||
return AuthInfo.fromJson(response.data);
|
||||
}
|
||||
|
||||
/// Get authentication information, returning cached information in case of failure
|
||||
Future<AuthInfo> authInfoOrCache() async {
|
||||
try {
|
||||
final config = await authInfo();
|
||||
this.prefs.setAuthInfo(config);
|
||||
return config;
|
||||
} catch (e, s) {
|
||||
Logger.root.warning("Failed to fetch user information! $e $s");
|
||||
}
|
||||
|
||||
final cached = this.prefs.authInfo();
|
||||
if (cached == null) {
|
||||
throw Exception(
|
||||
"Could not fetch user information, cached version is unavailable!",
|
||||
);
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -1,10 +1,8 @@
|
||||
// ignore_for_file: non_constant_identifier_names
|
||||
|
||||
import 'package:freezed_annotation/freezed_annotation.dart';
|
||||
import 'package:hooks_riverpod/hooks_riverpod.dart';
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:moneymgr_mobile/services/storage/prefs.dart';
|
||||
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
||||
|
||||
import 'api_client.dart';
|
||||
|
||||
@ -40,33 +38,30 @@ abstract class ServerConfig with _$ServerConfig {
|
||||
|
||||
/// Auth API
|
||||
extension ServerApi on ApiClient {
|
||||
/// Get authentication information from server
|
||||
/// Get server configuration
|
||||
Future<ServerConfig> serverConfig() async {
|
||||
final response = await execute("/server/config", method: "GET");
|
||||
return ServerConfig.fromJson(response.data);
|
||||
}
|
||||
|
||||
/// Get server configuration, or retrieve cached information (if available, in
|
||||
/// case of failure)
|
||||
Future<ServerConfig> serverConfigOrCache() async {
|
||||
try {
|
||||
final config = await serverConfig();
|
||||
this.prefs.setServerConfig(config);
|
||||
return config;
|
||||
} catch (e, s) {
|
||||
Logger.root.warning("Failed to fetch server configuration! $e $s");
|
||||
}
|
||||
|
||||
final cached = this.prefs.serverConfig();
|
||||
if (cached == null) {
|
||||
throw Exception(
|
||||
"Could not fetch server configuration, cached version is unavailable!",
|
||||
);
|
||||
}
|
||||
return cached;
|
||||
}
|
||||
}
|
||||
|
||||
/// Get authentication information from server, or retrieve cached information (if available, in
|
||||
/// case of failure)
|
||||
@riverpod
|
||||
Future<ServerConfig> serverConfigOrCache(Ref ref) async {
|
||||
final client = ref.watch(apiServiceProvider)!;
|
||||
try {
|
||||
final config = await client.serverConfig();
|
||||
client.prefs.setServerConfig(config);
|
||||
return config;
|
||||
} catch (e, s) {
|
||||
Logger.root.warning("Failed to fetch server configuration! $e $s");
|
||||
}
|
||||
|
||||
final cached = client.prefs.serverConfig();
|
||||
if (cached == null) {
|
||||
throw Exception(
|
||||
"Could not fetch server configuration, cached version is unavailable!",
|
||||
);
|
||||
}
|
||||
return cached;
|
||||
}
|
Reference in New Issue
Block a user