92 lines
3.0 KiB
Dart
92 lines
3.0 KiB
Dart
import 'dart:convert';
|
|
import 'package:http/http.dart' as http;
|
|
import 'share.dart';
|
|
|
|
class ShareService {
|
|
ShareService({http.Client? client, required this.baseUrl})
|
|
: _client = client ?? http.Client();
|
|
final http.Client _client;
|
|
final String baseUrl; // z.B. https://api.windesign.at
|
|
|
|
Map<String, String> _auth(String token, {Map<String, String>? extra}) =>
|
|
{'Authorization': 'Bearer $token', if (extra != null) ...extra};
|
|
|
|
Future<List<Share>> listShares(
|
|
{required int igelId, required String token}) async {
|
|
final r = await _client.get(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/igel/$igelId/shares'),
|
|
headers: _auth(token),
|
|
);
|
|
if (r.statusCode != 200) {
|
|
throw Exception('listShares failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
final List data = jsonDecode(r.body) as List;
|
|
return data.map((e) => Share.fromJson(e as Map<String, dynamic>)).toList();
|
|
}
|
|
|
|
Future<void> invite(
|
|
{required int igelId,
|
|
required String email,
|
|
required String role,
|
|
required String token}) async {
|
|
final r = await _client.post(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/igel/$igelId/shares'),
|
|
headers: _auth(token, extra: {'Content-Type': 'application/json'}),
|
|
body: jsonEncode({'email': email, 'role': role}),
|
|
);
|
|
if (r.statusCode != 201) {
|
|
throw Exception('invite failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
}
|
|
|
|
Future<void> updateRole(
|
|
{required int shareId,
|
|
required String role,
|
|
required String token}) async {
|
|
final r = await _client.patch(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/shares/$shareId'),
|
|
headers: _auth(token, extra: {'Content-Type': 'application/json'}),
|
|
body: jsonEncode({'role': role}),
|
|
);
|
|
if (r.statusCode != 200) {
|
|
throw Exception('updateRole failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
}
|
|
|
|
Future<void> revoke({required int shareId, required String token}) async {
|
|
final r = await _client.delete(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/shares/$shareId'),
|
|
headers: _auth(token),
|
|
);
|
|
if (r.statusCode != 200) {
|
|
throw Exception('revoke failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
}
|
|
|
|
Future<void> acceptInvite(
|
|
{required String tokenJwt, required String inviteToken}) async {
|
|
final r = await _client.post(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/invites/accept'),
|
|
headers: _auth(tokenJwt, extra: {'Content-Type': 'application/json'}),
|
|
body: jsonEncode({'token': inviteToken}),
|
|
);
|
|
if (r.statusCode != 200) {
|
|
throw Exception('acceptInvite failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
}
|
|
|
|
Future<List<SharedIgelItem>> listSharedWithMe({required String token}) async {
|
|
final r = await _client.get(
|
|
Uri.parse('$baseUrl/hedgehogs.php?r=/me/shared'),
|
|
headers: _auth(token),
|
|
);
|
|
if (r.statusCode != 200) {
|
|
throw Exception('listSharedWithMe failed (${r.statusCode}) ${r.body}');
|
|
}
|
|
final List data = jsonDecode(r.body) as List;
|
|
return data
|
|
.map((e) => SharedIgelItem.fromJson(e as Map<String, dynamic>))
|
|
.toList();
|
|
}
|
|
}
|