finnow/flutter_app/lib/api/accounts.dart

47 lines
1.5 KiB
Dart

import 'dart:convert';
import 'package:http/http.dart' as http;
class Account {
final int id;
final DateTime createdAt;
final bool archived;
final String type;
final String numberSuffix;
final String name;
final String currency;
final String description;
const Account(this.id, this.createdAt, this.archived, this.type,
this.numberSuffix, this.name, this.currency, this.description);
factory Account.fromJson(Map<String, dynamic> data) {
final id = data['id'] as int;
final createdAtStr = data['createdAt'] as String;
final createdAt = DateTime.parse(createdAtStr);
final archived = data['archived'] as bool;
final type = data['type'] as String;
final numberSuffix = data['numberSuffix'] as String;
final name = data['name'] as String;
final currency = data['currency'] as String;
final description = data['description'] as String;
return Account(id, createdAt, archived, type, numberSuffix, name, currency,
description);
}
}
Future<List<Account>> getAccounts(String token, String profileName) async {
final http.Response response = await http.get(
Uri.parse('http://localhost:8080/api/profiles/$profileName/accounts'),
headers: {
'Authorization': 'Bearer $token'
}
);
if (response.statusCode == 200) {
if (jsonDecode(response.body) == null) return [];
final data = jsonDecode(response.body) as List<dynamic>;
return data.map((obj) => Account.fromJson(obj)).toList();
} else {
throw Exception('Failed to get accounts.');
}
}