import 'dart:convert'; import 'package:http/http.dart' as http; import 'package:simplecloudnotifier/models/key_token_auth.dart'; import 'package:simplecloudnotifier/models/user.dart'; import '../models/channel.dart'; import '../models/message.dart'; enum ChannelSelector { owned(apiKey: 'owned'), // Return all channels of the user subscribedAny(apiKey: 'subscribed_any'), // Return all channels that the user is subscribing to allAny(apiKey: 'all_any'), // Return channels that the user owns or is subscribing subscribed(apiKey: 'subscribed'), // Return all channels that the user is subscribing to (even unconfirmed) all(apiKey: 'all'); // Return channels that the user owns or is subscribing (even unconfirmed) const ChannelSelector({required this.apiKey}); final String apiKey; } class APIClient { static const String _base = 'https://simplecloudnotifier.de/api/v2'; static Future verifyToken(String uid, String tok) async { final uri = Uri.parse('$_base/users/$uid'); final response = await http.get(uri, headers: {'Authorization': 'SCN $tok'}); return (response.statusCode == 200); } static Future getUser(String uid, String tok) async { final uri = Uri.parse('$_base/users/$uid'); final response = await http.get(uri, headers: {'Authorization': 'SCN $tok'}); if (response.statusCode != 200) { throw Exception('API request failed'); } return User.fromJson(jsonDecode(response.body)); } static getChannelList(KeyTokenAuth auth, ChannelSelector sel) async { var url = '$_base/users/${auth.userId}/channels?selector=${sel.apiKey}'; final uri = Uri.parse(url); final response = await http.get(uri, headers: {'Authorization': 'SCN ${auth.token}'}); if (response.statusCode != 200) { throw Exception('API request failed'); } final data = jsonDecode(response.body); return data['channels'].map((e) => ChannelWithSubscription.fromJson(e)).toList(); } static getMessageList(KeyTokenAuth auth, String pageToken, int? pageSize) async { var url = '$_base/messages?next_page_token=$pageToken'; if (pageSize != null) { url += '&page_size=$pageSize'; } final uri = Uri.parse(url); final response = await http.get(uri, headers: {'Authorization': 'SCN ${auth.token}'}); if (response.statusCode != 200) { throw Exception('API request failed'); } final data = jsonDecode(response.body); final npt = data['next_page_token'] as String; final messages = data['messages'].map((e) => Message.fromJson(e)).toList(); return [npt, messages]; } }