2019-08-18 23:04:39 +02:00
|
|
|
import 'package:http/http.dart';
|
|
|
|
import 'package:logger/logger.dart';
|
|
|
|
import 'dart:convert';
|
|
|
|
|
|
|
|
final log = Logger();
|
|
|
|
const defaultRoutes = {
|
|
|
|
"login": "/api/login",
|
|
|
|
"logout": "/api/logout",
|
|
|
|
"checkin": "/api/checkin",
|
|
|
|
"checkout": "/api/checkout",
|
|
|
|
"movements": "/api/movements",
|
|
|
|
"status": "/api/status",
|
|
|
|
"ping": "/api/ping",
|
|
|
|
};
|
|
|
|
|
|
|
|
class Connection {
|
|
|
|
// Connection constructor.
|
2019-08-30 01:29:15 +02:00
|
|
|
Connection(this.username, this.password, this.baseAddress,
|
|
|
|
[this.routes = defaultRoutes]);
|
2019-08-18 23:04:39 +02:00
|
|
|
// baseAddress contains the base address to the BotZ server
|
|
|
|
final Uri baseAddress;
|
|
|
|
// Username is the username to autenticate against the foreign
|
|
|
|
// service.
|
|
|
|
final String username;
|
|
|
|
// Password is the password to autenticate against the foreign
|
|
|
|
// service.
|
|
|
|
final String password;
|
|
|
|
final Map<String, String> routes;
|
|
|
|
// _stateCookie is private. It contains the HTTP state cookie
|
|
|
|
// assigned by the server.
|
|
|
|
String _cookies = "";
|
|
|
|
bool _loggedIn = false;
|
|
|
|
bool _checkedIn = false;
|
|
|
|
|
|
|
|
void updateState(String cookies, bool loggedIn) {
|
|
|
|
this._cookies = loggedIn ? cookies : "";
|
|
|
|
this._loggedIn = loggedIn;
|
|
|
|
}
|
|
|
|
|
|
|
|
void updateMovement(bool checkedIn) => this._checkedIn = checkedIn;
|
|
|
|
|
|
|
|
bool isLoggedIn() => this._loggedIn;
|
|
|
|
|
2019-08-30 01:29:15 +02:00
|
|
|
// FIXME: THIS IS WRONG! We might be yet checked in.
|
2019-08-18 23:04:39 +02:00
|
|
|
bool isCheckedIn() => this._checkedIn;
|
|
|
|
|
|
|
|
// Perform the login and obtain the cookie.
|
2019-08-30 01:29:15 +02:00
|
|
|
Future<void> login() async {
|
2019-08-18 23:04:39 +02:00
|
|
|
var targetUri = this.baseAddress.toString() + this.routes['login'];
|
|
|
|
var payload = jsonEncode({
|
2019-08-30 01:29:15 +02:00
|
|
|
"username": this.username,
|
|
|
|
"password": this.password,
|
|
|
|
});
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
var resp = await post(
|
2019-08-30 01:29:15 +02:00
|
|
|
targetUri,
|
|
|
|
headers: {"Content-Type": "application/json"},
|
|
|
|
body: payload,
|
|
|
|
);
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
log.d("[login] Post sent to: $targetUri");
|
|
|
|
handleResponse(resp);
|
|
|
|
var body = jsonDecode(resp.body);
|
|
|
|
log.d("[login] Response: $body");
|
2019-08-30 01:29:15 +02:00
|
|
|
var rawCookie = resp.headers['set-cookie'];
|
|
|
|
if (rawCookie == null) {
|
|
|
|
rawCookie = resp.headers['cookie'];
|
|
|
|
}
|
|
|
|
this.updateState(rawCookie, body['logged_in']);
|
2019-08-18 23:04:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the logout.
|
2019-08-30 01:29:15 +02:00
|
|
|
Future<void> logout() async {
|
2019-08-18 23:04:39 +02:00
|
|
|
var targetUri = this.baseAddress.toString() + this.routes['logout'];
|
|
|
|
|
|
|
|
var resp = await post(
|
2019-08-30 01:29:15 +02:00
|
|
|
targetUri,
|
|
|
|
headers: {'cookie': this._cookies},
|
|
|
|
);
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
log.d("[logout] Post sent to: $targetUri");
|
|
|
|
handleResponse(resp);
|
|
|
|
var body = jsonDecode(resp.body);
|
|
|
|
log.d("[logout] Response: $body");
|
|
|
|
this.updateState(this._cookies, body['logged_in']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the checkin if possible.
|
2019-08-30 01:29:15 +02:00
|
|
|
Future<void> checkin() async {
|
2019-08-18 23:04:39 +02:00
|
|
|
if (!this._loggedIn) {
|
|
|
|
throw UnacceptableInvocationException("Not logged in");
|
|
|
|
}
|
|
|
|
if (this._checkedIn) {
|
|
|
|
throw UnacceptableInvocationException("Yet checked in");
|
|
|
|
}
|
|
|
|
var targetUri = this.baseAddress.toString() + this.routes['checkin'];
|
|
|
|
|
|
|
|
var resp = await post(
|
2019-08-30 01:29:15 +02:00
|
|
|
targetUri,
|
|
|
|
headers: {'cookies': this._cookies},
|
|
|
|
);
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
handleResponse(resp);
|
|
|
|
var body = jsonDecode(resp.body);
|
|
|
|
log.d("[checkin] Response: $body");
|
|
|
|
this.updateMovement(body['checked_in']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform the checkout if possible.
|
2019-08-30 01:29:15 +02:00
|
|
|
Future<void> checkout() async {
|
2019-08-18 23:04:39 +02:00
|
|
|
if (!this._loggedIn) {
|
|
|
|
throw UnacceptableInvocationException("Not logged in");
|
|
|
|
}
|
|
|
|
if (!this._checkedIn) {
|
|
|
|
throw UnacceptableInvocationException("Not yet checked in");
|
|
|
|
}
|
|
|
|
var targetUri = this.baseAddress.toString() + this.routes['checkout'];
|
|
|
|
|
|
|
|
var resp = await post(
|
2019-08-30 01:29:15 +02:00
|
|
|
targetUri,
|
|
|
|
headers: {'cookies': this._cookies},
|
|
|
|
);
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
handleResponse(resp);
|
|
|
|
var body = jsonDecode(resp.body);
|
|
|
|
log.d("[checkout] Response: $body");
|
|
|
|
this.updateMovement(body['checked_in']);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query the server for the list of movements.
|
|
|
|
Future<Map<String, String>> getMovements() async {
|
|
|
|
if (!this._loggedIn) {
|
|
|
|
throw UnacceptableInvocationException("Not logged in");
|
|
|
|
}
|
|
|
|
if (!this._checkedIn) {
|
|
|
|
throw UnacceptableInvocationException("Not yet checked in");
|
|
|
|
}
|
|
|
|
var targetUri = this.baseAddress.toString() + this.routes['movements'];
|
|
|
|
|
|
|
|
var resp = await get(
|
2019-08-30 01:29:15 +02:00
|
|
|
targetUri,
|
|
|
|
headers: {'cookies': this._cookies},
|
|
|
|
);
|
2019-08-18 23:04:39 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
handleResponse(resp);
|
|
|
|
} on NotFoundException {
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
var body = jsonDecode(resp.body);
|
|
|
|
Map<String, String> movements;
|
|
|
|
for (var values in body['movements']) {
|
|
|
|
movements[values['time']] = values['type'];
|
|
|
|
}
|
|
|
|
return movements;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
|
|
|
} else if (c == 401) {
|
|
|
|
throw UnauthorizedException(resp.request.url);
|
|
|
|
} else if (c == 404) {
|
|
|
|
throw NotFoundException(resp.request.url);
|
2019-08-30 01:29:15 +02:00
|
|
|
} else if (c >= 400 || c < 500) {
|
2019-08-18 23:04:39 +02:00
|
|
|
throw ClientException("Unexpected return code: $c", resp.request.url);
|
|
|
|
} else if (c >= 500) {
|
|
|
|
throw ClientException("Server error: $c", resp.request.url);
|
|
|
|
}
|
|
|
|
return resp;
|
|
|
|
}
|