Implement Scanner-View

This commit is contained in:
2025-04-13 00:17:06 +02:00
parent c0b8a8a3f4
commit 95353735b0
19 changed files with 961 additions and 179 deletions

View File

@@ -5,6 +5,7 @@ import 'package:simplecloudnotifier/api/api_exception.dart';
import 'package:simplecloudnotifier/models/api_error.dart';
import 'package:simplecloudnotifier/models/client.dart';
import 'package:simplecloudnotifier/models/keytoken.dart';
import 'package:simplecloudnotifier/models/send_message_response.dart';
import 'package:simplecloudnotifier/models/sender_name_statistics.dart';
import 'package:simplecloudnotifier/models/subscription.dart';
import 'package:simplecloudnotifier/models/user.dart';
@@ -50,7 +51,8 @@ class MessageFilter {
}
class APIClient {
static const String _base = 'https://simplecloudnotifier.de/api/v2';
static const String _base = 'https://simplecloudnotifier.de';
static const String _prefix = '/api/v2';
static Future<T> _request<T>({
required String name,
@@ -61,10 +63,11 @@ class APIClient {
dynamic jsonBody,
String? authToken,
Map<String, String>? header,
bool? nonAPI,
}) async {
final t0 = DateTime.now();
final uri = Uri.parse('$_base/$relURL').replace(queryParameters: query ?? {});
final uri = Uri.parse('$_base${(nonAPI ?? false) ? '' : _prefix}/$relURL').replace(queryParameters: query ?? {});
final req = http.Request(method, uri);
@@ -380,9 +383,9 @@ class APIClient {
);
}
static Future<KeyTokenPreview> getKeyTokenPreview(TokenSource auth, String kid) async {
static Future<KeyTokenPreview> getKeyTokenPreviewByID(TokenSource auth, String kid) async {
return await _request(
name: 'getKeyTokenPreview',
name: 'getKeyTokenPreviewByID',
method: 'GET',
relURL: 'preview/keys/$kid',
fn: KeyTokenPreview.fromJson,
@@ -390,6 +393,16 @@ class APIClient {
);
}
static Future<KeyTokenPreview> getKeyTokenPreviewByToken(TokenSource auth, String tok) async {
return await _request(
name: 'getKeyTokenPreviewByToken',
method: 'GET',
relURL: 'preview/keys/$tok',
fn: KeyTokenPreview.fromJson,
authToken: auth.getToken(),
);
}
static Future<KeyToken> getKeyTokenByToken(String userid, String token) async {
return await _request(
name: 'getCurrentKeyToken',
@@ -410,11 +423,14 @@ class APIClient {
);
}
static Future<Subscription> subscribeToChannelbyID(TokenSource auth, String channelID) async {
static Future<Subscription> subscribeToChannelbyID(TokenSource auth, String channelID, {String? subscribeKey}) async {
return await _request(
name: 'subscribeToChannelbyID',
method: 'POST',
relURL: 'users/${auth.getUserID()}/subscriptions',
query: {
if (subscribeKey != null) 'chan_subscribe_key': [subscribeKey],
},
jsonBody: {
'channel_id': channelID,
},
@@ -458,4 +474,26 @@ class APIClient {
authToken: auth.getToken(),
);
}
static Future<SendMessageResponse> sendMessage(String userid, String keytoken, String text, {String? channel, String? content, String? messageID, int? priority, String? senderName, DateTime? timestamp}) async {
return await _request(
name: 'sendMessage',
method: 'POST',
relURL: '/send',
nonAPI: true,
jsonBody: {
'user_id': userid,
'key': keytoken,
'title': text,
if (channel != null) 'channel': channel,
if (content != null) 'content': content,
if (priority != null) 'priority': priority,
if (messageID != null) 'msg_id': messageID,
if (timestamp != null) 'timestamp': (timestamp.microsecondsSinceEpoch / 1000).toInt(),
if (senderName != null) 'sender_name': senderName,
},
fn: SendMessageResponse.fromJson,
authToken: null,
);
}
}