Implement local HTTP Api usage

This commit is contained in:
2023-02-14 18:06:18 +01:00
parent 83ad7d59d2
commit 7432972b3c
27 changed files with 1300 additions and 59 deletions

View 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!;
}
}

View File

@ -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);
}

View File

@ -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,
};

View File

@ -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);
}

View File

@ -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,
};

View File

@ -0,0 +1,17 @@
import 'dart:convert';
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
import 'package:marianum_mobile/api/webuntis/webuntisApi.dart';
import 'getRoomsResponse.dart';
class GetRooms extends WebuntisApi {
GetRooms() : super("getRooms", null);
@override
Future<GetRoomsResponse> run() async {
String rawAnswer = await query(this);
return finalize(GetRoomsResponse.fromJson(jsonDecode(rawAnswer)));
}
}

View File

@ -0,0 +1,29 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
part 'getRoomsResponse.g.dart';
@JsonSerializable()
class GetRoomsResponse extends ApiResponse {
Set<GetRoomsResponseObject> result;
GetRoomsResponse(this.result);
factory GetRoomsResponse.fromJson(Map<String, dynamic> json) => _$GetRoomsResponseFromJson(json);
Map<String, dynamic> toJson() => _$GetRoomsResponseToJson(this);
}
@JsonSerializable(explicitToJson: true)
class GetRoomsResponseObject {
int id;
String name;
String longName;
bool active;
String building;
GetRoomsResponseObject(this.id, this.name, this.longName, this.active, this.building);
factory GetRoomsResponseObject.fromJson(Map<String, dynamic> json) => _$GetRoomsResponseObjectFromJson(json);
Map<String, dynamic> toJson() => _$GetRoomsResponseObjectToJson(this);
}

View File

@ -0,0 +1,40 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'getRoomsResponse.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GetRoomsResponse _$GetRoomsResponseFromJson(Map<String, dynamic> json) =>
GetRoomsResponse(
(json['result'] as List<dynamic>)
.map(
(e) => GetRoomsResponseObject.fromJson(e as Map<String, dynamic>))
.toSet(),
);
Map<String, dynamic> _$GetRoomsResponseToJson(GetRoomsResponse instance) =>
<String, dynamic>{
'result': instance.result.toList(),
};
GetRoomsResponseObject _$GetRoomsResponseObjectFromJson(
Map<String, dynamic> json) =>
GetRoomsResponseObject(
json['id'] as int,
json['name'] as String,
json['longName'] as String,
json['active'] as bool,
json['building'] as String,
);
Map<String, dynamic> _$GetRoomsResponseObjectToJson(
GetRoomsResponseObject instance) =>
<String, dynamic>{
'id': instance.id,
'name': instance.name,
'longName': instance.longName,
'active': instance.active,
'building': instance.building,
};

View File

@ -0,0 +1,22 @@
import 'dart:convert';
import 'dart:developer';
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
import 'package:marianum_mobile/api/webuntis/webuntisApi.dart';
import 'getTimetableParams.dart';
import 'getTimetableResponse.dart';
class GetTimetable extends WebuntisApi {
GetTimetableParams params;
GetTimetable(this.params) : super("getTimetable", params);
@override
Future<GetTimetableResponse> run() async {
String rawAnswer = await query(this);
log(rawAnswer);
return finalize(GetTimetableResponse.fromJson(jsonDecode(rawAnswer)));
}
}

View File

