Clean up code
This commit is contained in:
18
lib/api.dart
18
lib/api.dart
@@ -14,7 +14,6 @@ class ApiClient {
|
||||
|
||||
ApiClient({required this.baseUrl, this.token});
|
||||
|
||||
/// Login and store the JWT token
|
||||
Future<bool> login(String username, String password) async {
|
||||
log.info("Logging in...");
|
||||
try {
|
||||
@@ -41,13 +40,11 @@ class ApiClient {
|
||||
return false;
|
||||
}
|
||||
|
||||
/// Logout and clear the token
|
||||
void logout() {
|
||||
token = null;
|
||||
log.info('Logged out successfully.');
|
||||
}
|
||||
|
||||
/// Make a GET request
|
||||
Future<http.Response?> get(String endpoint, {bool isBinary = false}) async {
|
||||
try {
|
||||
final url = '$baseUrl$endpoint';
|
||||
@@ -70,7 +67,6 @@ class ApiClient {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Make a POST request
|
||||
Future<http.Response?> post(
|
||||
String endpoint, Map<String, dynamic> body) async {
|
||||
try {
|
||||
@@ -98,7 +94,6 @@ class ApiClient {
|
||||
return null;
|
||||
}
|
||||
|
||||
/// Make a POST request with form data
|
||||
Future<http.Response?> postFormData(String endpoint, String body) async {
|
||||
try {
|
||||
final url = '$baseUrl$endpoint';
|
||||
@@ -152,29 +147,18 @@ class ApiClient {
|
||||
|
||||
Future<File?> getPdfFileCached(String sheetUuid) async {
|
||||
try {
|
||||
// Get the cache directory
|
||||
|
||||
// final cacheDir = kIsWeb
|
||||
// ? await MemoryFileSystem().systemTempDirectory.createTemp('cache')
|
||||
// : await getTemporaryDirectory();
|
||||
final cacheDir = await getTemporaryDirectory();
|
||||
final cachedPdfPath = '${cacheDir.path}/$sheetUuid.pdf';
|
||||
|
||||
// Check if the file already exists in the cache
|
||||
final cachedFile = File(cachedPdfPath);
|
||||
|
||||
if (await cachedFile.exists()) {
|
||||
log.info("PDF found in cache: $cachedPdfPath");
|
||||
return cachedFile;
|
||||
}
|
||||
|
||||
// Make the authenticated API call
|
||||
|
||||
final response = await this.get('/sheet/pdf/$sheetUuid', isBinary: true);
|
||||
final response = await get('/sheet/pdf/$sheetUuid', isBinary: true);
|
||||
|
||||
if (response != null && response.statusCode == 200) {
|
||||
// Save the fetched file to the cache
|
||||
//
|
||||
await cachedFile.writeAsBytes(response.bodyBytes);
|
||||
log.info("PDF downloaded and cached at: $cachedPdfPath");
|
||||
return cachedFile;
|
||||
|
||||
Reference in New Issue
Block a user