Skip to content
Snippets Groups Projects
Commit 68df2013 authored by doodlezucc's avatar doodlezucc
Browse files

use Set-Cookie response

parent a4c68ccd
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,10 @@ abstract class Cacheable<T> { ...@@ -21,6 +21,10 @@ abstract class Cacheable<T> {
return _cached!; return _cached!;
} }
void invalidateCache() {
_cached = null;
}
Future<Json> _request() { Future<Json> _request() {
return untis.requestOptions(request); return untis.requestOptions(request);
} }
......
...@@ -11,8 +11,7 @@ import 'element_type.dart'; ...@@ -11,8 +11,7 @@ import 'element_type.dart';
import 'lecture.dart'; import 'lecture.dart';
typedef RequestOptions = (String path, String method, Json params); typedef RequestOptions = (String path, String method, Json params);
typedef SimpleCookieMap = Map<String, String>; typedef JsonWithCookies = (Json json, List<Cookie> cookies);
typedef JsonWithCookies = (Json json, SimpleCookieMap cookies);
class UntisFetch { class UntisFetch {
static const String apiBase = '/WebUntis/api/public'; static const String apiBase = '/WebUntis/api/public';
...@@ -25,7 +24,7 @@ class UntisFetch { ...@@ -25,7 +24,7 @@ class UntisFetch {
final HttpClient client = HttpClient(); final HttpClient client = HttpClient();
final Uri serverHost = Uri.https('poly.webuntis.com'); final Uri serverHost = Uri.https('poly.webuntis.com');
final SimpleCookieMap _sessionCookies = {}; final List<Cookie> _sessionCookies = [];
late PageConfig<Class> allClasses = PageConfig( late PageConfig<Class> allClasses = PageConfig(
this, this,
...@@ -60,10 +59,7 @@ class UntisFetch { ...@@ -60,10 +59,7 @@ class UntisFetch {
final request = await client.openUrl(method, uri); final request = await client.openUrl(method, uri);
request.cookies.add(Cookie('schoolname', idHsReutlingen)); request.cookies.add(Cookie('schoolname', idHsReutlingen));
request.cookies.addAll( request.cookies.addAll(_sessionCookies);
_sessionCookies.entries.map((entry) => Cookie(entry.key, entry.value)),
);
final response = await request.close(); final response = await request.close();
return response; return response;
} }
...@@ -71,21 +67,42 @@ class UntisFetch { ...@@ -71,21 +67,42 @@ class UntisFetch {
Future<JsonWithCookies> _requestJson(RequestOptions options) async { Future<JsonWithCookies> _requestJson(RequestOptions options) async {
final (_, String method, _) = options; final (_, String method, _) = options;
final before = DateTime.now();
final response = await _request(method, _makeUri(options)); final response = await _request(method, _makeUri(options));
final cookies = response.cookies; final cookies = response.cookies;
final SimpleCookieMap cookieMap = {}; final resultCookies = <Cookie>[];
for (final cookie in cookies) { for (final cookie in cookies) {
if (_sessionCookieNames.contains(cookie.name)) { if (_sessionCookieNames.contains(cookie.name)) {
cookieMap[cookie.name] = cookie.value; resultCookies.add(cookie);
} }
} }
final afterDownloadStart = DateTime.now();
final responseTime = afterDownloadStart.difference(before);
final responseString = await utf8.decodeStream(response); final responseString = await utf8.decodeStream(response);
final afterDownloadEnd = DateTime.now();
final downloadTime = afterDownloadEnd.difference(afterDownloadStart);
final bodyJson = jsonDecode(responseString); final bodyJson = jsonDecode(responseString);
return (bodyJson as Json, cookieMap); final afterDecode = DateTime.now();
final decodeTime = afterDecode.difference(afterDownloadEnd);
final timeStats = {
'Ping': responseTime,
'Download': downloadTime,
'JSON': decodeTime,
};
print(timeStats.entries
.map((e) => '${e.key}: ${e.value.inMilliseconds}ms')
.join(', '));
return (bodyJson as Json, resultCookies);
} }
Future<Json> requestOptions(RequestOptions options) async { Future<Json> requestOptions(RequestOptions options) async {
...@@ -95,14 +112,9 @@ class UntisFetch { ...@@ -95,14 +112,9 @@ class UntisFetch {
final methodPadded = method.padRight(8); final methodPadded = method.padRight(8);
print('$methodPadded $uri'); print('$methodPadded $uri');
final before = DateTime.now();
final (result, cookies) = await compute(_requestJson, options); final (result, cookies) = await compute(_requestJson, options);
_sessionCookies.addAll(cookies); _sessionCookies.addAll(cookies);
final after = DateTime.now();
final totalTime = after.difference(before);
print('(Fetching/parsing took ${totalTime.inMilliseconds}ms)');
return result; return result;
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment