channel and message lists

This commit is contained in:
2024-02-18 17:36:58 +01:00
parent 1286a5d848
commit 56d49d8c5e
15 changed files with 484 additions and 23 deletions

View File

@@ -1,8 +1,23 @@
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';
@@ -23,4 +38,41 @@ class APIClient {
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<ChannelWithSubscription>((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<Message>((e) => Message.fromJson(e)).toList();
return [npt, messages];
}
}