make web version work, add dockerfile
This commit is contained in:
56
lib/api.dart
56
lib/api.dart
@@ -1,6 +1,7 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:http/http.dart' as http;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path_provider/path_provider.dart'; // For cache storage
|
||||
@@ -21,10 +22,7 @@ class ApiClient {
|
||||
final response = await http.post(
|
||||
Uri.parse(url),
|
||||
headers: {'Content-Type': 'application/json'},
|
||||
body: jsonEncode({
|
||||
'email': username,
|
||||
'password': password,
|
||||
}),
|
||||
body: jsonEncode({'email': username, 'password': password}),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
@@ -59,7 +57,8 @@ class ApiClient {
|
||||
return response;
|
||||
} else {
|
||||
log.warning(
|
||||
'GET request failed: ${response.statusCode} ${response.body}');
|
||||
'GET request failed: ${response.statusCode} ${response.body}',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
log.warning('Error during GET request', e);
|
||||
@@ -68,7 +67,9 @@ class ApiClient {
|
||||
}
|
||||
|
||||
Future<http.Response?> post(
|
||||
String endpoint, Map<String, dynamic> body) async {
|
||||
String endpoint,
|
||||
Map<String, dynamic> body,
|
||||
) async {
|
||||
try {
|
||||
final url = '$baseUrl$endpoint';
|
||||
final headers = {
|
||||
@@ -86,7 +87,8 @@ class ApiClient {
|
||||
return response;
|
||||
} else {
|
||||
log.info(
|
||||
'POST request failed: ${response.statusCode} ${response.body}');
|
||||
'POST request failed: ${response.statusCode} ${response.body}',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
log.info('Error during POST request: $e');
|
||||
@@ -112,7 +114,8 @@ class ApiClient {
|
||||
return response;
|
||||
} else {
|
||||
log.info(
|
||||
'POST Form Data request failed: ${response.statusCode} ${response.body}');
|
||||
'POST Form Data request failed: ${response.statusCode} ${response.body}',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
log.info('Error during POST Form Data request: $e');
|
||||
@@ -135,7 +138,8 @@ class ApiClient {
|
||||
.toList();
|
||||
} else {
|
||||
log.warning(
|
||||
'Failed to fetch sheets with status: ${response.statusCode}');
|
||||
'Failed to fetch sheets with status: ${response.statusCode}',
|
||||
);
|
||||
log.info('Response: ${response.body}');
|
||||
}
|
||||
} catch (e) {
|
||||
@@ -145,6 +149,25 @@ class ApiClient {
|
||||
return List.empty();
|
||||
}
|
||||
|
||||
Future<Uint8List?> fetchPdfFileData(String sheetUuid) async {
|
||||
try {
|
||||
final response = await get('/sheet/pdf/$sheetUuid', isBinary: true);
|
||||
|
||||
if (response != null && response.statusCode == 200) {
|
||||
log.info("PDF downloaded");
|
||||
return response.bodyBytes;
|
||||
} else {
|
||||
log.warning(
|
||||
"Failed to fetch PDF from API. Status: ${response?.statusCode}",
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
log.warning("Error fetching PDF", e);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
Future<File?> getPdfFileCached(String sheetUuid) async {
|
||||
try {
|
||||
final cacheDir = await getTemporaryDirectory();
|
||||
@@ -156,16 +179,11 @@ class ApiClient {
|
||||
return cachedFile;
|
||||
}
|
||||
|
||||
final response = await get('/sheet/pdf/$sheetUuid', isBinary: true);
|
||||
|
||||
if (response != null && response.statusCode == 200) {
|
||||
await cachedFile.writeAsBytes(response.bodyBytes);
|
||||
log.info("PDF downloaded and cached at: $cachedPdfPath");
|
||||
return cachedFile;
|
||||
} else {
|
||||
log.warning(
|
||||
"Failed to fetch PDF from API. Status: ${response?.statusCode}");
|
||||
}
|
||||
final pdfFileData = await fetchPdfFileData(sheetUuid);
|
||||
await cachedFile.writeAsBytes(
|
||||
pdfFileData!,
|
||||
); // TODO: proper error handling
|
||||
log.info("PDF cached at: $cachedPdfPath");
|
||||
} catch (e) {
|
||||
log.warning("Error fetching PDF", e);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user