59 lines
1.7 KiB
Dart
59 lines
1.7 KiB
Dart
import 'dart:developer';
|
|
|
|
import 'package:http/http.dart' as http;
|
|
|
|
import '../../apiError.dart';
|
|
import '../../apiParams.dart';
|
|
import '../../apiRequest.dart';
|
|
import '../../apiResponse.dart';
|
|
import '../nextcloud_ocs.dart';
|
|
|
|
enum TalkApiMethod {
|
|
get,
|
|
post,
|
|
put,
|
|
delete,
|
|
}
|
|
|
|
abstract class TalkApi<T extends ApiResponse?> extends ApiRequest {
|
|
String path;
|
|
ApiParams? body;
|
|
Map<String, String>? headers;
|
|
Map<String, dynamic>? getParameters;
|
|
|
|
http.Response? response;
|
|
|
|
TalkApi(this.path, this.body, {this.headers, this.getParameters});
|
|
|
|
Future<http.Response>? request(Uri uri, ApiParams? body, Map<String, String>? headers);
|
|
T assemble(String raw);
|
|
|
|
Future<T> run() async {
|
|
final endpoint = NextcloudOcs.uri('apps/spreed/api/$path', queryParameters: getParameters);
|
|
final mergedHeaders = {...NextcloudOcs.headers(), ...?headers};
|
|
|
|
http.Response? data;
|
|
try {
|
|
data = await request(endpoint, body, mergedHeaders);
|
|
if (data == null) throw Exception('No response Data');
|
|
if (data.statusCode >= 400 || data.statusCode < 200) {
|
|
throw Exception("Response status code '${data.statusCode}' might indicate an error");
|
|
}
|
|
} catch (e) {
|
|
log(e.toString());
|
|
throw ApiError('Request $endpoint could not be dispatched: ${e.toString()}');
|
|
}
|
|
|
|
try {
|
|
final assembled = assemble(data.body);
|
|
assembled?.headers = data.headers;
|
|
return assembled;
|
|
} catch (e) {
|
|
final message = 'Error assembling Talk API ${T.toString()} message: ${e.toString()}'
|
|
' response with request body: $body and request headers: $mergedHeaders';
|
|
log(message);
|
|
throw Exception(message);
|
|
}
|
|
}
|
|
}
|