2024-05-25 16:11:00 +00:00
|
|
|
import 'package:get/get.dart';
|
2024-08-21 07:39:29 +00:00
|
|
|
import 'package:solian/exceptions/request.dart';
|
2024-08-21 07:25:50 +00:00
|
|
|
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();
|
2024-08-21 07:25:50 +00:00
|
|
|
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();
|
2024-08-21 07:25:50 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-05-29 14:42:11 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-29 14:42:11 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.get('/realms/$alias');
|
2024-05-29 14:42:11 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-29 14:42:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
|
2024-05-25 16:11:00 +00:00
|
|
|
Future<Response> listAvailableRealm() async {
|
|
|
|
final AuthProvider auth = Get.find();
|
2024-08-21 07:25:50 +00:00
|
|
|
if (auth.isAuthorized.isFalse) throw const UnauthorizedException();
|
2024-05-25 16:11:00 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final client = auth.configureClient('auth');
|
2024-05-25 16:11:00 +00:00
|
|
|
|
2024-07-16 11:46:53 +00:00
|
|
|
final resp = await client.get('/realms/me/available');
|
2024-05-25 16:11:00 +00:00
|
|
|
if (resp.statusCode != 200) {
|
2024-08-21 07:39:29 +00:00
|
|
|
throw RequestException(resp);
|
2024-05-25 16:11:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return resp;
|
|
|
|
}
|
|
|
|
}
|