Implement local HTTP Api usage
This commit is contained in:
54
lib/api/webuntis/webuntisApi.dart
Normal file
54
lib/api/webuntis/webuntisApi.dart
Normal file
@ -0,0 +1,54 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:developer';
|
||||
import 'package:marianum_mobile/api/apiRequest.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:marianum_mobile/api/webuntis/webuntisError.dart';
|
||||
import 'package:marianum_mobile/api/webuntis/apiResponse.dart';
|
||||
|
||||
import 'apiParams.dart';
|
||||
import 'queries/authenticate/authenticate.dart';
|
||||
|
||||
abstract class WebuntisApi extends ApiRequest {
|
||||
String method;
|
||||
ApiParams? genericParam;
|
||||
http.Response? response;
|
||||
|
||||
bool authenticatedResponse;
|
||||
|
||||
WebuntisApi(this.method, this.genericParam, {this.authenticatedResponse = true}) : super(Uri.parse("https://peleus.webuntis.com/WebUntis/jsonrpc.do?school=marianum-fulda"));
|
||||
|
||||
|
||||
Future<String> query(WebuntisApi untis) async {
|
||||
String query = '{"id":"ID","method":"$method","params":${untis._body()},"jsonrpc":"2.0"}';
|
||||
log(query);
|
||||
|
||||
String sessionId = "0";
|
||||
if(authenticatedResponse) {
|
||||
sessionId = (await Authenticate.getSession()).sessionId;
|
||||
}
|
||||
http.Response data = await post(query, {"Cookie": "JSESSIONID=$sessionId"});
|
||||
response = data;
|
||||
|
||||
dynamic jsonData = jsonDecode(data.body);
|
||||
if(jsonData['error'] != null) {
|
||||
if(jsonData['error']['code'] == -8520) {
|
||||
await Authenticate.createSession();
|
||||
this.query(untis);
|
||||
} else {
|
||||
throw WebuntisError(jsonData['error']['message'], jsonData['error']['code']);
|
||||
}
|
||||
}
|
||||
return data.body;
|
||||
}
|
||||
|
||||
dynamic finalize(dynamic response) {
|
||||
response.rawResponse = this.response!;
|
||||
return response;
|
||||
}
|
||||
|
||||
Future<ApiResponse> run();
|
||||
|
||||
String _body() {
|
||||
return genericParam == null ? "{}" : jsonEncode(genericParam);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user