@ -0,0 +1,91 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:marianum_mobile/api/webuntis/apiParams.dart';
part 'getTimetableParams.g.dart';
@JsonSerializable(explicitToJson: true)
class GetTimetableParams extends ApiParams {
GetTimetableParamsOptions options;
GetTimetableParams({required this.options});
factory GetTimetableParams.fromJson(Map<String, dynamic> json) => _$GetTimetableParamsFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableParamsToJson(this);
}
@JsonSerializable(explicitToJson: true)
class GetTimetableParamsOptions {
GetTimetableParamsOptionsElement element;
@JsonKey(includeIfNull: false)
int? startDate;
@JsonKey(includeIfNull: false)
int? endDate;
@JsonKey(includeIfNull: false)
bool? onlyBaseTimetable;
@JsonKey(includeIfNull: false)
bool? showBooking;
@JsonKey(includeIfNull: false)
bool? showInfo;
@JsonKey(includeIfNull: false)
bool? showSubstText;
@JsonKey(includeIfNull: false)
bool? showLsText;
@JsonKey(includeIfNull: false)
bool? showLsNumber;
@JsonKey(includeIfNull: false)
bool? showStudentgroup;
@JsonKey(includeIfNull: false)
GetTimetableParamsOptionsFields? klasseFields;
@JsonKey(includeIfNull: false)
GetTimetableParamsOptionsFields? roomFields;
@JsonKey(includeIfNull: false)
GetTimetableParamsOptionsFields? subjectFields;
@JsonKey(includeIfNull: false)
GetTimetableParamsOptionsFields? teacherFields;
GetTimetableParamsOptions({
required this.element,
this.startDate,
this.endDate,
this.onlyBaseTimetable,
this.showBooking,
this.showInfo,
this.showSubstText,
this.showLsText,
this.showLsNumber,
this.showStudentgroup,
this.klasseFields,
this.roomFields,
this.subjectFields,
this.teacherFields
});
factory GetTimetableParamsOptions.fromJson(Map<String, dynamic> json) => _$GetTimetableParamsOptionsFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableParamsOptionsToJson(this);
}
enum GetTimetableParamsOptionsFields {
@JsonValue("id") id,
@JsonValue("name") name,
@JsonValue("longname") longname,
@JsonValue("externalkey") externalkey,
}
@JsonSerializable()
class GetTimetableParamsOptionsElement {
int id;
int type;
@JsonKey(includeIfNull: false)
GetTimetableParamsOptionsElementKeyType? keyType;
GetTimetableParamsOptionsElement({required this.id, required this.type, this.keyType});
factory GetTimetableParamsOptionsElement.fromJson(Map<String, dynamic> json) => _$GetTimetableParamsOptionsElementFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableParamsOptionsElementToJson(this);
}
enum GetTimetableParamsOptionsElementKeyType {
@JsonValue("id") id,
@JsonValue("name") name,
@JsonValue("externalkey") externalkey
}

View File

