Use logging library

This commit is contained in:
2024-12-21 22:04:20 +01:00
parent ff7c01c166
commit ba83b5ebf8
6 changed files with 72 additions and 25 deletions

View File

@@ -1,13 +1,14 @@
import 'dart:convert';
import 'dart:developer';
import 'dart:io';
import 'package:http/http.dart' as http;
import 'package:logging/logging.dart';
import 'package:path_provider/path_provider.dart'; // For cache storage
import 'sheet.dart';
class ApiClient {
final log = Logger("ApiClient");
final String baseUrl;
String? token;
@@ -15,7 +16,7 @@ class ApiClient {
/// Login and store the JWT token
Future<bool> login(String username, String password) async {
log("Logging in...");
log.info("Logging in...");
try {
final url = '$baseUrl/login';
final response = await http.post(
@@ -29,13 +30,13 @@ class ApiClient {
if (response.statusCode == 200) {
token = jsonDecode(response.body);
log('Login successful');
log.info('Login successful');
return true;
} else {
log('Login failed: ${response.statusCode}, ${response.body}');
log.warning('Login failed: ${response.statusCode}, ${response.body}');
}
} catch (e) {
log('Error during login: $e');
log.warning('Error during login', e);
}
return false;
}
@@ -43,7 +44,7 @@ class ApiClient {
/// Logout and clear the token
void logout() {
token = null;
log('Logged out successfully.');
log.info('Logged out successfully.');
}
/// Make a GET request
@@ -60,10 +61,11 @@ class ApiClient {
if (response.statusCode == 200) {
return response;
} else {
log('GET request failed: ${response.statusCode} ${response.body}');
log.warning(
'GET request failed: ${response.statusCode} ${response.body}');
}
} catch (e) {
log('Error during GET request: $e');
log.warning('Error during GET request', e);
}
return null;
}
@@ -87,10 +89,11 @@ class ApiClient {
if (response.statusCode == 200 || response.statusCode == 201) {
return response;
} else {
log('POST request failed: ${response.statusCode} ${response.body}');
log.info(
'POST request failed: ${response.statusCode} ${response.body}');
}
} catch (e) {
log('Error during POST request: $e');
log.info('Error during POST request: $e');
}
return null;
}
@@ -113,10 +116,11 @@ class ApiClient {
if (response.statusCode == 200 || response.statusCode == 201) {
return response;
} else {
log('POST Form Data request failed: ${response.statusCode} ${response.body}');
log.info(
'POST Form Data request failed: ${response.statusCode} ${response.body}');
}
} catch (e) {
log('Error during POST Form Data request: $e');
log.info('Error during POST Form Data request: $e');
}
return null;
}
@@ -131,16 +135,16 @@ class ApiClient {
if (response.statusCode == 200) {
final data = jsonDecode(response.body);
log("Data: $data");
return (data as List<dynamic>)
.map((sheet) => Sheet.fromJson(sheet as Map<String, dynamic>))
.toList();
} else {
log('Failed to fetch sheets with status: ${response.statusCode}');
log('Response: ${response.body}');
log.warning(
'Failed to fetch sheets with status: ${response.statusCode}');
log.info('Response: ${response.body}');
}
} catch (e) {
log('Error during fetching sheets: $e');
log.warning('Error during fetching sheets', e);
}
return List.empty();
@@ -160,7 +164,7 @@ class ApiClient {
final cachedFile = File(cachedPdfPath);
if (await cachedFile.exists()) {
log("PDF found in cache: $cachedPdfPath");
log.info("PDF found in cache: $cachedPdfPath");
return cachedFile;
}
@@ -172,13 +176,14 @@ class ApiClient {
// Save the fetched file to the cache
//
await cachedFile.writeAsBytes(response.bodyBytes);
log("PDF downloaded and cached at: $cachedPdfPath");
log.info("PDF downloaded and cached at: $cachedPdfPath");
return cachedFile;
} else {
log("Failed to fetch PDF from API. Status: ${response?.statusCode}");
log.warning(
"Failed to fetch PDF from API. Status: ${response?.statusCode}");
}
} catch (e) {
log("Error fetching PDF: $e");
log.warning("Error fetching PDF", e);
}
return null;