47 lines
1.5 KiB
Dart
47 lines
1.5 KiB
Dart
import 'package:flutter/foundation.dart';
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../../../errors/server_exception.dart';
|
|
import '../../../http_errors.dart';
|
|
import '../../nextcloud_ocs.dart';
|
|
import 'get_chat_params.dart';
|
|
import 'get_chat_response.dart';
|
|
|
|
/// GETs a chat page outside [TalkApi], which treats non-2xx as errors: here
|
|
/// HTTP 304 (nothing new / no older messages) is a normal outcome and yields
|
|
/// `null`.
|
|
Future<GetChatResponse?> fetchChatPage({
|
|
required String label,
|
|
required String chatToken,
|
|
required GetChatParams params,
|
|
Duration? timeout,
|
|
bool parseOffIsolate = false,
|
|
}) async {
|
|
final uri = NextcloudOcs.uri(
|
|
'apps/spreed/api/v1/chat/$chatToken',
|
|
queryParameters: params.toJson(),
|
|
);
|
|
final headers = NextcloudOcs.headers();
|
|
|
|
final response = (await sendGuarded('$label $uri', () {
|
|
final request = http.get(uri, headers: headers);
|
|
return timeout == null ? request : request.timeout(timeout);
|
|
}))!;
|
|
|
|
final status = response.statusCode;
|
|
if (status == 304) return null;
|
|
if (status >= 200 && status < 300) {
|
|
final parsed = parseOffIsolate
|
|
? await compute(_parseChatResponse, response.body)
|
|
: _parseChatResponse(response.body);
|
|
return parsed..headers = response.headers;
|
|
}
|
|
throw ServerException(
|
|
statusCode: status,
|
|
technicalDetails: '$label $uri: HTTP $status',
|
|
);
|
|
}
|
|
|
|
GetChatResponse _parseChatResponse(String body) =>
|
|
GetChatResponse.fromJson(NextcloudOcs.decode(body));
|