diff --git a/lib/utils/connection.dart b/lib/utils/connection.dart index af7cc0f..338da3c 100644 --- a/lib/utils/connection.dart +++ b/lib/utils/connection.dart @@ -2,7 +2,6 @@ import 'package:http/http.dart'; import 'package:logger/logger.dart'; import 'dart:convert'; - final log = Logger(); const defaultRoutes = { "login": "/api/login", @@ -14,10 +13,10 @@ const defaultRoutes = { "ping": "/api/ping", }; - class Connection { // Connection constructor. - Connection(this.username, this.password, this.baseAddress, [this.routes = defaultRoutes]); + Connection(this.username, this.password, this.baseAddress, + [this.routes = defaultRoutes]); // baseAddress contains the base address to the BotZ server final Uri baseAddress; // Username is the username to autenticate against the foreign @@ -42,37 +41,42 @@ class Connection { bool isLoggedIn() => this._loggedIn; + // FIXME: THIS IS WRONG! We might be yet checked in. bool isCheckedIn() => this._checkedIn; // Perform the login and obtain the cookie. - void login() async { + Future login() async { var targetUri = this.baseAddress.toString() + this.routes['login']; var payload = jsonEncode({ - "username": this.username, - "password": this.password, - }); + "username": this.username, + "password": this.password, + }); var resp = await post( - targetUri, - headers: {"Content-Type": "application/json"}, - body: payload, - ); + targetUri, + headers: {"Content-Type": "application/json"}, + body: payload, + ); log.d("[login] Post sent to: $targetUri"); handleResponse(resp); var body = jsonDecode(resp.body); log.d("[login] Response: $body"); - this.updateState(resp.headers['cookies'], body['logged_in']); + var rawCookie = resp.headers['set-cookie']; + if (rawCookie == null) { + rawCookie = resp.headers['cookie']; + } + this.updateState(rawCookie, body['logged_in']); } // Perform the logout. - void logout() async { + Future logout() async { var targetUri = this.baseAddress.toString() + this.routes['logout']; var resp = await post( - targetUri, - headers: {'cookies': this._cookies}, - ); + targetUri, + headers: {'cookie': this._cookies}, + ); log.d("[logout] Post sent to: $targetUri"); handleResponse(resp); @@ -82,7 +86,7 @@ class Connection { } // Perform the checkin if possible. - void checkin() async { + Future checkin() async { if (!this._loggedIn) { throw UnacceptableInvocationException("Not logged in"); } @@ -92,9 +96,9 @@ class Connection { var targetUri = this.baseAddress.toString() + this.routes['checkin']; var resp = await post( - targetUri, - headers: {'cookies': this._cookies}, - ); + targetUri, + headers: {'cookies': this._cookies}, + ); handleResponse(resp); var body = jsonDecode(resp.body); @@ -103,7 +107,7 @@ class Connection { } // Perform the checkout if possible. - void checkout() async { + Future checkout() async { if (!this._loggedIn) { throw UnacceptableInvocationException("Not logged in"); } @@ -113,9 +117,9 @@ class Connection { var targetUri = this.baseAddress.toString() + this.routes['checkout']; var resp = await post( - targetUri, - headers: {'cookies': this._cookies}, - ); + targetUri, + headers: {'cookies': this._cookies}, + ); handleResponse(resp); var body = jsonDecode(resp.body); @@ -134,9 +138,9 @@ class Connection { var targetUri = this.baseAddress.toString() + this.routes['movements']; var resp = await get( - targetUri, - headers: {'cookies': this._cookies}, - ); + targetUri, + headers: {'cookies': this._cookies}, + ); try { handleResponse(resp); @@ -152,27 +156,23 @@ class Connection { } } - class UnauthorizedException implements ClientException { UnauthorizedException(this.uri); final String message = "Unauthorized"; final Uri uri; } - class NotFoundException implements ClientException { NotFoundException(this.uri); final String message = "Resource not found"; final Uri uri; } - class UnacceptableInvocationException implements Exception { UnacceptableInvocationException(this.message); final String message; } - Response handleResponse(Response resp) { final c = resp.statusCode; if (c >= 200 && c < 400) { @@ -180,7 +180,7 @@ Response handleResponse(Response resp) { throw UnauthorizedException(resp.request.url); } else if (c == 404) { throw NotFoundException(resp.request.url); - }else if (c >=400 || c < 500) { + } else if (c >= 400 || c < 500) { throw ClientException("Unexpected return code: $c", resp.request.url); } else if (c >= 500) { throw ClientException("Server error: $c", resp.request.url);