This repository has been archived on 2025-08-25. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
Seshat/lib/data/services/api_client.dart
2025-08-20 12:43:35 +02:00

619 lines
18 KiB
Dart

import 'dart:convert';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
import 'package:http/http.dart';
import 'package:logger/logger.dart';
import 'package:seshat/config/constants.dart';
import 'package:seshat/domain/models/accounting.dart';
import 'package:seshat/domain/models/bal.dart';
import 'package:seshat/domain/models/bal_stats.dart';
import 'package:seshat/domain/models/book.dart';
import 'package:seshat/domain/models/book_instance.dart';
import 'package:seshat/domain/models/owner.dart';
import 'package:seshat/domain/models/search_result.dart';
import 'package:seshat/utils/command.dart';
import 'package:seshat/utils/result.dart';
extension StringExtension on String {
String capitalize() {
return "${this[0].toUpperCase()}${this.substring(1).toLowerCase()}";
}
}
class ApiClient {
ApiClient({String? host, int? port});
late final Command0 load;
String? token;
bool isReady = false;
FlutterSecureStorage? _secureStorage;
Logger log = Logger(
printer: PrettyPrinter(
colors: true,
lineLength: 100,
methodCount: 0,
dateTimeFormat: DateTimeFormat.dateAndTime,
),
);
Future<void> _initStore() async {
_secureStorage ??= const FlutterSecureStorage(aOptions: AndroidOptions());
}
Future<Map<String, String>> _getHeaders([
Map<String, String>? additionalHeaders,
]) async {
await _initStore();
final token = await _secureStorage!.read(key: "token");
final headers = {"Authorization": "Bearer $token", ...?additionalHeaders};
return headers;
}
/*
* ========================
* =====< Accounting >=====
* ========================
*/
Future<Result<Accounting>> getAccounting(int balId) async {
final url = "https://$apiBasePath/bal/$balId/accounting";
log.i("Fetching: getAccounting ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(Accounting.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No BAL with this is exists in database";
default:
throw "Unknown error of code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<void>> returnToId(int balId, int ownerId, String type) async {
final url =
"https://$apiBasePath/bal/${balId.toString()}/accounting/return/${ownerId.toString()}";
log.i("Fetching: returnToId ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = jsonEncode({"return_type": type.capitalize()});
final response = await client.post(
Uri.parse(url),
headers: headers,
body: body,
);
switch (response.statusCode) {
case 200:
return Result.ok(response);
case 403:
throw "You don't own the specified BAL or owner";
case 404:
throw "No BAL or owner with this id exists in database";
case 409:
throw "Books and money have already been returned, or there was nothing to return";
default:
throw "Unknown error of code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
/*
* =================
* =====[ BAL ]=====
* =================
*/
Future<Result<BalStats>> getBalStats(int id) async {
final url = "https://$apiBasePath/bal/${id.toString()}/stats";
log.i("Fetching: getBalStats ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(BalStats.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No BAL with this id exists in the database";
case 409:
throw "The specified BAL is not ended yet";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> stopBal(int id) async {
final url = "https://$apiBasePath/bal/${id.toString()}/stop";
log.i("Fetching: stopBal ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.post(Uri.parse(url), headers: headers);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No BAL with specified ID found";
case 409:
throw "Selected BAL was not on ongoing state";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> startBal(int id) async {
final url = "https://$apiBasePath/bal/${id.toString()}/start";
log.i("Fetching: startBal ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.post(Uri.parse(url), headers: headers);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No BAL with specified ID found";
case 409:
throw "Cannot have multiple BAl ongoing at the same time!";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> editBal(
int id,
String name,
DateTime start,
DateTime end,
) async {
final url = "https://$apiBasePath/bal/${id.toString()}";
log.i("Fetching: editBal ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = {
"name": name,
"start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
};
final response = await client.patch(
Uri.parse(url),
headers: headers,
body: jsonEncode(body),
);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No bal with specified id";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> getBalById(int id) async {
final url = "https://$apiBasePath/bal/${id.toString()}";
log.i("Fetching: getBalById ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
switch (response.statusCode) {
case 200:
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
case 403:
throw "You don't own the specified BAL";
case 404:
throw "No BAL with this id found in database";
default:
throw "Unknown error";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal>> addBal(String name, DateTime start, DateTime end) async {
final url = "https://$apiBasePath/bal";
log.i("Fetching: addBal ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = {
"name": name,
"start_timestamp": (start.millisecondsSinceEpoch / 1000).round(),
"end_timestamp": (end.millisecondsSinceEpoch / 1000).round(),
};
final response = await client.post(
Uri.parse(url),
headers: headers,
body: jsonEncode(body),
);
switch (response.statusCode) {
case 201:
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
case 400:
throw "Time cannot go backwards";
default:
throw "Unknown error with code ${response.statusCode.toString()}";
}
} catch (e) {
log.e(e.toString());
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<List<Bal>>> getBals() async {
final url = "https://$apiBasePath/bals";
log.i("Fetching: getBals ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(json.map((element) => Bal.fromJSON(element)).toList());
} else {
throw Exception("Something wrong happened");
}
} catch (e) {
debugPrint("ERROR: ${e.toString()}");
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<Bal?>> getCurrentBal() async {
final url = "https://$apiBasePath/bal/current";
log.i("Fetching: getCurrentBal ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Bal.fromJSON(json));
} else if (response.statusCode == 404) {
return Result.ok(null);
} else {
throw Exception("Something went wrong");
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
/*
* ===================
* =====[ BOOKS ]=====
* ===================
*/
Future<Result<Book>> getBookById(int id) async {
final url = "https://$apiBasePath/book/id/${id.toString()}";
log.i("Fetching: getBookById ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json));
} else {
throw Exception("The book was not found");
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<Book>> getBookByEAN(String ean) async {
final url = "https://$apiBasePath/book/ean/$ean";
log.i("Fetching: getBookByEan ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Book.fromJSON(json));
} else {
throw Exception("The book was not found");
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
/*
* =============================
* =====[ BOOKS INSTANCES ]=====
* =============================
*/
Future<Result<List<SearchResult>>> getBookInstanceBySearch(
int balId,
String title,
String author,
) async {
final url = "https://$apiBasePath/bal/${balId.toString()}/search";
log.i("Fetching: getBookInstancesBySearch ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = jsonEncode({"title": title, "author": author});
final response = await client.post(
Uri.parse(url),
headers: headers,
body: body,
);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(json.map((el) => SearchResult.fromJSON(el)).toList());
} else {
throw "Unknown Error";
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<List<BookInstance>>> getBookInstanceByEAN(
int balId,
int ean,
) async {
final url =
"https://$apiBasePath/bal/${balId.toString()}/ean/${ean.toString()}/book_instances";
log.i("Fetching: getBookInstancesByEan ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(json.map((el) => BookInstance.fromJSON(el)).toList());
} else {
throw "Unknown Error";
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<void>> sellBooks(Map<String, double?> books) async {
final url = "https://$apiBasePath/book_instance/sell/bulk";
log.i("Fetching: sellBooks ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = jsonEncode(books);
final response = await client.post(
Uri.parse(url),
headers: headers,
body: body,
);
if (response.statusCode == 200) {
return Result.ok(response.statusCode);
} else {
throw "Unknown error";
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
Future<Result<BookInstance>> sendBook(
Book book,
Owner owner,
Bal bal,
double price,
) async {
final url = "https://$apiBasePath/book_instance";
log.i("Fetching: sendBook ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = jsonEncode({
"bal_id": bal.id,
"book_id": book.id,
"owner_id": owner.id,
"price": price,
});
final response = await client.post(
Uri.parse(url),
headers: headers,
body: body,
);
if (response.statusCode == 201) {
final json = jsonDecode(response.body);
return Result.ok(BookInstance.fromJSON(json));
} else if (response.statusCode == 403) {
throw Exception("You don't own that book instance");
} else {
throw Exception("Something wrong happened");
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
/*
* ====================
* =====[ OWNERS ]=====
* ====================
*/
Future<Result<Owner>> getOwnerById(int id) async {
final url = "https://$apiBasePath/owner/${id.toString()}";
log.i("Fetching: getOwnerById ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json));
} else {
throw Exception("The owner was not found");
}
} catch (e) {
return Result.error(Exception("API $e"));
} finally {
client.close();
}
}
Future<Result<Owner>> getSectionOwner() async {
final url = "https://$apiBasePath/owner/self";
log.i("Fetching: getSectionOwner ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json));
} else {
throw "Unknown error";
}
} catch (e) {
return Result.error(Exception(e));
} finally {
client.close();
}
}
/// Call on `/owners` to get a list of all [Owner]s
Future<Result<List<Owner>>> getOwners() async {
final url = "https://$apiBasePath/owners";
log.i("Fetching: getOwners ($url)");
final client = Client();
try {
final headers = await _getHeaders();
final response = await client.get(Uri.parse(url), headers: headers);
if (response.statusCode == 200) {
final json = jsonDecode(response.body) as List<dynamic>;
return Result.ok(
json.map((element) => Owner.fromJSON(element)).toList(),
);
} else {
throw Exception("Invalid request");
}
} on Exception catch (error) {
return Result.error(error);
} finally {
client.close();
}
}
/// Adds an owner to the database
Future<Result<Owner>> addOwner(
String firstName,
String lastName,
String contact,
) async {
final url = "https://$apiBasePath/owner";
log.i("Fetching: addOwner ($url)");
final client = Client();
try {
final headers = await _getHeaders({"Content-Type": "application/json"});
final body = {
"first_name": firstName,
"last_name": lastName,
"contact": contact,
};
final response = await client.post(
Uri.parse(url),
headers: headers,
body: jsonEncode(body),
);
if (response.statusCode == 201) {
final json = jsonDecode(response.body);
return Result.ok(Owner.fromJSON(json));
} else {
throw Exception("Invalid request");
}
} on Exception catch (error) {
return Result.error(error);
} finally {
client.close();
}
}
}