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!;
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,16 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:marianum_mobile/api/webuntis/apiParams.dart';
|
||||
|
||||
part 'authenticateParams.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class AuthenticateParams extends ApiParams {
|
||||
|
||||
String user;
|
||||
String password;
|
||||
|
||||
AuthenticateParams({required this.user, required this.password});
|
||||
factory AuthenticateParams.fromJson(Map<String, dynamic> json) => _$AuthenticateParamsFromJson(json);
|
||||
|
||||
Map<String, dynamic> toJson() => _$AuthenticateParamsToJson(this);
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'authenticateParams.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
AuthenticateParams _$AuthenticateParamsFromJson(Map<String, dynamic> json) =>
|
||||
AuthenticateParams(
|
||||
user: json['user'] as String,
|
||||
password: json['password'] as String,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$AuthenticateParamsToJson(AuthenticateParams instance) =>
|
||||
<String, dynamic>{
|
||||
'user': instance.user,
|
||||
'password': instance.password,
|
||||
};
|
@ -0,0 +1,18 @@
|
||||
import 'package:json_annotation/json_annotation.dart';
|
||||
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
|
||||
|
||||
part 'authenticateResponse.g.dart';
|
||||
|
||||
@JsonSerializable()
|
||||
class AuthenticateResponse extends ApiResponse {
|
||||
|
||||
String sessionId;
|
||||
int personType;
|
||||
int personId;
|
||||
int klasseId;
|
||||
|
||||
AuthenticateResponse(this.sessionId, this.personType, this.personId, this.klasseId);
|
||||
|
||||
factory AuthenticateResponse.fromJson(Map<String, dynamic> json) => _$AuthenticateResponseFromJson(json);
|
||||
Map<String, dynamic> toJson() => _$AuthenticateResponseToJson(this);
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
// GENERATED CODE - DO NOT MODIFY BY HAND
|
||||
|
||||
part of 'authenticateResponse.dart';
|
||||
|
||||
// **************************************************************************
|
||||
// JsonSerializableGenerator
|
||||
// **************************************************************************
|
||||
|
||||
AuthenticateResponse _$AuthenticateResponseFromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
AuthenticateResponse(
|
||||
json['sessionId'] as String,
|
||||
json['personType'] as int,
|
||||
json['personId'] as int,
|
||||
json['klasseId'] as int,
|
||||
);
|
||||
|
||||
Map<String, dynamic> _$AuthenticateResponseToJson(
|
||||
AuthenticateResponse instance) =>
|
||||
<String, dynamic>{
|
||||
'sessionId': instance.sessionId,
|
||||
'personType': instance.personType,
|
||||
'personId': instance.personId,
|
||||
'klasseId': instance.klasseId,
|
||||
};
|
Reference in New Issue
Block a user