@ -0,0 +1,114 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'getTimetableParams.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GetTimetableParams _$GetTimetableParamsFromJson(Map<String, dynamic> json) =>
GetTimetableParams(
options: GetTimetableParamsOptions.fromJson(
json['options'] as Map<String, dynamic>),
);
Map<String, dynamic> _$GetTimetableParamsToJson(GetTimetableParams instance) =>
<String, dynamic>{
'options': instance.options.toJson(),
};
GetTimetableParamsOptions _$GetTimetableParamsOptionsFromJson(
Map<String, dynamic> json) =>
GetTimetableParamsOptions(
element: GetTimetableParamsOptionsElement.fromJson(
json['element'] as Map<String, dynamic>),
startDate: json['startDate'] as int?,
endDate: json['endDate'] as int?,
onlyBaseTimetable: json['onlyBaseTimetable'] as bool?,
showBooking: json['showBooking'] as bool?,
showInfo: json['showInfo'] as bool?,
showSubstText: json['showSubstText'] as bool?,
showLsText: json['showLsText'] as bool?,
showLsNumber: json['showLsNumber'] as bool?,
showStudentgroup: json['showStudentgroup'] as bool?,
klasseFields: $enumDecodeNullable(
_$GetTimetableParamsOptionsFieldsEnumMap, json['klasseFields']),
roomFields: $enumDecodeNullable(
_$GetTimetableParamsOptionsFieldsEnumMap, json['roomFields']),
subjectFields: $enumDecodeNullable(
_$GetTimetableParamsOptionsFieldsEnumMap, json['subjectFields']),
teacherFields: $enumDecodeNullable(
_$GetTimetableParamsOptionsFieldsEnumMap, json['teacherFields']),
);
Map<String, dynamic> _$GetTimetableParamsOptionsToJson(
GetTimetableParamsOptions instance) {
final val = <String, dynamic>{
'element': instance.element.toJson(),
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('startDate', instance.startDate);
writeNotNull('endDate', instance.endDate);
writeNotNull('onlyBaseTimetable', instance.onlyBaseTimetable);
writeNotNull('showBooking', instance.showBooking);
writeNotNull('showInfo', instance.showInfo);
writeNotNull('showSubstText', instance.showSubstText);
writeNotNull('showLsText', instance.showLsText);
writeNotNull('showLsNumber', instance.showLsNumber);
writeNotNull('showStudentgroup', instance.showStudentgroup);
writeNotNull('klasseFields',
_$GetTimetableParamsOptionsFieldsEnumMap[instance.klasseFields]);
writeNotNull('roomFields',
_$GetTimetableParamsOptionsFieldsEnumMap[instance.roomFields]);
writeNotNull('subjectFields',
_$GetTimetableParamsOptionsFieldsEnumMap[instance.subjectFields]);
writeNotNull('teacherFields',
_$GetTimetableParamsOptionsFieldsEnumMap[instance.teacherFields]);
return val;
}
const _$GetTimetableParamsOptionsFieldsEnumMap = {
GetTimetableParamsOptionsFields.id: 'id',
GetTimetableParamsOptionsFields.name: 'name',
GetTimetableParamsOptionsFields.longname: 'longname',
GetTimetableParamsOptionsFields.externalkey: 'externalkey',
};
GetTimetableParamsOptionsElement _$GetTimetableParamsOptionsElementFromJson(
Map<String, dynamic> json) =>
GetTimetableParamsOptionsElement(
id: json['id'] as int,
type: json['type'] as int,
keyType: $enumDecodeNullable(
_$GetTimetableParamsOptionsElementKeyTypeEnumMap, json['keyType']),
);
Map<String, dynamic> _$GetTimetableParamsOptionsElementToJson(
GetTimetableParamsOptionsElement instance) {
final val = <String, dynamic>{
'id': instance.id,
'type': instance.type,
};
void writeNotNull(String key, dynamic value) {
if (value != null) {
val[key] = value;
}
}
writeNotNull('keyType',
_$GetTimetableParamsOptionsElementKeyTypeEnumMap[instance.keyType]);
return val;
}
const _$GetTimetableParamsOptionsElementKeyTypeEnumMap = {
GetTimetableParamsOptionsElementKeyType.id: 'id',
GetTimetableParamsOptionsElementKeyType.name: 'name',
GetTimetableParamsOptionsElementKeyType.externalkey: 'externalkey',
};

View File

@ -0,0 +1,85 @@
import 'package:json_annotation/json_annotation.dart';
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
part 'getTimetableResponse.g.dart';
@JsonSerializable(explicitToJson: true)
class GetTimetableResponse extends ApiResponse {
Set<GetTimetableResponseObject> result;
GetTimetableResponse(this.result);
factory GetTimetableResponse.fromJson(Map<String, dynamic> json) => _$GetTimetableResponseFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableResponseToJson(this);
}
@JsonSerializable(explicitToJson: true)
class GetTimetableResponseObject {
int id;
int date;
int startTime;
int endTime;
String? lstype;
String? code;
String? info;
String? substText;
String? lstext;
int? lsnumber;
String? statflags;
String? activityType;
String? sg;
String? bkRemark;
String? bkText;
List<dynamic> kl;
List<dynamic> te;
List<dynamic> su;
List<dynamic> ro;
GetTimetableResponseObject({
required this.id,
required this.date,
required this.startTime,
required this.endTime,
this.lstype,
this.code,
this.info,
this.substText,
this.lstext,
this.lsnumber,
this.statflags,
this.activityType,
this.sg,
this.bkRemark,
required this.kl,
required this.te,
required this.su,
required this.ro
});
factory GetTimetableResponseObject.fromJson(Map<String, dynamic> json) => _$GetTimetableResponseObjectFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableResponseObjectToJson(this);
}
@JsonSerializable(explicitToJson: true)
class GetTimetableResponseObjectFields {
List<GetTimetableResponseObjectFieldsObject>? te;
GetTimetableResponseObjectFields(this.te);
factory GetTimetableResponseObjectFields.fromJson(Map<String, dynamic> json) => _$GetTimetableResponseObjectFieldsFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableResponseObjectFieldsToJson(this);
}
@JsonSerializable()
class GetTimetableResponseObjectFieldsObject {
int? id;
String? name;
String? longname;
String? externalkey;
GetTimetableResponseObjectFieldsObject({this.id, this.name, this.longname, this.externalkey});
factory GetTimetableResponseObjectFieldsObject.fromJson(Map<String, dynamic> json) => _$GetTimetableResponseObjectFieldsObjectFromJson(json);
Map<String, dynamic> toJson() => _$GetTimetableResponseObjectFieldsObjectToJson(this);
}

View File

@ -0,0 +1,103 @@
// GENERATED CODE - DO NOT MODIFY BY HAND
part of 'getTimetableResponse.dart';
// **************************************************************************
// JsonSerializableGenerator
// **************************************************************************
GetTimetableResponse _$GetTimetableResponseFromJson(
Map<String, dynamic> json) =>
GetTimetableResponse(
(json['result'] as List<dynamic>)
.map((e) =>
GetTimetableResponseObject.fromJson(e as Map<String, dynamic>))
.toSet(),
);
Map<String, dynamic> _$GetTimetableResponseToJson(
GetTimetableResponse instance) =>
<String, dynamic>{
'result': instance.result.map((e) => e.toJson()).toList(),
};
GetTimetableResponseObject _$GetTimetableResponseObjectFromJson(
Map<String, dynamic> json) =>
GetTimetableResponseObject(
id: json['id'] as int,
date: json['date'] as int,
startTime: json['startTime'] as int,
endTime: json['endTime'] as int,
lstype: json['lstype'] as String?,
code: json['code'] as String?,
info: json['info'] as String?,
substText: json['substText'] as String?,
lstext: json['lstext'] as String?,
lsnumber: json['lsnumber'] as int?,
statflags: json['statflags'] as String?,
activityType: json['activityType'] as String?,
sg: json['sg'] as String?,
bkRemark: json['bkRemark'] as String?,
kl: json['kl'] as List<dynamic>,
te: json['te'] as List<dynamic>,
su: json['su'] as List<dynamic>,
ro: json['ro'] as List<dynamic>,
)..bkText = json['bkText'] as String?;
Map<String, dynamic> _$GetTimetableResponseObjectToJson(
GetTimetableResponseObject instance) =>
<String, dynamic>{
'id': instance.id,
'date': instance.date,
'startTime': instance.startTime,
'endTime': instance.endTime,
'lstype': instance.lstype,
'code': instance.code,
'info': instance.info,
'substText': instance.substText,
'lstext': instance.lstext,
'lsnumber': instance.lsnumber,
'statflags': instance.statflags,
'activityType': instance.activityType,
'sg': instance.sg,
'bkRemark': instance.bkRemark,
'bkText': instance.bkText,
'kl': instance.kl,
'te': instance.te,
'su': instance.su,
'ro': instance.ro,
};
GetTimetableResponseObjectFields _$GetTimetableResponseObjectFieldsFromJson(
Map<String, dynamic> json) =>
GetTimetableResponseObjectFields(
(json['te'] as List<dynamic>?)
?.map((e) => GetTimetableResponseObjectFieldsObject.fromJson(
e as Map<String, dynamic>))
.toList(),
);
Map<String, dynamic> _$GetTimetableResponseObjectFieldsToJson(
GetTimetableResponseObjectFields instance) =>
<String, dynamic>{
'te': instance.te?.map((e) => e.toJson()).toList(),
};
GetTimetableResponseObjectFieldsObject
_$GetTimetableResponseObjectFieldsObjectFromJson(
Map<String, dynamic> json) =>
GetTimetableResponseObjectFieldsObject(
id: json['id'] as int?,
name: json['name'] as String?,
longname: json['longname'] as String?,
externalkey: json['externalkey'] as String?,
);
Map<String, dynamic> _$GetTimetableResponseObjectFieldsObjectToJson(
GetTimetableResponseObjectFieldsObject instance) =>
<String, dynamic>{
'id': instance.id,
'name': instance.name,
'longname': instance.longname,
'externalkey': instance.externalkey,
};