Files
sure/mobile/lib/services/transactions_service.dart
Lazy Bone 38938fe971 Add API key authentication support to mobile app (#850)
* feat: Add API key login option to mobile app

Add a "Via API Key Login" button on the login screen that opens a
dialog for entering an API key. The API key is validated by making a
test request to /api/v1/accounts with the X-Api-Key header, and on
success is persisted in secure storage. All HTTP services now use a
centralized ApiConfig.getAuthHeaders() helper that returns the correct
auth header (X-Api-Key or Bearer) based on the current auth mode.

https://claude.ai/code/session_01DnyCzdMjVpSsbBZK3XbzUH

* fix: Improve API key dialog context handling and controller disposal

- Use outer context for SnackBar so it displays on the main screen
  instead of behind the dialog
- Explicitly dispose TextEditingController to prevent memory leaks
- Close dialog on failure before showing error SnackBar for better UX
- Avoid StatefulBuilder context parameter shadowing

https://claude.ai/code/session_01DnyCzdMjVpSsbBZK3XbzUH

* fix: Use user-friendly error message in API key login catch block

Log the technical exception details via LogService.instance.error and
show a generic "Unable to connect" message to the user instead of
exposing the raw exception string.

https://claude.ai/code/session_01DnyCzdMjVpSsbBZK3XbzUH

---------

Co-authored-by: Claude <noreply@anthropic.com>
2026-01-31 13:25:52 +01:00

233 lines
6.2 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
import '../models/transaction.dart';
import 'api_config.dart';
class TransactionsService {
Future<Map<String, dynamic>> createTransaction({
required String accessToken,
required String accountId,
required String name,
required String date,
required String amount,
required String currency,
required String nature,
String? notes,
}) async {
final url = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions');
final body = {
'transaction': {
'account_id': accountId,
'name': name,
'date': date,
'amount': amount,
'currency': currency,
'nature': nature,
if (notes != null) 'notes': notes,
}
};
try {
final response = await http.post(
url,
headers: {
...ApiConfig.getAuthHeaders(accessToken),
'Content-Type': 'application/json',
},
body: jsonEncode(body),
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200 || response.statusCode == 201) {
final responseData = jsonDecode(response.body);
return {
'success': true,
'transaction': Transaction.fromJson(responseData),
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
try {
final responseData = jsonDecode(response.body);
return {
'success': false,
'error': responseData['error'] ?? 'Failed to create transaction',
};
} catch (e) {
return {
'success': false,
'error': 'Failed to create transaction: ${response.body}',
};
}
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> getTransactions({
required String accessToken,
String? accountId,
int? page,
int? perPage,
}) async {
final Map<String, String> queryParams = {};
if (accountId != null) {
queryParams['account_id'] = accountId;
}
if (page != null) {
queryParams['page'] = page.toString();
}
if (perPage != null) {
queryParams['per_page'] = perPage.toString();
}
final baseUri = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions');
final url = queryParams.isNotEmpty
? baseUri.replace(queryParameters: queryParams)
: baseUri;
try {
final response = await http.get(
url,
headers: {
...ApiConfig.getAuthHeaders(accessToken),
'Content-Type': 'application/json',
},
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200) {
final responseData = jsonDecode(response.body);
// Handle both array and object responses
List<dynamic> transactionsJson;
Map<String, dynamic>? pagination;
if (responseData is List) {
transactionsJson = responseData;
} else if (responseData is Map && responseData.containsKey('transactions')) {
transactionsJson = responseData['transactions'];
// Extract pagination metadata if present
if (responseData.containsKey('pagination')) {
pagination = responseData['pagination'];
}
} else {
transactionsJson = [];
}
final transactions = transactionsJson
.map((json) => Transaction.fromJson(json))
.toList();
return {
'success': true,
'transactions': transactions,
if (pagination != null) 'pagination': pagination,
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
return {
'success': false,
'error': 'Failed to fetch transactions',
};
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> deleteTransaction({
required String accessToken,
required String transactionId,
}) async {
final url = Uri.parse('${ApiConfig.baseUrl}/api/v1/transactions/$transactionId');
try {
final response = await http.delete(
url,
headers: {
...ApiConfig.getAuthHeaders(accessToken),
'Content-Type': 'application/json',
},
).timeout(const Duration(seconds: 30));
if (response.statusCode == 200 || response.statusCode == 204) {
return {
'success': true,
};
} else if (response.statusCode == 401) {
return {
'success': false,
'error': 'unauthorized',
};
} else {
try {
final responseData = jsonDecode(response.body);
return {
'success': false,
'error': responseData['error'] ?? 'Failed to delete transaction',
};
} catch (e) {
return {
'success': false,
'error': 'Failed to delete transaction: ${response.body}',
};
}
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
Future<Map<String, dynamic>> deleteMultipleTransactions({
required String accessToken,
required List<String> transactionIds,
}) async {
try {
final results = await Future.wait(
transactionIds.map((id) => deleteTransaction(
accessToken: accessToken,
transactionId: id,
)),
);
final allSuccess = results.every((result) => result['success'] == true);
if (allSuccess) {
return {
'success': true,
'deleted_count': transactionIds.length,
};
} else {
final failedCount = results.where((r) => r['success'] != true).length;
return {
'success': false,
'error': 'Failed to delete $failedCount transactions',
};
}
} catch (e) {
return {
'success': false,
'error': 'Network error: ${e.toString()}',
};
}
}
}