Solian/lib/providers/content/realm.dart

51 lines
1.4 KiB
Dart
Raw Normal View History

2024-05-25 16:11:00 +00:00
import 'package:get/get.dart';
import 'package:solian/exceptions/unauthorized.dart';
2024-08-01 07:21:43 +00:00
import 'package:solian/models/realm.dart';
2024-05-25 16:11:00 +00:00
import 'package:solian/providers/auth.dart';
class RealmProvider extends GetxController {
2024-08-01 07:21:43 +00:00
RxBool isLoading = false.obs;
RxList<Realm> availableRealms = RxList.empty(growable: true);
Future<void> refreshAvailableRealms() async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-08-01 07:21:43 +00:00
isLoading.value = true;
final resp = await listAvailableRealm();
isLoading.value = false;
availableRealms.value =
resp.body.map((x) => Realm.fromJson(x)).toList().cast<Realm>();
availableRealms.refresh();
}
2024-05-29 14:42:11 +00:00
Future<Response> getRealm(String alias) async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-29 14:42:11 +00:00
final client = auth.configureClient('auth');
2024-05-29 14:42:11 +00:00
final resp = await client.get('/realms/$alias');
2024-05-29 14:42:11 +00:00
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
2024-05-25 16:11:00 +00:00
Future<Response> listAvailableRealm() async {
final AuthProvider auth = Get.find();
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
2024-05-25 16:11:00 +00:00
final client = auth.configureClient('auth');
2024-05-25 16:11:00 +00:00
final resp = await client.get('/realms/me/available');
2024-05-25 16:11:00 +00:00
if (resp.statusCode != 200) {
throw Exception(resp.bodyString);
}
return resp;
}
}