Implement local HTTP Api usage
This commit is contained in:
55
lib/api/webuntis/queries/authenticate/authenticate.dart
Normal file
55
lib/api/webuntis/queries/authenticate/authenticate.dart
Normal file
@ -0,0 +1,55 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:marianum_mobile/api/webuntis/webuntisApi.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'authenticateParams.dart';
|
||||
import 'authenticateResponse.dart';
|
||||
|
||||
class Authenticate extends WebuntisApi {
|
||||
AuthenticateParams param;
|
||||
|
||||
Authenticate(this.param) : super("authenticate", param, authenticatedResponse: false);
|
||||
|
||||
@override
|
||||
Future<AuthenticateResponse> run() async {
|
||||
awaitingResponse = true;
|
||||
String rawAnswer = await query(this);
|
||||
AuthenticateResponse response = finalize(AuthenticateResponse.fromJson(jsonDecode(rawAnswer)['result']));
|
||||
_lastResponse = response;
|
||||
awaitedResponse.complete();
|
||||
return response;
|
||||
}
|
||||
|
||||
static bool awaitingResponse = false;
|
||||
static Completer awaitedResponse = Completer();
|
||||
static AuthenticateResponse? _lastResponse;
|
||||
|
||||
static Future<void> createSession() async {
|
||||
SharedPreferences preferences = await SharedPreferences.getInstance();
|
||||
|
||||
_lastResponse = await Authenticate(
|
||||
AuthenticateParams(
|
||||
user: preferences.getString("username")!,
|
||||
password: preferences.getString("password")!,
|
||||
)
|
||||
).run();
|
||||
}
|
||||
|
||||
static Future<AuthenticateResponse> getSession() async {
|
||||
if(awaitingResponse) {
|
||||
log("Other query in progress... waiting");
|
||||
await awaitedResponse.future;
|
||||
}
|
||||
|
||||
if(_lastResponse == null) {
|
||||
log("Not authenticated... requesting");
|
||||
awaitingResponse = true;
|
||||
await createSession();
|
||||
}
|
||||
return _lastResponse!;
|
||||
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user