loading state and error handling refactor
This commit is contained in:
@@ -1,10 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:http/http.dart' as http;
|
||||
|
||||
import '../../model/endpoint_data.dart';
|
||||
import '../apiParams.dart';
|
||||
import '../apiRequest.dart';
|
||||
import '../apiResponse.dart';
|
||||
import '../errors/network_exception.dart';
|
||||
import '../errors/parse_exception.dart';
|
||||
import 'queries/authenticate/authenticate.dart';
|
||||
import 'webuntisError.dart';
|
||||
|
||||
@@ -29,10 +34,15 @@ abstract class WebuntisApi extends ApiRequest {
|
||||
var data = await post(query, {'Cookie': 'JSESSIONID=$sessionId'});
|
||||
response = data;
|
||||
|
||||
dynamic jsonData = jsonDecode(data.body);
|
||||
final dynamic jsonData;
|
||||
try {
|
||||
jsonData = jsonDecode(data.body);
|
||||
} on FormatException catch (e) {
|
||||
throw ParseException(technicalDetails: 'WebUntis JSON decode: ${e.message}');
|
||||
}
|
||||
if(jsonData['error'] != null) {
|
||||
if(jsonData['error']['code'] == -8520) {
|
||||
if(retry) throw WebuntisError('Authentication was tried (probably session timeout), but was not successful!', 1);
|
||||
if(retry) throw WebuntisError('Authentication was tried (probably session timeout), but was not successful!', -8520);
|
||||
await Authenticate.createSession();
|
||||
return await this.query(untis, retry: true);
|
||||
} else {
|
||||
@@ -51,10 +61,16 @@ abstract class WebuntisApi extends ApiRequest {
|
||||
|
||||
String _body() => genericParam == null ? '{}' : jsonEncode(genericParam);
|
||||
|
||||
Future<http.Response> post(String data, Map<String, String>? headers) async => await http
|
||||
.post(endpoint, body: data, headers: headers)
|
||||
.timeout(
|
||||
const Duration(seconds: 10),
|
||||
onTimeout: () => throw WebuntisError('Timeout', 1)
|
||||
);
|
||||
Future<http.Response> post(String data, Map<String, String>? headers) async {
|
||||
try {
|
||||
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'),
|
||||
);
|
||||
} on SocketException catch (e) {
|
||||
throw NetworkException(technicalDetails: 'WebUntis $method: ${e.message}');
|
||||
} on http.ClientException catch (e) {
|
||||
throw NetworkException(technicalDetails: 'WebUntis $method: ${e.message}');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user