Files
sheetless/lib/api.dart
2026-01-24 19:22:03 +01:00

164 lines
4.3 KiB
Dart

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
import 'sheet.dart';
class ApiClient {
final log = Logger("ApiClient");
final String baseUrl;
String? token;
ApiClient({required this.baseUrl, this.token});
Future<void> login(String username, String password) async {
log.info("Logging in...");
final url = '$baseUrl/auth/login';
final response = await http.post(
Uri.parse(url),
headers: {'Content-Type': 'application/json'},
body: jsonEncode({'username': username, 'password': password}),
);
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
token = responseData['token'];
log.info('Login successful');
} else {
throw Exception(
"Failed logging in: Response code ${response.statusCode}\nResponse: ${response.body}",
);
}
}
void logout() {
token = null;
log.info('Logged out successfully.');
}
Future<http.Response> get(
String endpoint, {
bool isBinary = false,
bool throwExceptionIfStatusCodeNot200 = false,
}) async {
final url = '$baseUrl$endpoint';
final headers = {
'Authorization': 'Bearer $token',
if (!isBinary) 'Content-Type': 'application/json',
};
final response = await http.get(Uri.parse(url), headers: headers);
if (!throwExceptionIfStatusCodeNot200 || response.statusCode == 200) {
return response;
} else {
log.warning(
"Failed get request to '$url'! StatusCode: ${response.statusCode}\nResponseBody: ${response.body}",
);
throw Exception(
'GET request failed: ${response.statusCode} ${response.body}',
);
}
}
Future<http.Response?> post(
String endpoint,
Map<String, dynamic> body,
) async {
try {
final url = '$baseUrl$endpoint';
final headers = {
'Authorization': 'Bearer $token',
'Content-Type': 'application/json',
};
final response = await http.post(
Uri.parse(url),
headers: headers,
body: jsonEncode(body),
);
if (response.statusCode == 200 || response.statusCode == 201) {
return response;
} else {
log.info(
'POST request failed: ${response.statusCode} ${response.body}',
);
}
} catch (e) {
log.info('Error during POST request: $e');
}
return null;
}
Future<http.Response?> postFormData(String endpoint, String body) async {
try {
final url = '$baseUrl$endpoint';
final headers = {
'Authorization': 'Bearer $token',
'Content-Type': 'application/x-www-form-urlencoded',
};
final response = await http.post(
Uri.parse(url),
headers: headers,
body: body,
);
if (response.statusCode == 200 || response.statusCode == 201) {
return response;
} else {
log.info(
'POST Form Data request failed: ${response.statusCode} ${response.body}',
);
}
} catch (e) {
log.info('Error during POST Form Data request: $e');
}
return null;
}
Future<List<Sheet>> fetchSheets() async {
final response = await get(
"/api/sheets/list",
throwExceptionIfStatusCodeNot200: true,
);
final data = jsonDecode(response.body);
return (data['sheets'] as List<dynamic>)
.map((sheet) => Sheet.fromJson(sheet as Map<String, dynamic>))
.toList();
}
Future<Uint8List> fetchPdfFileData(String sheetUuid) async {
final response = await get(
'/api/sheets/get/$sheetUuid',
isBinary: true,
throwExceptionIfStatusCodeNot200: true,
);
log.info("PDF downloaded");
return response.bodyBytes;
}
Future<File> getPdfFileCached(String sheetUuid) async {
final cacheDir = await getTemporaryDirectory();
final cachedPdfPath = '${cacheDir.path}/$sheetUuid.pdf';
final cachedFile = File(cachedPdfPath);
if (await cachedFile.exists()) {
log.info("PDF found in cache: $cachedPdfPath");
return cachedFile;
}
final pdfFileData = await fetchPdfFileData(sheetUuid);
await cachedFile.writeAsBytes(pdfFileData);
log.info("PDF cached at: $cachedPdfPath");
return cachedFile;
}
}