Fix connection.

This commit is contained in:
sfigato 2019-08-30 01:29:15 +02:00
parent b677865c21
commit 1e976224cb
Signed by: blallo
GPG Key ID: 0CBE577C9B72DC3F

View File

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