dart format
This commit is contained in:
@@ -14,18 +14,24 @@ import 'queries/authenticate/authenticate.dart';
|
||||
import 'webuntis_error.dart';
|
||||
|
||||
abstract class WebuntisApi extends ApiRequest {
|
||||
Uri endpoint = Uri.parse('https://${EndpointData().webuntis().full()}/WebUntis/jsonrpc.do?school=marianum-fulda');
|
||||
Uri endpoint = Uri.parse(
|
||||
'https://${EndpointData().webuntis().full()}/WebUntis/jsonrpc.do?school=marianum-fulda',
|
||||
);
|
||||
String method;
|
||||
ApiParams? genericParam;
|
||||
http.Response? response;
|
||||
|
||||
bool authenticatedResponse;
|
||||
|
||||
WebuntisApi(this.method, this.genericParam, {this.authenticatedResponse = true});
|
||||
|
||||
WebuntisApi(
|
||||
this.method,
|
||||
this.genericParam, {
|
||||
this.authenticatedResponse = true,
|
||||
});
|
||||
|
||||
Future<String> query(WebuntisApi untis, {bool retry = false}) async {
|
||||
final body = '{"id":"ID","method":"$method","params":${untis._body()},"jsonrpc":"2.0"}';
|
||||
final body =
|
||||
'{"id":"ID","method":"$method","params":${untis._body()},"jsonrpc":"2.0"}';
|
||||
|
||||
var sessionId = '0';
|
||||
if (authenticatedResponse) {
|
||||
@@ -38,13 +44,20 @@ abstract class WebuntisApi extends ApiRequest {
|
||||
try {
|
||||
jsonData = jsonDecode(data.body) as Map<String, dynamic>;
|
||||
} on FormatException catch (e) {
|
||||
throw ParseException(technicalDetails: 'WebUntis JSON decode: ${e.message}');
|
||||
throw ParseException(
|
||||
technicalDetails: 'WebUntis JSON decode: ${e.message}',
|
||||
);
|
||||
}
|
||||
final error = jsonData['error'] as Map<String, dynamic>?;
|
||||
if (error != null) {
|
||||
final code = error['code'] as int;
|
||||
if (code == -8520) {
|
||||
if (retry) throw WebuntisError('Authentication was tried (probably session timeout), but was not successful!', -8520);
|
||||
if (retry) {
|
||||
throw WebuntisError(
|
||||
'Authentication was tried (probably session timeout), but was not successful!',
|
||||
-8520,
|
||||
);
|
||||
}
|
||||
await Authenticate.createSession();
|
||||
return query(untis, retry: true);
|
||||
} else {
|
||||
@@ -65,14 +78,22 @@ abstract class WebuntisApi extends ApiRequest {
|
||||
|
||||
Future<http.Response> post(String data, Map<String, String>? headers) async {
|
||||
try {
|
||||
return await http.post(endpoint, body: data, headers: headers).timeout(
|
||||
return await http
|
||||
.post(endpoint, body: data, headers: headers)
|
||||
.timeout(
|
||||
const Duration(seconds: 10),
|
||||
onTimeout: () => throw NetworkException.timeout(technicalDetails: 'WebUntis $method timed out after 10s'),
|
||||
onTimeout: () => throw NetworkException.timeout(
|
||||
technicalDetails: 'WebUntis $method timed out after 10s',
|
||||
),
|
||||
);
|
||||
} on SocketException catch (e) {
|
||||
throw NetworkException(technicalDetails: 'WebUntis $method: ${e.message}');
|
||||
throw NetworkException(
|
||||
technicalDetails: 'WebUntis $method: ${e.message}',
|
||||
);
|
||||
} on http.ClientException catch (e) {
|
||||
throw NetworkException(technicalDetails: 'WebUntis $method: ${e.message}');
|
||||
throw NetworkException(
|
||||
technicalDetails: 'WebUntis $method: ${e.message}',
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user