67 lines
1.9 KiB
Dart
67 lines
1.9 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
|
|
import '../../../../model/account_data.dart';
|
|
import '../../webuntis_api.dart';
|
|
import 'authenticate_params.dart';
|
|
import 'authenticate_response.dart';
|
|
|
|
class Authenticate extends WebuntisApi {
|
|
AuthenticateParams param;
|
|
|
|
Authenticate(this.param)
|
|
: super('authenticate', param, authenticatedResponse: false);
|
|
|
|
@override
|
|
Future<AuthenticateResponse> run() async {
|
|
awaitingResponse = true;
|
|
try {
|
|
final rawAnswer = await query(this);
|
|
final decoded = jsonDecode(rawAnswer) as Map<String, dynamic>;
|
|
final response = finalize(
|
|
AuthenticateResponse.fromJson(
|
|
decoded['result'] as Map<String, dynamic>,
|
|
),
|
|
);
|
|
_lastResponse = response;
|
|
if (!awaitedResponse.isCompleted) awaitedResponse.complete();
|
|
return response;
|
|
} catch (e) {
|
|
// Surface the error to anyone waiting on the current completer, then
|
|
// install a fresh one so a future attempt can succeed. Without this,
|
|
// any later call to getSession() would hang forever on a completer
|
|
// that is already settled with no listeners (or never settles at all).
|
|
if (!awaitedResponse.isCompleted) awaitedResponse.completeError(e);
|
|
awaitedResponse = Completer<void>();
|
|
rethrow;
|
|
} finally {
|
|
awaitingResponse = false;
|
|
}
|
|
}
|
|
|
|
static bool awaitingResponse = false;
|
|
static Completer<void> awaitedResponse = Completer<void>();
|
|
static AuthenticateResponse? _lastResponse;
|
|
|
|
static Future<void> createSession() async {
|
|
_lastResponse = await Authenticate(
|
|
AuthenticateParams(
|
|
user: AccountData().getUsername(),
|
|
password: AccountData().getPassword(),
|
|
),
|
|
).run();
|
|
}
|
|
|
|
static Future<AuthenticateResponse> getSession() async {
|
|
if (awaitingResponse) {
|
|
await awaitedResponse.future;
|
|
}
|
|
|
|
if (_lastResponse == null) {
|
|
awaitingResponse = true;
|
|
await createSession();
|
|
}
|
|
return _lastResponse!;
|
|
}
|
|
}
|